-
-
Notifications
You must be signed in to change notification settings - Fork 262
Glasgow | 25-ITP-SEP | Alaa Tagi | Sprint 2 | coursework/sprint-2 #729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e3dd609
7f96fdf
4e3088e
ce8b66d
0a2d800
eb2af74
7a1579c
61620bc
5711722
544cb83
8c774bc
472c56d
24c915a
812128c
3865600
bfafdef
0671c48
3b782df
d059399
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,23 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> write your prediction here : | ||
| // The function capitalise is expected to take a string input and return the string with the first letter capitalised. When called with the input "hello", it should return "Hello". However, since the function body is empty, it will likely result in an error or return undefined. | ||
|
|
||
| // call the function capitalise with a string input | ||
| // interpret the error message and figure out why an error is occurring | ||
|
|
||
| // function capitalise(str) { | ||
| //let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| // return str; | ||
| //} | ||
|
|
||
| // =============> write your explanation here : | ||
| // The function capitalise is called with the string "hello" as an argument. The function takes the first character of the string, converts it to uppercase, and concatenates it with the rest of the string starting from the second character. The result is "Hello", which is then returned and logged to the console. There is no error in this code; it works as intended. | ||
|
|
||
| // =============> write your new code here : | ||
|
|
||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return capitalisedStr; | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| console.log(capitalise("hello")); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,22 @@ | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
|
|
||
| // this function should square any number but instead we're going to get an error | ||
|
|
||
| // =============> write your prediction of the error here | ||
| // =============> write your prediction of the error here : There will be a syntax error because the parameter name cannot be a number. | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| function square(3) { | ||
| return num * num; | ||
| } | ||
|
|
||
| // =============> write the error message here | ||
| // =============> write the error message here : SyntaxError: Unexpected number. | ||
|
|
||
| // =============> explain this error message here | ||
| // =============> explain this error message here : /*The error message shows that there is an unexpected number in the function parameter name. Function parameters must be valid identifiers (like variable names) and cannot be numeric literals.*/ | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } | ||
| console.log(square(3)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,21 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // =============> write your prediction here : This function doesn’t have a return statement. | ||
| // That means: even though it takes parameters, it won’t give any value back when called and the result will be undefined. | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
| //function multiply(a, b) { | ||
| //console.log(a * b); | ||
| //} | ||
|
|
||
| // console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your explanation here : /* The function multiply does not return any value, it only logs the result to the console. It returns undefined. To fix this, we need to add a return statement in the multiply function. */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. excellent explanation! |
||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| function multiply(a, b) { | ||
| return a * b; | ||
| } | ||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> write your prediction here : A syntax error for the semicolon after return. | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
|
|
@@ -8,6 +8,11 @@ function sum(a, b) { | |
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your explanation here : The semicolon after return at end of the statement. So there will be no value returned from the function. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you are moving in the right direction but the explanation is not entirely correct. Think about this -- would just removing semicolon fix the function? |
||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function sum(a, b) { | ||
| return a + b; | ||
| } | ||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,3 +4,23 @@ | |
| // You will need to declare a function called toPounds with an appropriately named parameter. | ||
|
|
||
| // You should call this function a number of times to check it works for different inputs | ||
|
|
||
| function toPounds(penceToPound) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very cleanly written code with super descriptive variable names. Great job! |
||
| const penceStringWithoutTrailingP = penceToPound.substring(0, penceToPound.length - 1); // Remove the P at the end. | ||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); // Make sure there 3 digits at least. | ||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2 | ||
| ); // get the pounds part( - the last two digits). | ||
| const pence = paddedPenceNumberString | ||
| .substring(paddedPenceNumberString.length - 2) | ||
| .padEnd(2, "0"); // get the pence part( they should be 2 digits). | ||
| return `£${pounds}.${pence}`; // The final format. | ||
| } | ||
| console.log(toPounds("3990p")); | ||
| console.log(toPounds("1500p")); | ||
| console.log(toPounds("150p")); | ||
| console.log(toPounds("100p")); | ||
| console.log(toPounds("70p")); | ||
| console.log(toPounds("50p")); | ||
| console.log(toPounds("0p")); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,115 @@ | ||
| // This is the latest solution to the problem from the prep. | ||
| // Make sure to do the prep before you do the coursework | ||
| // Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. | ||
|
|
||
| // Fixed function | ||
| function formatAs12HourClock(time) { | ||
| const hours = Number(time.slice(0, 2)); | ||
| // Validate input format | ||
| if (typeof time !== "string" || !time.includes(":")) { | ||
| return "Invalid time"; | ||
| } | ||
|
|
||
| const [hourStr, minuteStr] = time.split(":"); | ||
| const hours = Number(hourStr); | ||
| const minutes = Number(minuteStr); | ||
| let period = "am"; | ||
|
|
||
| // Validate range | ||
| if ( | ||
| isNaN(hours) || isNaN(minutes) || | ||
| hours < 0 || hours > 23 || | ||
| minutes < 0 || minutes > 59 | ||
| ) { | ||
| return "Invalid time"; | ||
| } | ||
|
|
||
| // Midnight | ||
| if (hours === 0) { | ||
| return `12:${minuteStr.padStart(2, "0")} am`; | ||
| } | ||
|
|
||
| // Noon | ||
| if (hours === 12) { | ||
| period = "pm"; | ||
| return `12:${minuteStr.padStart(2, "0")} ${period}`; | ||
| } | ||
|
|
||
| // PM hours | ||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| return `${hours - 12}:${minuteStr.padStart(2, "0")} pm`; | ||
| } | ||
| return `${time} am`; | ||
|
|
||
| // AM hours | ||
| return `${hours}:${minuteStr.padStart(2, "0")} ${period}`; | ||
| } | ||
|
|
||
| const currentOutput = formatAs12HourClock("08:00"); | ||
| const targetOutput = "08:00 am"; | ||
| // ✅ Re-tests | ||
| const currentOutput1 = formatAs12HourClock("08:00"); | ||
| const targetOutput1 = "8:00 am"; | ||
| console.assert( | ||
| currentOutput === targetOutput, | ||
| `current output: ${currentOutput}, target output: ${targetOutput}` | ||
| ); | ||
| currentOutput1 === targetOutput1, | ||
| `current output: ${currentOutput1}, target output: ${targetOutput1}` | ||
| ); | ||
|
|
||
| const currentOutput2 = formatAs12HourClock("23:00"); | ||
| const targetOutput2 = "11:00 pm"; | ||
| const currentOutput2 = formatAs12HourClock("7:45"); | ||
| const targetOutput2 = "7:45 am"; | ||
| console.assert( | ||
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| ); | ||
|
|
||
| const currentOutput3 = formatAs12HourClock("23:00"); | ||
| const targetOutput3 = "11:00 pm"; | ||
| console.assert( | ||
| currentOutput3 === targetOutput3, | ||
| `current output: ${currentOutput3}, target output: ${targetOutput3}` | ||
| ); | ||
|
|
||
| const currentOutput4 = formatAs12HourClock("12:00"); | ||
| const targetOutput4 = "12:00 pm"; | ||
| console.assert( | ||
| currentOutput4 === targetOutput4, | ||
| `current output: ${currentOutput4}, target output: ${targetOutput4}` | ||
| ); | ||
|
|
||
| const currentOutput5 = formatAs12HourClock("00:00"); | ||
| const targetOutput5 = "12:00 am"; | ||
| console.assert( | ||
| currentOutput5 === targetOutput5, | ||
| `current output: ${currentOutput5}, target output: ${targetOutput5}` | ||
| ); | ||
|
|
||
| const currentOutput6 = formatAs12HourClock("15:30"); | ||
| const targetOutput6 = "3:30 pm"; | ||
| console.assert( | ||
| currentOutput6 === targetOutput6, | ||
| `current output: ${currentOutput6}, target output: ${targetOutput6}` | ||
| ); | ||
|
|
||
| const currentOutput7 = formatAs12HourClock("11:45"); | ||
| const targetOutput7 = "11:45 am"; | ||
| console.assert( | ||
| currentOutput7 === targetOutput7, | ||
| `current output: ${currentOutput7}, target output: ${targetOutput7}` | ||
| ); | ||
|
|
||
| const currentOutput8 = formatAs12HourClock("25:30"); | ||
| const targetOutput8 = "Invalid time"; | ||
| console.assert( | ||
| currentOutput8 === targetOutput8, | ||
| `current output: ${currentOutput8}, target output: ${targetOutput8}` | ||
| ); | ||
|
|
||
| const currentOutput9 = formatAs12HourClock("1724688"); | ||
| const targetOutput9 = "Invalid time"; | ||
| console.assert( | ||
| currentOutput9 === targetOutput9, | ||
| `current output: ${currentOutput9}, target output: ${targetOutput9}` | ||
| ); | ||
|
|
||
| // ✅ Display output | ||
| console.log(formatAs12HourClock("08:00")); | ||
| console.log(formatAs12HourClock("7:45")); | ||
| console.log(formatAs12HourClock("23:00")); | ||
| console.log(formatAs12HourClock("12:00")); | ||
| console.log(formatAs12HourClock("00:00")); | ||
| console.log(formatAs12HourClock("15:30")); | ||
| console.log(formatAs12HourClock("11:45")); | ||
| console.log(formatAs12HourClock("25:30")); | ||
| console.log(formatAs12HourClock("1724688")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works nicely but as an optional exercise, could you think of a way to simplify the code within this function?