-
-
Notifications
You must be signed in to change notification settings - Fork 221
London | 25-ITP-Sep | Adnaan Abo | Sprint 2 | Coursework/sprint 2 #712
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 10 commits
e3b89c3
3d852fa
ce0868b
f775ea1
7a75478
15b01b6
4fa6e04
d8d8f84
0752aa5
faeb0ff
ebb51e5
345f9a2
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,29 @@ | ||
// Predict and explain first... | ||
// =============> write your prediction here | ||
// the code will throw an error because the variable str is being declared twice within the same scope | ||
|
||
// 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 | ||
// =============> write your new code here | ||
*/ | ||
|
||
// =============> write your explanation | ||
/* the code throws a SyntaxError because the variable 'str' is declared twice in the same scope using 'let'. In JavaScript, | ||
you cannot declare a variable with the same name more than once in the same scope. The first declaration of 'str' is as a | ||
function parameter, and the second declaration is within the function body. To fix this, you can either rename the second | ||
'str' variable or remove the 'let' keyword from the second declaration to reassign the value to the existing parameter variable.*/ | ||
|
||
// =============> write your new code here | ||
|
||
function capitalise(str) { | ||
str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
return str; | ||
} | ||
|
||
console.log(capitalise("hello")); // Output: "Hello" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,26 @@ | ||
// Predict and explain first... | ||
|
||
// =============> write your prediction here | ||
/* The function multiply does not return any value, it only logs the product of a and b to the console. | ||
Therefore, when we try to log the result of multiply(10, 32), it will log 'undefined' because the | ||
function does not return anything.*/ | ||
|
||
/* | ||
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 | ||
// The function multiply does not have a return statement, so it returns undefined by default. | ||
|
||
// 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,27 @@ | ||
// Predict and explain first... | ||
// =============> write your prediction here | ||
// An error will occur because the function 'sum' has a return statement that is not returning any value. | ||
|
||
/* | ||
function sum(a, b) { | ||
return; | ||
a + b; | ||
} | ||
|
||
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
*/ | ||
|
||
// =============> write your explanation here | ||
/* The error occurs because the 'sum' function has a return statement that does not return any value. In JavaScript, | ||
when a function reaches a return statement without a value, it returns 'undefined' by default. Therefore, when we | ||
call 'sum(10, 32)', it returns 'undefined', and the output will be "The sum of 10 and 32 is undefined". To fix this, | ||
we need to ensure that the function returns the result of 'a + b'.*/ | ||
|
||
// 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,28 @@ | |
// 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 // to remove the trailing 'p' | ||
); | ||
|
||
const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); | ||
const pounds = paddedPenceNumberString.substring( | ||
0, | ||
paddedPenceNumberString.length - 2 // all but the last two characters | ||
); | ||
|
||
const pence = paddedPenceNumberString | ||
.substring(paddedPenceNumberString.length - 2) // the last two characters | ||
.padEnd(2, "0"); // in case there is only one character of pence | ||
|
||
return `£${pounds}.${pence}`; | ||
} | ||
|
||
console.log(toPounds("399p")); // should log "£3.99" | ||
console.log(toPounds("5p")); // should log "£0.05" | ||
console.log(toPounds("89p")); // should log "£0.89" | ||
console.log(toPounds("123456p")); // should log "£1234.56" |
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.
The BMI calculation has to be to 1 decimal place, according to the specification. You're fixing the position in your log, after the function returns. Is that the best way to implement this?
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.
The best practice is letting the function return the BMI formatted to 1 decimal place, rather than having it fixed in the log.