-
-
Notifications
You must be signed in to change notification settings - Fork 397
Glasgow | 26-ITP-May | Christelle Boten | Sprint 2 | Coursework #1583
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
a7f08b1
3ae31b1
5e2ff7a
45d77d3
5ad1764
9bfb4c9
685482a
b414784
ec1013e
76784cd
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,18 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> write your prediction here: The function is capitalise is supposed to the take the first letter (at index 0) of the word, then turn it to a capital letter. And the slice (1) is supposed to delete the first letter at index 0, of a word and write the remaining letters. Then combine the first capital letter with the rest of the word which stayed the same except for the first letter which was removed with the slice method. | ||
|
|
||
| // 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; | ||
| // } | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your explanation here: There is an error in line 8 because we are using 'str' to declare a new variable meanwhile this name has previously been used. WE should get a new name. | ||
| // =============> write your new code here | ||
| function capitalise(str) { | ||
| let strOne = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| return console.log(strOne); | ||
| } | ||
| capitalise("manhood"); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,29 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // The function is trying to convert a decimal number into a percentage. | ||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
| // =============> write your prediction here: in the function, on line 9, the variable name 'decimalNumber' has already been used on line 8, so we need a new name or else, we will get a referencing error. ON line 15, the 'decimalNumber' cannot be printed because it is a local variable which exists only inside the function 'convertToPercentage' and cannot be access when outside of this function. | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| // function convertToPercentage(decimalNumber) { | ||
| // const decimalNumber = 0.5; | ||
| // const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
| // return percentage; | ||
| // } | ||
|
|
||
| console.log(decimalNumber); | ||
| // console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
| // =============> write your explanation here: When running the code, I get an error on line 9 for because I used the variable name decimalNumber a 2nd time. Then there is a reference error on line 15 because 'decimalNumber' cannot be accessed when outside of the function. Also, we do not really need the code on line 9. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function convertToPercentage(decimalNumber) { | ||
| const firstDecimal = 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. Does this firstDecimal constant do anything in this function? |
||
| const percentage = `${decimalNumber * 100}%`; | ||
|
|
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(convertToPercentage(0.1)); | ||
| console.log(convertToPercentage(0.5)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,24 @@ | ||
|
|
||
| // Predict and explain first BEFORE you run any code... | ||
|
|
||
| // THere will be an error on line 8 because we used a value (number 3) instead of a parameter. Functions are designed to be reusable. | ||
| // 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 get an error because we used a value (3) instead of a parameter. | ||
|
|
||
| function square(3) { | ||
| return num * num; | ||
| } | ||
| // function square(3) { | ||
| // return num * num; | ||
| // } | ||
|
|
||
| // =============> write the error message here | ||
| // =============> write the error message here: The error says that "unexpected number ". And it comes from line 8. | ||
|
|
||
| // =============> explain this error message here | ||
| // =============> explain this error message here: We are suppose to use a parameter instead of a hard value. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
|
|
||
| // =============> write your new code here | ||
|
|
||
| function square(num) { | ||
| return num * num; | ||
| } | ||
| console.log(square(3)); | ||
| console.log(square(12)); | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,19 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
| // =============> write your prediction here: The function console.log on line 6 doesn't return any value. It's only function is to print out. | ||
|
|
||
| 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 | ||
| // =============> write your explanation here: Because we didn't have a return statement for our function 'multiply', we got an undefined value. | ||
|
|
||
| // 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,19 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // =============> write your prediction here: Line 5 and line 6 are not connected so as they are separated by ';', there is nothing that the function is returning. | ||
|
|
||
| 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 | ||
| // =============> write your explanation here: We have an undefined value for the sum of a and b because we are not returning anything. | ||
| // 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,34 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Predict the output of the following code: | ||
| // =============> Write your prediction here | ||
| // =============> Our function doesn't take a parameter. So, it is not reusable and will always consider num = 103 . The num.toString() method converts everything into a String. Then the slice(-1) extracts the last figure of the num and give it back to us. | ||
|
|
||
| // 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 output is the same as my prediction | ||
| // Explain why the output is the way it is | ||
| // =============> The function isn't reusable because we didn't add a parameter to the function getLastDigit(). So, it will always take num = 103 because that is what line 9 does. | ||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| 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 | ||
| // Because our function doesn't take a parameter, line 9 is actually calling the global variable which is const num = 103. That is why all out return output will be 3. That is why I allocated a parameter to our function, though I gave it the name 'num'. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,4 +16,10 @@ | |
|
|
||
| function calculateBMI(weight, height) { | ||
| // return the BMI of someone based off their weight and height | ||
| } | ||
| const heightSquare = height * height; | ||
| const newBMI = weight / heightSquare; | ||
| const toOneDecimal = Math.round(newBMI * 10)/10; | ||
|
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 isn't wrong, but can you think of a cleaner solution that multiplying then dividing by 10? |
||
| return toOneDecimal | ||
| } | ||
|
|
||
| console.log(calculateBMI(70, 1.73)); | ||
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.
Do we want to print out or return a value here?