-
-
Notifications
You must be signed in to change notification settings - Fork 266
West Midlands | ITP September 2025 | Jonathan Boahene | Sprint 2 | Module Structuring and Testing Data #825
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
1eb4e07
bc143ce
5cb7a47
b383e53
ea7eebf
d8487ec
fcb215c
db6578c
7229e50
ba0a3a8
fe53ddf
09b1e52
75983a9
42f5fa5
4e090d9
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,22 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> write your prediction here => This code will throw a syntax error | ||
| // because the parameter str is redeclared as a let variable inside the function, which is not allowed. | ||
|
|
||
| // 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 error occurs because we cannot declare | ||
| // a new variable with the same name as a function parameter (str) using let inside the function. | ||
| // This causes a "Identifier 'str' has already been declared" error according to mdn. | ||
|
|
||
| // =============> write your new code here | ||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| return `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| console.log(capitalise("bethan")); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,25 @@ | ||
|
|
||
| // 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 | ||
| // The code will throw a syntax error because you cannot use a number as a function parameter. | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| // function square(3) { | ||
| // return num * num; | ||
| // } | ||
|
|
||
| // =============> write the error message here | ||
| // SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| // Function parameters must be variable names, not values. Using a number as a parameter is invalid syntax in JavaScript -mdn | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
| function square(num) { | ||
| return num * num; | ||
| } | ||
|
|
||
|
|
||
| console.log(square(7)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,23 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // function multiply(a, b) { | ||
| // console.log(a * b); | ||
| // } | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
| // console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
| // The output will be: | ||
| // 320 | ||
| // The result of multiplying 10 and 32 is undefined | ||
| // This is because multiply logs the result but does not return it, so the template literal gets undefined. | ||
|
|
||
| // =============> write your explanation here | ||
| // The function multiply only prints the result to the console and does not return anything. | ||
| // When we use multiply(10, 32) inside the template literal, it evaluates to undefined because the function has no return value. | ||
|
|
||
| // 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,13 +1,21 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // function sum(a, b) { | ||
| // return; | ||
| // a + b; | ||
| // } | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
| // console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
| // The output will be: The sum of 10 and 32 is undefined | ||
| // This is because the function returns immediately with no value, so a + b is never executed. | ||
|
|
||
| // =============> write your explanation here | ||
| // The return statement ends the function before a + b is calculated, so the function returns undefined. | ||
|
|
||
| // 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,21 @@ | |
| // 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(penceString) { | ||
| // Remove trailing 'p' and pad with zeros to ensure at least 3 digits | ||
| const pence = penceString | ||
| .substring(0, penceString.length - 1) | ||
|
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. This is clever and handles edge cases very well. |
||
| .padStart(3, "0"); | ||
| // Get pounds and pence parts | ||
| const pounds = pence.substring(0, pence.length - 2); | ||
| const pencePart = pence.substring(pence.length - 2).padEnd(2, "0"); | ||
| // Return formatted string | ||
| return `£${pounds}.${pencePart}`; | ||
|
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. 💡 Nice that you included the £ symbol directly. |
||
| } | ||
|
|
||
| console.log(toPounds("399p")); // £3.99 | ||
| console.log(toPounds("9p")); // £0.09 | ||
| console.log(toPounds("99p")); // £0.99 | ||
| console.log(toPounds("100p")); // £1.00 | ||
| console.log(toPounds("1234p")); // £12.34 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,24 +2,71 @@ | |
| // 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. | ||
|
|
||
| // function formatAs12HourClock(time) { | ||
| // const hours = Number(time.slice(0, 2)); | ||
| // if (hours > 12) { | ||
| // return `${hours - 12}:00 pm`; | ||
| // } | ||
| // return `${time} am`; | ||
| // } | ||
|
|
||
| // const currentOutput = formatAs12HourClock("08:00"); | ||
| // const targetOutput = "08:00 am"; | ||
| // console.assert( | ||
| // currentOutput === targetOutput, | ||
| // `current output: ${currentOutput}, target output: ${targetOutput}` | ||
| // ); | ||
|
|
||
| // const currentOutput2 = formatAs12HourClock("23:00"); | ||
| // const targetOutput2 = "11:00 pm"; | ||
| // console.assert( | ||
| // currentOutput2 === targetOutput2, | ||
| // `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| // ); | ||
|
|
||
| function formatAs12HourClock(time) { | ||
|
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. solid implementation |
||
| const hours = Number(time.slice(0, 2)); | ||
| const minutes = time.slice(3, 5); | ||
|
|
||
| if (hours === 0) { | ||
| return `12:${minutes} am`; | ||
| } | ||
| if (hours === 12) { | ||
| return `12:${minutes} pm`; | ||
| } | ||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| return `${pad(hours - 12)}:${minutes} pm`; | ||
| } | ||
| return `${time} am`; | ||
| return `${pad(hours)}:${minutes} am`; | ||
| } | ||
|
|
||
| function pad(num) { | ||
| return num.toString().padStart(2, "0"); | ||
| } | ||
|
|
||
| const currentOutput = formatAs12HourClock("08:00"); | ||
| const targetOutput = "08:00 am"; | ||
| // Test cases for different groups of input data and edge cases | ||
| console.assert( | ||
| currentOutput === targetOutput, | ||
| `current output: ${currentOutput}, target output: ${targetOutput}` | ||
| formatAs12HourClock("00:00") === "12:00 am", | ||
| "Test midnight failed" | ||
| ); | ||
|
|
||
| const currentOutput2 = formatAs12HourClock("23:00"); | ||
| const targetOutput2 = "11:00 pm"; | ||
| console.assert(formatAs12HourClock("01:00") === "01:00 am", "Test 1am failed"); | ||
| console.assert(formatAs12HourClock("08:00") === "08:00 am", "Test 8am failed"); | ||
| console.assert(formatAs12HourClock("12:00") === "12:00 pm", "Test noon failed"); | ||
| console.assert(formatAs12HourClock("13:00") === "01:00 pm", "Test 1pm failed"); | ||
| console.assert( | ||
| formatAs12HourClock("11:59") === "11:59 am", | ||
| "Test 11:59am failed" | ||
| ); | ||
| console.assert( | ||
| formatAs12HourClock("12:59") === "12:59 pm", | ||
| "Test 12:59pm failed" | ||
| ); | ||
| console.assert( | ||
| formatAs12HourClock("15:30") === "03:30 pm", | ||
| "Test 3:30pm failed" | ||
| ); | ||
| console.assert(formatAs12HourClock("23:00") === "11:00 pm", "Test 11pm failed"); | ||
| console.assert( | ||
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| formatAs12HourClock("23:59") === "11:59 pm", | ||
| "Test 11:59pm failed" | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // function decimalTopercent(num){ | ||
| // let percent = num * 100; | ||
| // return percent | ||
| // } | ||
| // console.log(decimalTopercent(0.5) + "%"); | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| return percentage; | ||
| } | ||
|
|
||
| const result = convertToPercentage(0.5); | ||
| const result1 = convertToPercentage(0.231); | ||
| console.log(result); | ||
| console.log(result1); |
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.
Good, clear function name.