-
-
Notifications
You must be signed in to change notification settings - Fork 240
Manchester | 25-ITP-Sep | Khalidbih | Sprint 2 | coursework/sprint-2 #854
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
07075e1
7291d85
e838340
9bcb86b
b4435a7
ccf9b9b
3ae35f7
f5a273a
3960c56
e7be3d3
3557fef
41416d5
770c94a
f425e8a
b953519
f976015
2579613
7f0b538
e679e68
34ea901
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,21 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| //I think it will give an error because we have `str` twice in line 10 we should only have return without `str` | ||
|
|
||
| // 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 new code here | ||
| // The error happened because 'str' was declared twice, once as a parameter and once with 'let' inside the function. | ||
| // Also, when calling the function, using a word without quotes caused a ReferenceError. | ||
| // After removing the duplicate declaration and passing a string with quotes, the function works correctly, | ||
| // =============> write your new code here | ||
| function capitalise(str) { | ||
| return `${str[0].toUpperCase()}${str.slice(1)}`; | ||
| } | ||
| console.log(capitalise("Hard")); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,24 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // Why will an error occur when this program runs? | ||
| // =============> write your prediction here | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
| // =============> write your prediction here | ||
|
|
||
| function convertToPercentage(decimalNumber) { | ||
| const decimalNumber = 0.5; | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| // the error occur because the decimalNumber has already been declared. | ||
|
|
||
| // Try playing computer with the example to work out what is going on | ||
|
|
||
| return percentage; | ||
| } | ||
|
|
||
| console.log(decimalNumber); | ||
|
|
||
| // =============> write your explanation here | ||
|
|
||
| // I tried to run the code it gives me SyntaxError,that the decimalNumber has already been declared, we can see in the parameter already has name decimalNumber. | ||
| // thats why declaring it again inside the function, causes an errors. | ||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function convertToPercentage(decimalNumber){ | ||
| const percentage = `${decimalNumber * 100}%`; | ||
| return percentage | ||
| } | ||
| console.log (convertToPercentage(0.5)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,17 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // =============> write your prediction here | ||
|
|
||
| function multiply(a, b) { | ||
| console.log(a * b); | ||
| } | ||
|
|
||
| console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); | ||
| // in console.log we should just write (multiply(10,32) because writing in plain english will give us an error, | ||
| // and it won't give the output. | ||
|
|
||
| // =============> write your explanation here | ||
| // the code was broken because the console.log was inside the function it not a function,by using the return it gives the outupt | ||
|
|
||
|
|
||
| // Finally, correct the code to fix the problem | ||
| // =============> write your new code here | ||
| function multiply(a, b) { | ||
| return (a * b) | ||
| } | ||
|
|
||
| console.log(multiply(10, 32)); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,16 @@ | ||
| // Predict and explain first... | ||
| // =============> write your prediction here | ||
| // The return statement is not in the right place, so the function doesn’t return the value. | ||
|
|
||
| function sum(a, b) { | ||
| return; | ||
| a + b; | ||
| } | ||
| // =============> write your explanation here | ||
|
|
||
| console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
| // The return statement is stopping the function too early, so it doesn’t return a + b. | ||
|
|
||
| // =============> write your explanation here | ||
| // 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 |
|---|---|---|
|
|
@@ -4,3 +4,27 @@ | |
| // You will need to declare a function called toPounds with an appropriately named parameter. | ||
|
|
||
| // You should call this function a number of times to check it works for different inputs | ||
| function toPounds (penceString) { | ||
|
|
||
|
|
||
| const penceStringWithoutTrailingP = penceString.substring ( | ||
| 0, | ||
| penceString.length - 1 | ||
| ); | ||
|
|
||
| const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
| const pounds = paddedPenceNumberString.substring( | ||
| 0, | ||
| paddedPenceNumberString.length - 2 | ||
| ); | ||
|
|
||
| const pence = paddedPenceNumberString | ||
| .substring(paddedPenceNumberString.length - 2) | ||
| .padEnd(2, "0"); | ||
| return `£${pounds}.${pence}`; | ||
| } | ||
|
Comment on lines
+7
to
+25
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. The indentation of the code is a bit off. Suggestion: You can use this VSCode feature to auto indent JS code. |
||
| console.log (toPounds("399p")); | ||
| console.log (toPounds("400p")); | ||
| console.log (toPounds("243p")); | ||
| console.log (toPounds("9876p")); | ||
| console.log (toPounds("18p")); | ||
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.
Did you notice the variables
roundedandBMIare rendered in different colors?Many IDEs and viewers that support syntax highlighting (including GitHub) display identifiers in different formats and colors.
Can you look up the naming conventions in JavaScript? In particular,
Then, update the variable names according to those conventions.
On separate note, it is a good practice to declare a "variable" using
constinstead ofletwhen the variable is not going to be reassigned a value.