-
-
Notifications
You must be signed in to change notification settings - Fork 239
Manchester | 25-ITP-Sep | Hani Sadah| Sprint 2 | Coursework/sprint 2 #836
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
15dadf1
c642719
f91a49c
a33f384
a734ad2
336d1b4
11a298c
c14e9d7
97da7db
23ddb39
2f8ccfb
25bda16
db3249a
e03cac5
cb2de15
6941206
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 | ||
| // =============> I predict that we will not need to write let in line 8 as str is already declared | ||
| // as an argument within the function captialise. | ||
|
|
||
| // 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 occurred as 'str' is being cleared. | ||
|
|
||
|
|
||
| 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,33 @@ | ||
|
|
||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // =============> I believe that the function convertToPercentage is not mentioned in the console.log statement. | ||
| // Declaring the const decimalNumber is unnecessary as it is already declared as a parameter within the function convertToPercentage. | ||
|
|
||
| // 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 occurred saying decimalNumber is already 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); | ||
|
|
||
| // =============> write your explanation here | ||
| console.log(convertToPercentage()); | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| // the new code runs without error and outputs "50%" | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,26 @@ | ||
|
|
||
|
|
||
| // 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 | ||
| // =============> The num parameter should be mentioned inside the function square instead of the number 3. | ||
| // then it should be called as square (3) in the console.log statement. | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| //function square(3) { | ||
| //return num * num; | ||
| //} | ||
|
|
||
| // =============> function square(3) syntaxError: Unexpected number. | ||
| // =============> 3 wasn't expected... instead a declaration like 'num' was expected. | ||
|
|
||
| // =============> write the error message here | ||
|
|
||
| // =============> explain this error message here | ||
|
|
||
| // 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 | ||
| // =============> In line 6, instead of console.log printing the result of multiply(10, 32), return a*b would be better | ||
| //because the console.log would only print 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 the code, we got 'the result of multiplying 10 and 32 is undefined' | ||
| // as the multiply parameters weren't considered as parameters inside the function multiply. | ||
|
|
||
| // 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,13 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> SyntaxError will occur due to putting a semicolon after return. | ||
|
|
||
| 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 the expectation that the code runs 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 needed just to remove the semicolon after return statement and to bring up the a+b expression side by side with return. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,34 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| // =============> The code will return 3 as a string first, but after calling the getLastDigit function, | ||
| // it will not return anything because every number based will be converted to string. | ||
|
|
||
| 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 | ||
| // Explain why the output is the way it is | ||
| // =============> write your explanation here | ||
| // =============> The last digit of 42 is 3 three times. | ||
| // the prediction was close but not accurate, the case is that the parameter num needs to be | ||
| //the 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)}`); | ||
| // This program should tell the user the last digit of each number. | ||
| // Explain why getLastDigit is not working properly - correct the problem | ||
| // parameter num needs to be with the getLastDigit function instead of const num=103. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,5 +15,8 @@ | |
| // It should return their Body Mass Index to 1 decimal place | ||
|
|
||
| function calculateBMI(weight, height) { | ||
| const bmi = weight / (weight*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. What type of value do you expect your function to return? A number or a string? Different types of values may appear identical in the console output, but they are represented and treated differently in the program. For example, console.log(123); // Output 123
console.log("123"); // Output 123
// Treated differently in the program
let sum1 = 123 + 100; // Evaluate to 223 -- a number
let sum 2 = "123" + 100; // Evaluate to "123100" -- a string.
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. I expect the function will return a number and it does return a number.
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. Can you look up how to check if a value is a number in JavaScript, and then apply the technique to verify your expectation? |
||
| // return the BMI of someone based off their weight and height | ||
| } | ||
| } | ||
| console.log("the BMI is", calculateBMI(70, 1.73)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
|
|
||
| function pad(num) { | ||
| return num.toString().padStart(2, "0"); | ||
| } | ||
|
|
@@ -10,25 +11,25 @@ function formatTimeDisplay(seconds) { | |
|
|
||
| return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; | ||
| } | ||
|
|
||
| console.log(formatTimeDisplay(61)); | ||
| // You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit | ||
| // to help you answer these questions | ||
|
|
||
| // Questions | ||
|
|
||
| // a) When formatTimeDisplay is called how many times will pad be called? | ||
| // =============> write your answer here | ||
| // =============> It will be called 3 times. | ||
|
|
||
| // 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 | ||
| // =============> 0 | ||
|
|
||
| // c) What is the return value of pad is called for the first time? | ||
| // =============> write your answer here | ||
| // =============> 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 | ||
| // =============> The leftover second is assigned to num as remainingSEconds 61%60 | ||
|
|
||
| // e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer | ||
| // =============> write your answer here | ||
| // =============> 01 the remainingSeconds from passed from pad to num is changed to string and padded as 01 | ||
Uh oh!
There was an error while loading. Please reload this page.