Repository navigation
London | 26-ITP-Sep | Anita Amirhaeri | Sprint 3 | coursework/sprint 3 #1636
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
2300ec7
a9cb566
a729eff
7cd8ae3
a128bbf
1d044cb
d1a35bc
35c7d91
2d78dfc
6956732
76ac665
0b2d01c
c287e7c
10d77c6
54f45d7
e9e592e
4815e4c
e1c461b
7a13acc
fd9940e
fa1b84b
37ef650
f98fb5e
2adce38
fa4e290
91bfb82
64e95e7
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 | ||
|
|
||
| // =============> We will have syntax error because string is the parameter of the function and the next line,it is declared wit let | ||
| //so it is declared two times in one part | ||
| // call the function capitalise with a string input | ||
|
|
||
| function capitalise(str) { | ||
| str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| } | ||
| console.log(capitalise("mother")); | ||
| // 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 | ||
| // =============> write your new code here | ||
| // =============> write your explanation here SyntaxError: Identifier 'str' has already been declared | ||
| // =============> write your new code here. | ||
| function capitalise(str) { | ||
| return `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| // Predict and explain first... | ||
| // Predict and explain first...:It doesn't run.We have a syntaxError. | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // Why will an error occur when this program runs?because decimalNumber is declared. | ||
| // as a parameter and then declared again using const. | ||
|
|
||
| // =============> write your prediction here:We will have a SyntaxError because decimalNumber is a parameter of the function, but it is declared again using const inside the function. | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
|
|
@@ -14,7 +16,14 @@ function convertToPercentage(decimalNumber) { | |
|
|
||
| console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your explanation here:decimalNumber is inside the function, so we can not use it outside the function. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function convertToPercentage(decimalNumber) { | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| ca | ||
|
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 is |
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(convertToPercentage(0.5)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,18 +3,23 @@ | |
|
|
||
| // this function should square any number but instead we're going to get an error | ||
|
|
||
| // =============> write your prediction of the error here | ||
| // =============> write your prediction of the error here:We will have a SyntaxError because we need a valid function parameter. | ||
|
|
||
| function square(3) { | ||
|
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. Your explanation is right. Now put |
||
| return num * num; | ||
| } | ||
|
|
||
| // =============> write the error message here | ||
| // =============> write the error message here:SyntaxError: Unexpected number | ||
|
|
||
| // =============> explain this error message here | ||
| // =============> explain this error message here:function square(3) { | ||
| //3 is a number, not a valid variable. Program expects a parameter name there. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
| // =============> 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,22 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // =============> write your prediction here: | ||
| //The code will run, but the output won't be what we expect because the multiply function logs the result instead of returning it. | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // =============> write your explanation here:console.log(a * b); | ||
| //The result is 320, but the function doesn't return a value. It shows us undefined because we don't have return in the function. | ||
| //320 | ||
| //The result of multiplying 10 and 32 is undefined | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // =============> 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,7 +1,7 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| // =============> Write your prediction here:I predict the output will be 3 every time, because the function uses the global constant num = 103 instead of using the numbers passed into getLastDigit(). | ||
|
|
||
| const num = 103; | ||
|
|
||
|
|
@@ -14,11 +14,28 @@ 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 | ||
| // =============> 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 | ||
| // =============> write your explanation here:there is no parameter in the function. | ||
| //So when we call the function: | ||
| //getLastDigit(42) | ||
| //the 42 is passed to the function, but the function has no parameter to receive it. | ||
| //Instead, it uses the global variable (const num = 103) | ||
| //so every call converts 103 to a string and gets its last digit: 3. | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // =============> write your new code here: | ||
| const num = 103; | ||
|
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. node stops 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)}`); | ||
|
|
||
| // 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 |
|---|---|---|
|
|
@@ -15,5 +15,7 @@ | |
| // It should return a string of their Body Mass Index to 1 decimal place | ||
|
|
||
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| const bmi = weight / (height * height); | ||
| return bmi.toFixed(1); | ||
|
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.
|
||
| } | ||
| // return the BMI of someone based off their weight and height | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,18 +21,41 @@ function formatTimeDisplay(seconds) { | |
| // Questions | ||
|
|
||
| // a) When formatTimeDisplay is called how many times will pad be called? | ||
| // =============> write your answer here | ||
| // =============> write your answer here: three calls to pad() in the return statement: | ||
| //return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; | ||
|
|
||
| // Call formatTimeDisplay with an input of 61, now answer the following: | ||
|
|
||
| // b) What is the value assigned to num when pad is called for the first time? | ||
| // =============> write your answer here | ||
| // =============> write your answer here: | ||
| //For formatTimeDisplay(61): | ||
| //remainingSeconds = 61 % 60 = 1 | ||
| //totalMinutes = (61 - 1) / 60 → 1 | ||
| //remainingMinutes = 1 % 60 → 1 | ||
| //totalHours = (1 - 1) / 60 → 0 | ||
| //num = 1 | ||
|
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. Line 15 calls |
||
|
|
||
| // c) What is the return value of pad when it is called for the first time? | ||
| // =============> write your answer here | ||
| // =============> write your answer here:num is 0. | ||
| //let numString = num.toString(); gives us 0 | ||
|
|
||
| // This loop adds a "0" because the string has only one character: | ||
| //numString = "0" + "0"; | ||
| //return value is: | ||
| //"00" | ||
|
|
||
| // d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer | ||
| // =============> write your answer here | ||
| // =============> write your answer here: | ||
| //The last call is: pad(remainingSeconds) | ||
|
|
||
| //For 61 seconds, remainingSeconds is 61 % 60 = 1: | ||
| //so num = 1 | ||
|
|
||
| // e) What is the return value of pad when it is called for the last time in this program? Explain your answer | ||
| // =============> write your answer here | ||
| // =============> write your answer here: | ||
| //num is 1, so: | ||
| //num.toString() gives "1". | ||
|
|
||
| // and this loop adds a 0 to the beginning: "01" | ||
|
|
||
| //So the final result of formatTimeDisplay(61) is: 00:01:01 | ||
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.
Run
node 0.js: node stops at line 14 with the SyntaxError, so this line never runs. Put//in front of lines 13 to 16, the original code. Lines 6 to 10 and 20 to 22 do the same thing now, so you only need one of them.