-
-
Notifications
You must be signed in to change notification settings - Fork 195
LondonlMay-2025lping wanglCoursework/sprint 2 #642
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
0e93b52
202d64e
5d023b1
43cff66
b52edc9
9c51754
2803234
934f53b
1ccd377
43f590e
90208f2
c60427b
9ab417a
f396f5f
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,13 @@ | ||
// Predict and explain first... | ||
// =============> write your prediction here | ||
"hello Mhairi" === `hello ${mhairiName}`; | ||
"${mhairiName} is 28" === `Mhairi is ${mhairiAge}`; | ||
|
||
// call the function capitalise with a string input | ||
// interpret the error message and figure out why an error is occurring | ||
// My answer is: | ||
const mhairiName = "Mhairi"; | ||
const mhairiAge = 28; | ||
|
||
function capitalise(str) { | ||
let str = `${str[0].toUpperCase()}${str.slice(1)}`; | ||
return str; | ||
} | ||
const sentence1= `Hello ${mhairiName},nice to meet you`; | ||
console.log(sentence1); // ➜ Output: Hello Mhairi, nice to meet you | ||
|
||
// =============> write your explanation here | ||
// =============> write your new code here | ||
|
||
const sentence= `${mhairiName} is ${mhairiAge} years old`; | ||
console.log(sentence); // ➜ Output: Mhairi is 28 years old |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,23 @@ | ||
// Predict and explain first... | ||
|
||
// =============> write your prediction here | ||
// =============> console.log(a*b) is inside the function, so it does not return anything instead return undefined by default | ||
|
||
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 | ||
// =============> When we call multiply(10, 32) inside the template string, it first runs console.log(320) inside the function. | ||
// Then it tries to insert the function’s return value (which is undefined) into the string. | ||
// So, we’ll see:csharp | ||
// Copy | ||
// Edit | ||
// 320 | ||
Comment on lines
+14
to
+16
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. Did you mean to add this to the solution?
|
||
// The result of multiplying 10 and 32 is undefined | ||
|
||
// Finally, correct the code to fix the problem | ||
// =============> write your new code here | ||
// =============> my new code: | ||
function multiply(a,b) {return(a*b)}; | ||
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 the formatting be improved here? |
||
|
||
console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// Predict and explain first... | ||
// =============> write your prediction here | ||
// =============> it should be{return a+b;}; rather than have semicolon between return and a+b | ||
|
||
function sum(a, b) { | ||
return; | ||
|
@@ -8,6 +8,17 @@ function sum(a, b) { | |
|
||
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
||
// =============> write your explanation here | ||
// =============> The return; statement immediately exits the function and returns undefined. | ||
// The line a + b; after return; never runs — it's unreachable code.So, when we call sum(10, 32), it returns undefined. | ||
// Output we get: | ||
// python | ||
// Copy | ||
// Edit | ||
Comment on lines
+14
to
+16
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. Did you mean to commit this? |
||
// The sum of 10 and 32 is undefined | ||
// Finally, correct the code to fix the problem | ||
// =============> write your new code here | ||
// =============> my new code: | ||
function sum (a,b) {return a+b;}; | ||
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. Could the formatting be improved here? |
||
|
||
console.log (`The sum of 10 and 32 is ${sum(10, 32)}`); | ||
|
||
// the output: The sum of 10 and 32 is 42 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
// Predict and explain first... | ||
|
||
// Predict the output of the following code: | ||
// =============> Write your prediction here | ||
// =============> get rid of const num = 103 as it is defined in global scope. the output will be always 3. | ||
|
||
const num = 103; | ||
|
||
|
@@ -14,11 +14,27 @@ 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 | ||
// =============> The last digit of 42 is 3 | ||
//The last digit of 105 is 3 | ||
//The last digit of 806 is 3 | ||
// | ||
|
||
// Explain why the output is the way it is | ||
// =============> write your explanation here | ||
//num is always 103 inside the function (from the global variable). | ||
//So, every call to getLastDigit() returns "3" (the last digit of 103). | ||
//The arguments you pass (like 42, 105) are ignored because the function doesn't take parameters. | ||
|
||
|
||
// Finally, correct the code to fix the problem | ||
// =============> write your new code here | ||
// =============> my new code: | ||
function getLastDigit(num) {return num.toString().slice(-1);}; | ||
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. Could the formatting be improved here? |
||
|
||
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)}`); | ||
|
||
// The output will be: The last digit of 42 is 2 | ||
// The last digit of 105 is 5 | ||
// The last digit of 806 is 6 | ||
// This program should tell the user the last digit of each number. | ||
// Explain why getLastDigit is not working properly - correct the problem |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
// Then when we call this function with the weight and height | ||
// 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 | ||
} | ||
function calculateBMI(weight, height) { const bmi= weight/(height*height); return parseFloat(bmi.toFixed(1));} | ||
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. Could the formatting also be improved here? |
||
|
||
|
||
console.log(calculateBMI(58,1.50)); // output: 25.8 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,16 @@ | |
// You will need to come up with an appropriate name for the function | ||
// Use the MDN string documentation to help you find a solution | ||
// This might help https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase | ||
|
||
// The function to convert any string to UPPER_SNAKE_CASE: | ||
|
||
function toUpperSnakeCase(input) { | ||
return input.trim().toUpperCase().replace(/\s+/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. Could the formatting be improved here? |
||
|
||
console.log(toUpperSnakeCase("hello there")); // output:HELLO_THERE | ||
console.log(toUpperSnakeCase("lord of the rings")); // output: LORD_OF_THE_RINGS | ||
|
||
//trim() removes extra spaces at the beginning and end. | ||
//toUpperCase() makes everything uppercase. | ||
//replace(/\s+/g, "_") replaces all spaces (even multiple) with underscores. | ||
|
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 | ||
// the code is: | ||
function formatPenceToPounds(penceString) { | ||
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. Are there some edge cases you might want to consider? |
||
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}`; | ||
} | ||
|
||
console.log(formatPenceToPounds("7p")); // £0.07 | ||
console.log(formatPenceToPounds("67p")); // £0.67 | ||
console.log(formatPenceToPounds("153p")); // £1.53 | ||
console.log(formatPenceToPounds("3p")); // £0.03 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,41 @@ console.assert( | |
currentOutput2 === targetOutput2, | ||
`current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
); | ||
|
||
// i put this function for test in console, chrome but it showed undefined then i put 12:00 and the output is:Uncaught SyntaxError: | ||
// Unexpected token.so the function is not workable and has some bugs.Unfortunately, i can not work out by myself, i went to chatGpt which | ||
// explained the detail to me. function format12HourClock(time) does not work right because it does not work for 12:00, 24:00 d not for | ||
// minutes either. The right function should be: | ||
|
||
function formatAs12HourClock(time) { | ||
const hours24 = Number(time.slice(0, 2)); | ||
const minutes = time.slice(3); | ||
|
||
const period = hours24 >= 12 ? "pm" : "am"; | ||
const hours12 = hours24 % 12 === 0 ? 12 : hours24 % 12; | ||
|
||
return `${String(hours12).padStart(2, "0")}:${minutes} ${period}`; | ||
} | ||
|
||
const currentOutput = formatAs12HourClock("08:00"); | ||
const targetOutput = "08:00 am"; | ||
console.assert( | ||
currentOutput === targetOutput, | ||
`current output: ${currentOutput}, target output: ${targetOutput}` | ||
); | ||
Comment on lines
+44
to
+47
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. Do you think you could go further to convert this assertion function into a reusable function? |
||
|
||
const currentOutput2 = formatAs12HourClock("23:00"); | ||
const targetOutput2 = "11:00 pm"; | ||
console.assert( | ||
currentOutput2 === targetOutput2, | ||
`current output: ${currentOutput2}, target output: ${targetOutput2}` | ||
); | ||
|
||
// const period = hours24 >= 12 ? "pm" : "am";This line uses a ternary operator — a concise way to write an if...else statement. | ||
// If hours24 is greater than or equal to 12, then set period to "pm".Otherwise, set it to "am". | ||
|
||
// const hours12 = hours24 % 12 === 0 ? 12 : hours24 % 12;This is also a ternary operator (shortcut for if...else) | ||
// used to convert 24-hour time to 12-hour time format, which means:"If hours24 % 12 is 0, then set hours12 to 12, | ||
// otherwise set it to hours24 % 12." For me, i can understand now with chatgpt help. | ||
|
||
|
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 you also think the formatting can be improved here?