generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 261
Sheffield | 25-ITP-SEP | Jak Rhodes-Smith | Sprint 2 | coursework/sprint-2 #715
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
Open
jakr-s
wants to merge
7
commits into
CodeYourFuture:main
Choose a base branch
from
jakr-s:coursework/sprint-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+214
−59
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
47f18bb
Complete 1-key-errors
jakr-s f682c6c
Complete 2-mandatory-debug
jakr-s 29f55b4
Complete 3-mandatory-implement
jakr-s 4361e74
Complete 4-mandatory-interpret
jakr-s f7c0a35
Complete 5-stretch-extend
jakr-s 23a25ca
fix: correct time formatting for 12 PM and update edge case test
jakr-s e39749f
fix: improve BMI calculation precision and return type
jakr-s File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,18 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // The function is supposed to convert the first character of a string to uppercase | ||
| // There will be a syntax error due to the redeclaration of the variable 'str' | ||
|
|
||
| // call the function capitalise with a string input | ||
| // interpret the error message and figure out why an error is occurring | ||
| capitalise("hello"); | ||
|
|
||
| // function capitalise(str) { | ||
| // let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| // return str; | ||
| // } | ||
|
|
||
| // The error occurred because 'str' was declared again with 'let', which is not allowed. | ||
|
|
||
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,26 @@ | ||
| // Predict and explain first... | ||
| // The function is intended to take a decimal number and convert it to a percentage (e.g. 1.5 => 150%) | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // There will be a syntax error due to the redeclaration of the variable 'decimalNumber' | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| // function convertToPercentage(decimalNumber) { | ||
| // const decimalNumber = 0.5; | ||
| // const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
| // return percentage; | ||
| // } | ||
|
|
||
| console.log(decimalNumber); | ||
| // console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
| // Redeclaring the variable decimalNumber causes a syntax error | ||
| // The parameter decimalNumber cannot be logged to the console as it is a parameter and holds no value until the function is called | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function convertToPercentage(decimalNumber) { | ||
| return `${decimalNumber * 100}%`; | ||
| } | ||
|
|
||
| console.log(convertToPercentage(0.5)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,28 @@ | ||
|
|
||
| // 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 | ||
| // A number is not a valid parameter (Syntax Error) | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| // function square(3) { | ||
| // return num * num; | ||
| // } | ||
|
|
||
| // =============> write the error message here | ||
| // Failed to instrument Sprint-2/1-key-errors/2.js | ||
| // 7 | | ||
| // > 8 | function square(3) { | ||
| // | ^ SyntaxError: Unexpected token (8:16) | ||
| // 9 | return num * num; | ||
| // 10 | } | ||
| // 11 | | ||
| // at <rootDir>/Sprint-2/1-key-errors/2.js:8:16 | ||
|
|
||
| // =============> explain this error message here | ||
| // parameter names cannot start with a digit | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } | ||
|
|
||
| console.log(square(3)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,19 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // The result of a * b will be printed to the console | ||
| // Since the function does not return any value, it will return undefined | ||
|
|
||
| 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)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // Because the function is evaluated before the string is printed to the console, it will log the result of a * b to the console first, but because it does not return a value, the next console.log of the string will print 'The result of multiplying 10 and 32 is undefined' | ||
|
|
||
| // 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)}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,18 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // The function is intended to take 2 arguments and return the sum | ||
| // it will return undefined | ||
|
|
||
| // function sum(a, b) { | ||
| // return; | ||
| // a + b; | ||
| // } | ||
|
|
||
| // console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // it currently has a return statement that ends the function prematurely, causing it to return undefined | ||
| // Finally, correct the code to fix the problem | ||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| return a + b; | ||
| } | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,35 @@ | ||
| // Predict and explain first... | ||
| // The function intends to take a number and return it's last digit | ||
| // Each call of the function will return the same result | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| // 'The last digit of 42 is 3' | ||
| // 'The last digit of 105 is 3' | ||
| // 'The last digit of 806 is 3' | ||
|
|
||
| const num = 103; | ||
| // const num = 103; | ||
|
|
||
| function getLastDigit() { | ||
| return num.toString().slice(-1); | ||
| } | ||
| // function getLastDigit() { | ||
| // return num.toString().slice(-1); | ||
| // } | ||
|
|
||
| console.log(`The last digit of 42 is ${getLastDigit(42)}`); | ||
| console.log(`The last digit of 105 is ${getLastDigit(105)}`); | ||
| console.log(`The last digit of 806 is ${getLastDigit(806)}`); | ||
| // console.log(`The last digit of 42 is ${getLastDigit(42)}`); | ||
| // console.log(`The last digit of 105 is ${getLastDigit(105)}`); | ||
| // console.log(`The last digit of 806 is ${getLastDigit(806)}`); | ||
|
|
||
| // Now run the code and compare the output to your prediction | ||
| // =============> write the output here | ||
| // 'The last digit of 42 is 3' | ||
| // 'The last digit of 105 is 3' | ||
| // 'The last digit of 806 is 3' | ||
|
|
||
| // Explain why the output is the way it is | ||
| // =============> write your explanation here | ||
| // Because the function was not correctly defined to accept an argument, It was using a fixed variable instead | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function getLastDigit(num) { | ||
| return num.toString().slice(-1); | ||
| } | ||
|
|
||
| // This program should tell the user the last digit of each number. | ||
| // Explain why getLastDigit is not working properly - correct the problem | ||
| console.log(`The last digit of 42 is ${getLastDigit(42)}`); | ||
| console.log(`The last digit of 105 is ${getLastDigit(105)}`); | ||
| console.log(`The last digit of 806 is ${getLastDigit(806)}`); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,18 @@ | |
|
|
||
| function formatAs12HourClock(time) { | ||
| const hours = Number(time.slice(0, 2)); | ||
| const minutes = time.slice(3, 5); | ||
|
|
||
| if (hours === 0) { | ||
| return `12:${minutes} am`; | ||
| } | ||
|
|
||
| if (hours === 12) { | ||
| return `${hours}:${minutes} pm`; | ||
| } | ||
|
|
||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| return `${hours !== 12 ? hours - 12 : hours}:${minutes} pm`; | ||
| } | ||
| return `${time} am`; | ||
| } | ||
|
|
@@ -23,3 +33,75 @@ console.assert( | |
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| ); | ||
|
|
||
| // Midnight | ||
| const outMidnight = formatAs12HourClock("00:00"); | ||
| const targetMidnight = "12:00 am"; | ||
| console.assert( | ||
| outMidnight === targetMidnight, | ||
| `current output: ${outMidnight}, target output: ${targetMidnight}` | ||
| ); | ||
|
|
||
| // Noon | ||
| const outNoon = formatAs12HourClock("12:00"); | ||
| const targetNoon = "12:00 pm"; | ||
| console.assert( | ||
| outNoon === targetNoon, | ||
| `current output: ${outNoon}, target output: ${targetNoon}` | ||
| ); | ||
|
|
||
| // 1 PM | ||
| const out13 = formatAs12HourClock("13:00"); | ||
| const target13 = "1:00 pm"; | ||
| console.assert( | ||
| out13 === target13, | ||
| `current output: ${out13}, target output: ${target13}` | ||
| ); | ||
|
|
||
| // 12:59 PM | ||
| const out1259 = formatAs12HourClock("12:59"); | ||
| const target1259 = "12:59 pm"; | ||
| console.assert( | ||
| out1259 === target1259, | ||
| `current output: ${out1259}, target output: ${target1259}` | ||
| ); | ||
|
|
||
| // 11:59 PM | ||
| const out2359 = formatAs12HourClock("23:59"); | ||
| const target2359 = "11:59 pm"; | ||
| console.assert( | ||
| out2359 === target2359, | ||
| `current output: ${out2359}, target output: ${target2359}` | ||
| ); | ||
|
|
||
| // 01:00 AM | ||
| const out1 = formatAs12HourClock("01:00"); | ||
| const target1 = "01:00 am"; | ||
| console.assert( | ||
| out1 === target1, | ||
| `current output: ${out1}, target output: ${target1}` | ||
| ); | ||
|
Comment on lines
+78
to
+83
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. Some return values are padded with zeroes and some aren't. |
||
|
|
||
| // 10:30 AM | ||
| const out1030 = formatAs12HourClock("10:30"); | ||
| const target1030 = "10:30 am"; | ||
| console.assert( | ||
| out1030 === target1030, | ||
| `current output: ${out1030}, target output: ${target1030}` | ||
| ); | ||
|
|
||
| // 15:45 PM | ||
| const out1545 = formatAs12HourClock("15:45"); | ||
| const target1545 = "3:45 pm"; | ||
| console.assert( | ||
| out1545 === target1545, | ||
| `current output: ${out1545}, target output: ${target1545}` | ||
| ); | ||
|
|
||
| // Edge: single digit hour | ||
| const outSingle = formatAs12HourClock("7:45"); | ||
| const targetSingle = "7:45 am"; | ||
| console.assert( | ||
| outSingle === targetSingle, | ||
| `current output: ${outSingle}, target output: ${targetSingle}` | ||
| ); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
On this line
hourswill never be equal to 12. So the condition is alwaystrue.