diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..b231fac70 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,21 @@ // Predict and explain first... -// =============> write your prediction here +// function parameter and variable inside the function have same name. // call the function capitalise with a string input +console.log(capitalise("message")); // 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; -} +/*Identifier 'str' has already been declared . +This mean that 'str' has already been declared as a function parameter, +so we cannot declare another variable with the same name using 'let'. + */ // =============> write your explanation here +// The error occurs because 'str' is already declare as a function parameter. +//The 'let str' inside the function tries to declare 'str' again in the same scope. +//I changed the name of new variable to 'result' to fix the error. + // =============> write your new code here +function capitalise(str) { + let result = `${str[0].toUpperCase()}${str.slice(1)}`; + return result; +} diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..2d15e8022 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -2,19 +2,27 @@ // Why will an error occur when this program runs? // =============> write your prediction here - +// If we run the program, I predict we will get an error. +// The first problem is that 'decimalNumber' is already function parameter, but you try to declare another variable with the same name inside the function. +//another problem is that 'decimalNumber' is a local variable inside the function +// and we cannot access to it outside the function. +//------------------------------------------------------ // Try playing computer with the example to work out what is going on +//SyntaxError: Identifier 'decimalNumber' has already been declared + +// =============> write your explanation here + +// The error occurs because 'decimalNumber' has already been declared +// as a function parameter. we cannot declare it again with 'const' + +// Finally, correct the code to fix the problem +// =============> write your new code here +const decimalNumber = 0.5; function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; const percentage = `${decimalNumber * 100}%`; return percentage; } -console.log(decimalNumber); - -// =============> write your explanation here - -// Finally, correct the code to fix the problem -// =============> write your new code here +console.log(convertToPercentage(decimalNumber)); diff --git a/Sprint-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..15c5512b8 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -1,20 +1,27 @@ - // Predict and explain first BEFORE you run any code... +// when we run the code, I predict that we will got the error in function argument +//because it is not a variable // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// -function square(3) { +/*function square(3) { return num * num; -} +}*/ // =============> write the error message here +// SyntaxError: Unexpected number // =============> explain this error message here +// The error occurs because 3 is a number, not a valid variable name. // Finally, correct the code to fix the problem // =============> write your new code here - - +const num = 3; +function square(num) { + return num * num; +} +console.log(square(num)); diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..e3bc4d83d 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,14 +1,24 @@ // Predict and explain first... - // =============> write your prediction here +//when we run the code, I predict that the function just execute but doesn't return any thing. +// function just print the result of multiply +// I am not sure what going to be show in the second console.log -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)}`); +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);*/ // =============> write your explanation here +// After running the code, the multiply function display the result (320) +// but it does not return result back to the code +// therefore, multiply(10, 32) evaluate undefined // 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)}`); diff --git a/Sprint-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..00cbe2bc2 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,13 +1,21 @@ // Predict and explain first... -// =============> write your prediction here +// =============> I think the position of return inside the function is wrong +//probably it is show nothing -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 +// After running code it is show "The sum of 10 and 32 is undefined" +// because return has no value so it return undefined. + // 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)}`); diff --git a/Sprint-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..54fa3df0b 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -1,9 +1,11 @@ // Predict and explain first... +// In my idea we can not change value of num as it is defined by const // Predict the output of the following code: // =============> Write your prediction here +//perhaps we will get value definition error -const num = 103; +/*const num = 103; function getLastDigit() { return num.toString().slice(-1); @@ -11,14 +13,26 @@ function getLastDigit() { 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)}`); +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 +// I was wrong in all the function call, the result is the same because the function always uses the global num. + // Finally, correct the code to fix the problem -// =============> write your new code here + +function getLastDigit(num) { + return num.toString().slice(-1); +} + +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)}`); // This program should tell the user the last digit of each number. // Explain why getLastDigit is not working properly - correct the problem diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..f5c1ce957 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -15,5 +15,7 @@ // It should return a string of their Body Mass Index to 1 decimal place function calculateBMI(weight, height) { - // return the BMI of someone based off their weight and height + const BMI = weight / (height * height); + return BMI.toFixed(1); } +console.log(`your body mass is ${calculateBMI(87, 1.58)}`); diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..587eea086 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,13 @@ // 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 +function toUpperSnakeCase(text) { + const newText = text.replace(/ /g, "_"); + return newText.toUpperCase(); +} +console.log( + `The upper snake case of "hello there" is ${toUpperSnakeCase("hello there")}`, +); +console.log( + `The upper snake case of "lord of rings" is ${toUpperSnakeCase("lord of the rings")}`, +); diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..84b875e51 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,23 @@ // 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, + ); + 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(toPounds("399p")); +console.log(toPounds("2p")); +console.log(toPounds("4500p")); +console.log(toPounds("95p")); +console.log(toPounds("3675p")); diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..657377776 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -14,25 +14,28 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } - +console.log(formatTimeDisplay(61)); // You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit // to help you answer these questions // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> The pad function is called three times on line 15. // 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 value assigned to num is 0. // c) What is the return value of pad when it is called for the first time? -// =============> write your answer here +// =============> The value of pad when it is called for the first time "00". // 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 value assign to num is 1. +// because the last call to pad is pad(remainingSeconds), and remainingSeconds is 1 +// when formatTimeDisplay(61) is called. // e) What is the return value of pad when it is called for the last time in this program? Explain your answer -// =============> write your answer here +// =============> The return value is "01" because the last call to pad is pad(1). +// The pad function add a zero to make the number two digits. diff --git a/Sprint-3/5-stretch-extend/format-time.js b/Sprint-3/5-stretch-extend/format-time.js index 32a32e66b..7bc4b34a9 100644 --- a/Sprint-3/5-stretch-extend/format-time.js +++ b/Sprint-3/5-stretch-extend/format-time.js @@ -4,22 +4,56 @@ function formatAs12HourClock(time) { const hours = Number(time.slice(0, 2)); + if (hours === 0) { + return `12${time.slice(2)} am`; + } + if (hours === 12) { + return `12${time.slice(2)} pm`; + } if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${String(hours - 12).padStart(2, "0")}:${time.slice(3)} pm `; } return `${time} am`; } -const currentOutput = formatAs12HourClock("08:00"); -const targetOutput = "08:00 am"; +const currentOutput = formatAs12HourClock("08:30"); +const targetOutput = "08:30 am"; console.assert( currentOutput === targetOutput, - `current output: ${currentOutput}, target output: ${targetOutput}` + `current output: ${currentOutput}, target output: ${targetOutput}`, ); -const currentOutput2 = formatAs12HourClock("23:00"); -const targetOutput2 = "11:00 pm"; +const currentOutput2 = formatAs12HourClock("15:30"); +const targetOutput2 = "03:30 pm"; console.assert( currentOutput2 === targetOutput2, - `current output: ${currentOutput2}, target output: ${targetOutput2}` + `current output: ${currentOutput2}, target output: ${targetOutput2}`, ); + +const currentOutput5 = formatAs12HourClock("11:59"); +const targetOutput5 = "11:59 am"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}`, +); + +const currentOutput3 = formatAs12HourClock("00:00"); +const targetOutput3 = "12:00 am"; + +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}`, +); +const currentOutput4 = formatAs12HourClock("12:02"); +const targetOutput4 = "12:02 pm"; + +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}`, +); + +console.log(currentOutput, targetOutput); +console.log(currentOutput2, targetOutput2); +console.log(currentOutput3, targetOutput3); +console.log(currentOutput4, targetOutput4); +console.log(currentOutput5, targetOutput5);