Skip to content

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
20 changes: 10 additions & 10 deletions Sprint-2/1-key-errors/0.js
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
19 changes: 17 additions & 2 deletions Sprint-2/1-key-errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,22 @@ function convertToPercentage(decimalNumber) {

console.log(decimalNumber);

// =============> write your explanation here
// =============>const decimalNumber =0.5 is wrong because we can not redeclare parameter using const inside function.
// what is more, decimalNumber is not defined in the global scope so we can not use console.log(decimalNumber)


// 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)); // Output: "50%"

//or another concised code
function convertToPercentage(decimalNumber) {
return`${decimalNumber * 100}%`;
}

console.log(convertToPercentage(0.5)); // Output: "50%"
12 changes: 8 additions & 4 deletions Sprint-2/1-key-errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@

// this function should square any number but instead we're going to get an error

// =============> write your prediction of the error here
// =============> wrong syntax the number 3 should not be in function

function square(3) {
return num * num;
}

// =============> write the error message here
// =============> syntaxError: Unexpected number

// =============> explain this error message here
// =============> when defining a function, the parentheses must contain parameter names, not values.
// function square(3) is invalid because 3 is a value, not a parameter name.

// Finally, correct the code to fix the problem

// =============> write your new code here
// =============>
function square(num){return num*num};

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?


console.log (square(3));


15 changes: 12 additions & 3 deletions Sprint-2/2-mandatory-debug/0.js
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to add this to the solution?

csharp
//               Copy
//               Edit
//               320

// 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)};

Choose a reason for hiding this comment

The 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)}`);
17 changes: 14 additions & 3 deletions Sprint-2/2-mandatory-debug/1.js
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;
Expand All @@ -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

Choose a reason for hiding this comment

The 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;};

Choose a reason for hiding this comment

The 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
24 changes: 20 additions & 4 deletions Sprint-2/2-mandatory-debug/2.js
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;

Expand All @@ -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);};

Choose a reason for hiding this comment

The 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
7 changes: 4 additions & 3 deletions Sprint-2/3-mandatory-implement/1-bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));}

Choose a reason for hiding this comment

The 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
13 changes: 13 additions & 0 deletions Sprint-2/3-mandatory-implement/2-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, "_");}

Choose a reason for hiding this comment

The 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.

24 changes: 24 additions & 0 deletions Sprint-2/3-mandatory-implement/3-to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Choose a reason for hiding this comment

The 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
18 changes: 13 additions & 5 deletions Sprint-2/4-mandatory-interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,26 @@ function formatTimeDisplay(seconds) {
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?
// =============> write your answer here
// =============> pad is called 3 times — once each for hours, for minutes andvfor seconds.


// 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
// =============> The first call is for totalHours, and when you call formatTimeDisplay(61), the value of totalHours is:
//totalHours = (61 - 1) / 60 = 60 / 60 = 1, remainingMinutes = 1
//totalHours = totalMinutes - remainingMinutes = 1 - 1 = 0
// So, num = 0 for the first pad call.

// c) What is the return value of pad is called for the first time?
// =============> write your answer here
// =============> The return value is "00" because pad(0) results in "00" using .padStart(2, "0").

// 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
// =============> The last call is for remaining seconds. With input 61, remainingSeconds = 61 % 60 = 1, so num = 1 in the last call.

// 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
// =============> The return value is "01" – pad(1) returns "01" because it adds a leading zero to make it 2 characters.

// so final output should be "00:01:01"


38 changes: 38 additions & 0 deletions Sprint-2/5-stretch-extend/format-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The 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.