-
-
Notifications
You must be signed in to change notification settings - Fork 197
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?
Changes from 5 commits
395eced
d289941
2330e68
6c148b5
2cc6615
3909fbd
07f4d37
ca7399e
a1fc7bf
f9fdda6
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,30 @@ | ||
// Predict and explain first... | ||
// =============> write your prediction here | ||
// I am unable to predict the error right now. | ||
|
||
// call the function capitalise with a string input | ||
// interpret the error message and figure out why an error is occurring | ||
|
||
function capitalise(str) { | ||
/*function capitalise(str) { | ||
let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
return str; | ||
} | ||
|
||
output = capitalise("my name is adiyah"); | ||
console.log(output);*/ | ||
|
||
// =============> write your explanation here | ||
|
||
/* SyntaxError: Identifier 'str' has already been declared. | ||
This error occurs because the function parameter name and the variable name declared inside | ||
the function is same. This error can be fixed by either changing the parameter name or the variable name. | ||
*/ | ||
// =============> write your new code here | ||
|
||
function capitalize(str) { | ||
let inputStr = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
return inputStr; | ||
} | ||
|
||
let output = capitalize("my name is Adiyah"); | ||
console.log(output); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,30 @@ | ||
|
||
// Predict and explain first BEFORE you run any code... | ||
|
||
// this function should square any number but instead we're going to get an error | ||
|
||
// =============> write your prediction of the error here | ||
// The error occurs because we pass a number as a parameter instead of passing the variable | ||
// num which we return through the function. | ||
|
||
function square(3) { | ||
/*function square(3) { | ||
return num * num; | ||
} | ||
*/ | ||
|
||
// =============> write the error message here | ||
// SyntaxError: Unexpected number | ||
|
||
// =============> explain this error message here | ||
// It says the number pass through the parameter is not a valid argument | ||
// instead the variable must be used. | ||
|
||
// Finally, correct the code to fix the problem | ||
|
||
// =============> write your new code here | ||
|
||
function square(num) { | ||
return num * num; | ||
} | ||
|
||
let result = square(3); | ||
console.log(result); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,24 @@ | ||
// Predict and explain first... | ||
|
||
// =============> write your prediction here | ||
// In line 10 the function 'multiply(a, b)' called which doesn't return any value. | ||
|
||
function multiply(a, b) { | ||
/*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 should return a value for which we need to | ||
// replace the console.log() function with the return 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,22 @@ | ||
// Predict and explain first... | ||
// =============> write your prediction here | ||
// function doesn't return any value and print 'undefined' in console.log(). | ||
|
||
function sum(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 | ||
// The function first returns the value with is null and then the calculation takes place which doesn't return. | ||
|
||
// 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 |
---|---|---|
|
@@ -15,5 +15,12 @@ | |
// It should return their Body Mass Index to 1 decimal place | ||
|
||
function calculateBMI(weight, height) { | ||
// return the BMI of someone based off their weight and height | ||
} | ||
// return the BMI of someone based off their weight and height | ||
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 commentThe 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? 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 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. |
||
|
||
result = calculateBMI(80, 1.89); | ||
console.log(result); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,26 @@ | |
// 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 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 commentThe 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 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. Acknowledged. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,18 +17,18 @@ function formatTimeDisplay(seconds) { | |
// Questions | ||
|
||
// a) When formatTimeDisplay is called how many times will pad be called? | ||
// =============> write your answer here | ||
// 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 | ||
// 0 | ||
|
||
// 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 commentThe 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., 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. Acknowledged. I have updated the file by enclosing a value in the double quotes. |
||
|
||
// 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 | ||
// 1. num is assigned the value of remaining seconds. | ||
|
||
// 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 | ||
// 01. It padded the value to 2 digits. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,9 @@ | |
|
||
function formatAs12HourClock(time) { | ||
const hours = Number(time.slice(0, 2)); | ||
const minutes = time.slice(3, 5); | ||
if (hours > 12) { | ||
return `${hours - 12}:00 pm`; | ||
return `${hours - 12}:${minutes} pm`; | ||
} | ||
return `${time} am`; | ||
} | ||
|
@@ -23,3 +24,10 @@ console.assert( | |
currentOutput2 === targetOutput2, | ||
`current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
); | ||
|
||
const currentOutput3 = formatAs12HourClock("23:45"); | ||
const targetOutput3 = "11:45 pm"; | ||
console.assert( | ||
currentOutput3 === targetOutput3, | ||
`current output: ${currentOutput3}, target output: ${targetOutput3}` | ||
); | ||
Comment on lines
+31
to
+37
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. Can you add a few more test cases to test your function? In particulars, 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. 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. |
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.