-
-
Notifications
You must be signed in to change notification settings - Fork 546
London | 26-ITP-Sep | Alan Mak | Sprint 3 | Module-JavaScript-Fundamentals Coursework #1619
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 25 commits
576dc5a
342efc6
f47f36b
41e9a7a
9c61083
3ecd296
55f005d
bc68642
a243d6d
1b09621
742ac3f
9ba67d2
6be655b
cf2bf33
9a86ff0
0142183
c4affdd
d35a846
214b717
8e32548
1d3a340
b8dc470
773c6bc
01c7b46
fe78cd5
fa9defb
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,12 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> I expect the function capitalise will turn first letter of string given by user into capital letter. | ||
|
|
||
| // call the function capitalise with a string input | ||
| // interpret the error message and figure out why an error is occurring | ||
|
|
||
| // =============> The result was a Syntax error because str has been declared as parameter of function captialise before let reassign str. | ||
| // =============> | ||
| 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,17 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // =============> SyntaxError will occurs | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| // =============> decimalNumber first declared as parameter, it cannot be reassigned as const. | ||
| // Also, console.log try to run decimalNumber as a variable while decimalNumber was assigned as a parameter. | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| console.log(convertToPercentage(0.5)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,17 @@ | ||
|
|
||
| // 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 | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| // =============> I expect SyntaxError will result | ||
|
|
||
| // =============> write the error message here | ||
| // =============> SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| // =============> Numerical character "3" cannot be named as parameter of a function. | ||
|
|
||
| // 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,18 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // =============> Undefined at the end of sentence. console.log in local function will result the product. But console.log in global scope will not call function multiply. | ||
|
|
||
| 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); | ||
| //} | ||
|
|
||
| // =============> write your explanation here | ||
| //console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> The function multiply does not really carry out the multiplication. It only logs the result of a*b. That's why calling multiply become 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)}`); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,18 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> I predict a SyntaxError in original code | ||
|
|
||
| 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)}`); | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> Expression a + b is not assigned to return. | ||
| // 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 |
|---|---|---|
| @@ -1,24 +1,36 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| // =============> Code returns last digit of 103 which is the value of num assigned in global scope. | ||
|
|
||
| //const num = 103; | ||
|
|
||
| //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 | ||
| // The last digit of 105 is 3 | ||
| // The last digit of 806 is 3 | ||
| // Explain why the output is the way it is | ||
| // =============> The function refers to the value of `num` assigned in global scope rather than getLastDigit, because 'num' was not a parameter in function name. | ||
| // Finally, correct the code to fix the problem | ||
| // =============> | ||
| const num = 103; | ||
|
|
||
| function getLastDigit() { | ||
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,22 +4,62 @@ | |
|
|
||
| function formatAs12HourClock(time) { | ||
| const hours = Number(time.slice(0, 2)); | ||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| const minutes = time.slice(-2); | ||
| if (hours >= 24) { | ||
| return `0${hours - 24}:${minutes} am +1`; | ||
| } else if (hours > 22) { | ||
| return `${hours - 12}:${minutes} pm`; | ||
| } else if (hours > 12) { | ||
| return `0${hours - 12}:${minutes} pm`; | ||
| } else if (hours > 9) { | ||
| return `${hours}:${minutes} am`; | ||
| } | ||
| return `${time} am`; | ||
| return `0${hours}:${minutes} am`; | ||
| } | ||
|
|
||
| const currentOutput = formatAs12HourClock("08:00"); | ||
| const targetOutput = "08:00 am"; | ||
| const currentOutput = formatAs12HourClock("08:01"); | ||
| const targetOutput = "08:01 am"; | ||
| console.assert( | ||
| currentOutput === targetOutput, | ||
| `current output: ${currentOutput}, target output: ${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}` | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}`, | ||
| ); | ||
|
|
||
| const currentOutput3 = formatAs12HourClock("25:00"); | ||
| const targetOutput3 = "01:00 am +1"; | ||
| console.assert( | ||
| currentOutput3 === targetOutput3, | ||
| `current output: ${currentOutput3}, target output: ${targetOutput3}`, | ||
| ); | ||
|
|
||
| const currentOutput4 = formatAs12HourClock("00:01"); | ||
| const targetOutput4 = "00:01 am"; | ||
|
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. In 12-hour clock, |
||
| console.assert( | ||
| currentOutput4 === targetOutput4, | ||
| `current output: ${currentOutput4}, target output: ${targetOutput4}`, | ||
| ); | ||
|
|
||
| const currentOutput5 = formatAs12HourClock("11:01"); | ||
| const targetOutput5 = "11:01 am"; | ||
| console.assert( | ||
| currentOutput5 === targetOutput5, | ||
| `current output: ${currentOutput5}, target output: ${targetOutput5}`, | ||
| ); | ||
|
|
||
| console.log(formatAs12HourClock("08:01")); | ||
| console.log(formatAs12HourClock("23:55")); | ||
| console.log(formatAs12HourClock("25:01")); | ||
|
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. What output do you expect from
Author
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. Good catch thanks! Fixed to ensure leading zeros for single-digit hours, and to ensure correct am/pm representation |
||
| console.log(formatAs12HourClock("00:01")); | ||
| console.log(formatAs12HourClock("11:01")); | ||
|
|
||
| //Two patches | ||
| //First hour input > 24, current algo is hours - 12 , e.g. 25-12 and print 13:00pm , rather than 1:00am (+1) | ||
| //to Define `hoursOfOneDay` = 24 | ||
| //2 min input | ||
| //to Define | ||
Uh oh!
There was an error while loading. Please reload this page.