-
-
Notifications
You must be signed in to change notification settings - Fork 245
Manchester | 25-ITP-Sep | Mahtem T. Mengstu | Sprint 2 | coursework/sprint-2 #805
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 11 commits
4e992fc
be4ee28
45af577
ef668cc
4ef9f59
b7707e3
3ba023b
3d61c7f
971777e
e9f24d4
8da53ea
4d2946e
976687d
2cfbda1
46f9c56
c197bdc
c6c3d9d
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,24 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> I guess we would'nt need to say let in line 8 because str is already decleared as an argument | ||
| // within the function capitalise(str). | ||
| // | ||
|
|
||
| // 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; | ||
| //} | ||
|
|
||
| //capitalise("hello") | ||
| // =============> A syntaxError occured as 'str' is already declared. | ||
| // =============> write your new code here | ||
|
|
||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
|
|
||
| str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
|
|
||
| // =============> 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,34 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
|
|
||
| // =============> I think, the function convertToPercentage is not called at line 16, and | ||
| // there is no any importance of declaring cons decimalNumber as it is already declared. | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| //function convertToPercentage(decimalNumber) { | ||
| // const decimalNumber = 0.5; | ||
| // const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| // return percentage; | ||
| //} | ||
|
|
||
| //console.log(decimalNumber); | ||
|
|
||
| // =============> A syntaxError appeared indicating 'decimalNumber' has already been declared. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
|
|
||
| function convertToPercentage() { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(decimalNumber); | ||
| console.log(convertToPercentage()); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // The new code run smoothly and retuned "50%" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,22 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // =============> In line 6 instead of console.log, I would expect retun a*b; the code will result in | ||
| // just printing a*b | ||
|
|
||
| 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 | ||
| // =============> After running it returned 320 The result of multiplying 10 and 32 is undefined | ||
| // Because the multiply parameters were not taken as paramaters within a retunr. | ||
|
|
||
| // 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,14 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> The semi colon ; after retunr will results in syntax error. | ||
|
|
||
| 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 | ||
| // =============> Unlike my expectation the code run without error as "The sum of 10 and 32 is undefined" | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| // we just need to delete the ; after return and bring a+b side by side as retun a+b; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,35 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| // =============> The code will first return 3 as string and after calling the function getLastDigit, | ||
| // it will not return anything because every number pased will be changed to string. | ||
|
|
||
| const num = 103; | ||
| // const num = 103; | ||
|
|
||
| function getLastDigit() { | ||
| // 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)}`); | ||
|
|
||
| // Now run the code and compare the output to your prediction | ||
| // =============> The last digit of 42 is 3 three times.. | ||
| // // =============> my prediction was somehow closer but not exact, the case is parameter num needs to be with the getLastDifit | ||
| // function instead of const num = 103. | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| function getLastDigit(num) { | ||
| 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)}`); | ||
|
|
||
| // Now run the code and compare the output to your prediction | ||
| // =============> write the output here | ||
| // Explain why the output is the way it is | ||
| // =============> write your explanation here | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
|
|
||
| // This program should tell the user the last digit of each number. | ||
| // Explain why getLastDigit is not working properly - correct the problem | ||
|
|
||
| //the case is parameter num needs to be with the getLastDigit function instead of const num = 103. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // 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. | ||
| // 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)); | ||
|
|
@@ -23,3 +24,63 @@ console.assert( | |
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| ); | ||
|
|
||
| const currentOutput3 = formatAs12HourClock("00:00"); | ||
| const targetOutput3 = "12:00 am"; | ||
| 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("13:00"); | ||
| const targetOutput5 = "01:00 pm"; | ||
| console.assert( | ||
| currentOutput5 === targetOutput5, | ||
| `current output: ${currentOutput5}, target output: ${targetOutput5}` | ||
|
|
||
| ); | ||
|
|
||
| const currentOutput7 = formatAs12HourClock("25:00"); | ||
| const targetOutput7 = "01:00 am"; | ||
| console.assert( | ||
| currentOutput7 === targetOutput7, | ||
| `current output: ${currentOutput7}, target output: ${targetOutput7}` | ||
|
|
||
| ) | ||
|
|
||
| // console.assert(formatAs12HourClock("08:00") === "08:00 am"); | ||
| // console.assert(formatAs12HourClock("23:00") === "11:00 pm"); | ||
| // console.assert(formatAs12HourClock("22:00") === "10:00 pm"); | ||
|
|
||
| // Modified Code: | ||
|
|
||
| function formatAs12HourClock(time) { | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let hours = Number(time.slice(0, 2)); | ||
| const minutes = time.slice(3); | ||
cjyuan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| let suffix; | ||
| if (hours >= 12) { | ||
| suffix = "pm"; | ||
| } else { | ||
| suffix = "am"; | ||
| } | ||
|
|
||
| hours = hours % 12 || 12; // convert 0 to 12, 13 to 1 | ||
|
||
| const formattedHours = hours.toString().padStart(2, "0"); | ||
|
|
||
| return `${formattedHours}:${minutes} ${suffix}`; | ||
|
|
||
| } | ||
|
|
||
| const currentOutput6 = formatAs12HourClock("23:00"); | ||
| const targetOutput6 = "11:00 pm"; | ||
| console.assert( | ||
| currentOutput6 === targetOutput6, | ||
| `current output: ${currentOutput6}, target output: ${targetOutput6}` | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.