-
-
Notifications
You must be signed in to change notification settings - Fork 337
London | 26-ITP-January | Khaliun Baatarkhuu | Sprint 2 | Coursework. #1143
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
62dc90f
1eea298
02c8367
a10df18
ad1ca7f
f9e4e19
b7fb44c
9c6d604
4366cc4
d46a3dd
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 | ||
| // =============> write your prediction here: We'll get an error message because "str" paramater has been declared already, | ||
| //using it again inside the function will clash due to redaclaration. | ||
|
|
||
| // 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; | ||
| //} | ||
|
|
||
| // =============> write your explanation here: Identifier 'str' has already been declared, | ||
| //redaclaration in the same scope throws up the error message | ||
| // =============> write your new code here: | ||
| function capitalise(str) { | ||
| let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return str; | ||
| let newStr = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return newStr; | ||
| } | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your new code here | ||
| //Above code works because we have used another variable "newStr", avoiding redeclaration. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,47 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // =============> write your prediction here: potential error, decimalNumber has already been declared, can't redaclare it again with a new variable in the same scope. | ||
| //another error could be, console.log will try to access a local variable from the global scope. | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| // step one: function convertToPercentage(decimalNumber) | ||
| // function created with a parameter named (decimalNumber) | ||
| // step two: const decimalNumber = 0.5; | ||
| // error occurs because decimalNumber has been declared already in the fucntion, cannot declare another one with the same name , in the same scope. | ||
| // program stops. | ||
| // we have fixed the first error. Code runs again. | ||
| // step three: const percentage = `${decimalNumber * 100}%`; | ||
| // new variable called percentage created, then the JS code is inserted, where decimalNumber is multiplied by 100. | ||
| // no errors here | ||
| // step four: return percentage; | ||
| // this line sends the value out of the function then stops the function. No errors so far. | ||
| // Step five: console.log(decimalNumber); | ||
| // error occurs again, because the value created in step four can't be stored in console.log, due to decimalNumber being a parameter inside the function. Outside the function | ||
| // there is no variable named decimalNumber. | ||
| // we have fixed console.log with (convertToPercentage(0.5), now the result will print properly. | ||
|
|
||
|
|
||
|
|
||
| //function convertToPercentage(decimalNumber) { | ||
| // const decimalNumber = 0.5; | ||
| // const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| // return percentage; | ||
| //} | ||
|
|
||
| //console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here: my error predictions turned out to be true and i have played computer with js code to further illustrate my reasoning. | ||
|
|
||
| // 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; | ||
|
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. This is good. But do you think there could be another way to do this? One that doesn't require you to declare any variables at all?
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. function convertToPercentage(decimalNumber) { console.log(convertToPercentage(0.5)); 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. Yes similar to before 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. Make sure to retain the string templating |
||
| } | ||
|
|
||
| console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
| console.log(convertToPercentage(0.5)); | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,23 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // =============> write your prediction here: there will be an error, due to console.log being used incorrectly. Console.log is for displaying but there is no return command to | ||
| //clarify what to send to console.log | ||
|
|
||
| //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: I have run the code on node.js and got 'undefined' as an error message. I shall add 'return' instead of the first 'console.log' | ||
| //so that the result can be displayed properly in the second console.log. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here: | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| return a * b; | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,22 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> write your prediction here: js is a sequential language, as in reads code line by line, so i predict nothing will happen after 'return' is processed and | ||
| //some sort of an error message will appear. | ||
|
|
||
| //function sum(a, b) { | ||
| // return; | ||
| // a + b; | ||
| //} | ||
|
|
||
| //console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
|
||
| // =============> write your explanation here: 'undefined' error appeared. Fixed the code by adding the addition on the same line as return. | ||
|
|
||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here: | ||
|
|
||
| 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 | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,38 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| // Predict the output of the following code | ||
| // =============> Write your prediction here: i think, the console.log will only show '3' because the paramater hasn't been specified in the function. | ||
|
|
||
| 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 | ||
| // =============> write the output here: I've run the code and got '3' in each console.log line. | ||
|
|
||
| // Explain why the output is the way it is | ||
| // =============> write your explanation here: It's because there is no parameter in the function to differentiate viarable 103 from 42, 105, 806. | ||
|
|
||
|
|
||
| // 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 | ||
|
|
||
| // it works now, but since cosnt num = 103 is unused, i have removed it. | ||
|
|
||
| // 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 |
|---|---|---|
|
|
@@ -14,6 +14,12 @@ | |
| // Then when we call this function with the weight and height | ||
| // It should return their Body Mass Index to 1 decimal place | ||
|
|
||
| function calculateBMI(weight, height) { | ||
| function calculateBMI(weight, height) { | ||
| const bmi = weight / (height * height); | ||
|
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. There's a division here. Do you think we need to put in any specific safety guards?
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. We should guard against height being 0 so we could add the following guard: if (height <= 0) {
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. @ykamal I think i have fixed all three problems you have raised, could you please have another look? 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. Less than 0 is still fine. It's 0 that's the problem.
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. function calculateBMI(weight, height) { } @ykamal i have changed the guards for 0 specifically 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. Great, thanks. Ah, it's a body weight thing, so less than zero would be a problem 😂😂 but code wise this is fine |
||
| return Number(bmi.toFixed(1)); | ||
| } | ||
|
|
||
| console.log(calculateBMI(88, 1.76)); | ||
|
|
||
| // return the BMI of someone based off their weight and height | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,29 @@ | ||
| function pad(num) { | ||
| return num.toString().padStart(2, "0"); | ||
| return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; | ||
| } | ||
|
|
||
| function formatTimeDisplay(seconds) { | ||
| const remainingSeconds = seconds % 60; | ||
| const totalMinutes = (seconds - remainingSeconds) / 60; | ||
| const remainingMinutes = totalMinutes % 60; | ||
| const totalHours = (totalMinutes - remainingMinutes) / 60; | ||
| console.log(formatTimeDisplay(61)); | ||
| console.log(formatTimeDisplay(6671)); | ||
| console.log(formatTimeDisplay(832)); | ||
|
|
||
| return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; | ||
| } | ||
|
|
||
| // 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 | ||
| // =============> write your answer here: pad 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 | ||
| // =============> write your answer here: The value is 0. | ||
|
|
||
| // c) What is the return value of pad is called for the first time? | ||
| // =============> write your answer here | ||
| // =============> write your answer here: The 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 value is 1 because 61 % 60 leaves 1 second remaining. | ||
|
|
||
| // 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 | ||
| // =============> write your answer here: The return value is '01' because return num.toString().padstart(2, '0') formats 1 into '01'. |
Uh oh!
There was an error while loading. Please reload this page.
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.
This is good. But do you think there could be another way to do this? One that doesn't require you to declare any variables at all?
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.
function capitalise(str) {
return str[0].toUpperCase() + str.slice(1);
}
Could this work? Just by removing the unnecessary variable?
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.
Yes. Less memory, simpler. Well done