-
-
Notifications
You must be signed in to change notification settings - Fork 195
Glasgow | May-2025 | Adiyah Farhan | Sprint-2 #515
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?
Glasgow | May-2025 | Adiyah Farhan | Sprint-2 #515
Conversation
Sprint-2/1-key-errors/1.js
Outdated
|
||
const decimalNumber = 0.5; | ||
|
||
function convertToPercentage() { | ||
const percentage = `${decimalNumber * 100}%`; | ||
|
||
return percentage; | ||
} | ||
|
||
result = convertToPercentage(); | ||
console.log(result); |
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.
Could you introduce a parameter to the function so that we can reuse the function's code with different numbers.
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.
I have modified the file by adding a parameter to the function to enhance its reusability.
const heightSquare = height * height; | ||
const BMI = weight / heightSquare; | ||
const scaledBMI = BMI.toFixed(1); | ||
return scaledBMI; | ||
} |
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.
What type of value do you expect the function to return? A number or a string?
Does your function return the type of value you expect it to return?
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 function returns the String value while we expect it to return a Number. I have updated my function by explicitly converting the type of return value of the function to Number.
function convertToPounds(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"); | ||
|
||
const convertAmount = "£" + pounds + "." + pence; | ||
console.log(convertAmount); | ||
} | ||
|
||
convertToPounds("399p"); | ||
convertToPounds("450p"); |
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.
When we designed a function to print the result instead of returning it, we limit how the result can be used.
For example, we cannot show the result in a dialog box via alert( convertToPounds("1234p"))
or save the result to a file.
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.
Acknowledged.
I have updated the function so that it returns the convertAmount instead of printing it.
|
||
// c) What is the return value of pad is called for the first time? | ||
// =============> write your answer here | ||
// 00 |
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.
We can enclose a value in double quotes to indicate that it is a string, e.g., "00"
.
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.
Acknowledged. I have updated the file by enclosing a value in the double quotes.
|
||
const currentOutput3 = formatAs12HourClock("23:45"); | ||
const targetOutput3 = "11:45 pm"; | ||
console.assert( | ||
currentOutput3 === targetOutput3, | ||
`current output: ${currentOutput3}, target output: ${targetOutput3}` | ||
); |
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.
Can you add a few more test cases to test your function? In particulars, "00:59"
, and "12:34"
.
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.
Two more test cases ("00:59" and "12:34") added to test the function. The function has been updated in order to pass all tests.
…type of the return value.
…t cases "00:59" and "12:34"
…es to indicate that it is a string
Thank you for taking the time to review this @cjyuan . I have updated the files and incorporated the suggested changes in it. Please have a look. |
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.
Changes look good. Well done.
const decimalNumber = 0.5; | ||
|
||
function convertToPercentage() { | ||
function convertToPercentage(decimalNumber) { |
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.
Note: You could also keep the original global variable decimalNumber
should you need to, even if it has the same name as the parameter. If you are interested in know why, you can look up "How JS resolve conflicting name".
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.
Thank you for this valuable information.
Learners, PR Template
Self checklist
Changelist
Files in the folder 1-key-errors have been updated with explanation and all errors have been resolved.
Files in the folder 2-mandatory-debug have been updated with corrected code.
Files in the folder 3-mandatory-implement have been updated with the required codes as per given task
The file in the folder 4-mandatory-interpret has been updated with all questions answered
The file in the 5-stretch-extend folder has been updated to include a new test using a different number of minutes as input.
Questions
Not yet.