-
-
Notifications
You must be signed in to change notification settings - Fork 239
West Midlands | ITP-Sept-25 | Mustaf Asani | Sprint 2 | coursework/sprint-2 #777
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
26fd354
5ab6322
0c7cc6f
cc216e1
90013dc
f63f7cf
a02b0c5
e097316
742c228
0d5ad45
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 | ||
| //we should have an error showing that str already exists so we cannot declare it again inside the function. | ||
|
|
||
| // 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; | ||
| } | ||
| //function capitalise(str) { | ||
| // let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| // return str; | ||
| //} | ||
|
|
||
| //console.log(capitalise("cat")); | ||
|
|
||
| // =============> write your explanation here | ||
| //str has been passed down as a parameter so we cannot declare it again. we would need to declare a different variable name. | ||
| // =============> write your new code here | ||
| function capitalise(str) { | ||
| let capitalisedStr = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return capitalisedStr; | ||
| } | ||
|
|
||
| console.log(capitalise("cat")); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,23 @@ | ||
|
|
||
| // 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 | ||
| //it will throw an error for use of a primitive value instead of a variable. | ||
|
|
||
| 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 | ||
|
|
||
| //this error message is becasue we cannot pass a direct value to a function when creating it but rather when we call it. Instead we need to give it a parameter that can then hold the value that we want to pass to the 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,21 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| //line 7 will log the result of a * b, line 10 will throw an error since the function does not return anything so it will say The result of multiplying 10 and 32 is 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 | ||
| //since the function does not return a value we get undefined passed back to the console. | ||
|
|
||
| // 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,20 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // the console will show The sum of 10 and 32 is undefined, this is because the return command is before the calculation so the return has no value to bring back. | ||
|
|
||
| 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 | ||
| //the console will show The sum of 10 and 32 is undefined, this is because the return command is before the calculation so the return has no value to bring back. | ||
| // 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 |
|---|---|---|
|
|
@@ -15,5 +15,14 @@ | |
| // It should return their Body Mass Index to 1 decimal place | ||
|
|
||
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| } | ||
| // return the BMI of someone based off their weight and height | ||
| const bmi = weight / height ** 2; | ||
| return bmi.toFixed(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. What type of value do you expect the function to return? A number or a string? |
||
| } | ||
|
|
||
| console.log(`Your BMI is ${calculateBMI(70, 1.73)}`); | ||
|
|
||
| //const actualOutput = calculateBMI(70, 1.73); | ||
| //const targetOutput = "23.4"; | ||
|
|
||
| //console.assert(actualOutput === targetOutput, `That is not the correct BMI`); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,14 @@ | |
|
|
||
| function formatAs12HourClock(time) { | ||
| const hours = Number(time.slice(0, 2)); | ||
| const minutes = time.slice(3); | ||
| console.log(minutes); | ||
|
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. Code submitted to a PR should be clean. So it is best practices to not leaving any debugging statement in the code. |
||
| if (hours == 12) { | ||
| return `${hours}:${minutes} pm`; | ||
| } | ||
|
|
||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| return `${hours - 12}:${minutes} pm`; | ||
| } | ||
| return `${time} am`; | ||
| } | ||
|
Comment on lines
+12
to
17
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 do you expect from the following function calls? formatAs12HourClock("00:00");
formatAs12HourClock("01:01");
formatAs12HourClock("13:01"); Does your function return the value you expected? |
||
|
|
@@ -23,3 +29,17 @@ console.assert( | |
| currentOutput2 === targetOutput2, | ||
| `current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
| ); | ||
|
|
||
| const currentOutput3 = formatAs12HourClock("23:30"); | ||
| const targetOutput3 = "11:30 pm"; | ||
| console.assert( | ||
| currentOutput3 === targetOutput3, | ||
| `current output: ${currentOutput3}, target output: ${targetOutput3}` | ||
| ); | ||
|
|
||
| const currentOutput4 = formatAs12HourClock("12:01"); | ||
| const targetOutput4 = "12:01 pm"; | ||
| console.assert( | ||
| currentOutput4 === targetOutput4, | ||
| `current output: ${currentOutput4}, target output: ${targetOutput4}` | ||
| ); | ||
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.
We could also move the declaration at line 10 outside the function without modifying the
console.logstatement.Variables in different scopes can have the same name and the JS language has a set of rules to resolve "conflicting" names.
Do look up these two concepts, identifier scope and identifier resolution, in the context of JavaScript programming. ChatGPT can give a good explanation.