diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..d14945a73 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,18 @@ // Predict and explain first... -// =============> write your prediction here +// I guess that the error could be due to the having the same variable in parameter and in the function body. // call the function capitalise with a string input // 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; +// } + +// So, the error sounds "SyntaxError: Identifier 'str' has already been declared", it means that we can't declare the sane variable name twice +// =============> write your new code here function capitalise(str) { - let str = `${str[0].toUpperCase()}${str.slice(1)}`; - return str; + return `${str[0].toUpperCase()}${str.slice(1)}`; } -// =============> write your explanation here -// =============> write your new code here +console.log(capitalise("greetings")); // Output: "Greetings" diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..f04dbe14a 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,19 +2,29 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// I suppose the error could be: +// 1. The variable "decimalNumber" declared twice in the same scope (in the function's parameter and in the function's body) +// 2. We can't reassign a value if we use word "const" to declare a variaable +// 3. The variable "decimalNumber" isn't defined in the global scope, so when we try to use function console.log(decimalNumber) it will throw a ReferenceError. If we want to log the result of the function we have to call this function with some argument: console.log(convertToPercentage(0.5)) // Try playing computer with the example to work out what is going on -function convertToPercentage(decimalNumber) { - const decimalNumber = 0.5; - const percentage = `${decimalNumber * 100}%`; +// function convertToPercentage(decimalNumber) { +// const decimalNumber = 0.5; +// const percentage = `${decimalNumber * 100}%`; - return percentage; -} +// return percentage; +// } -console.log(decimalNumber); +// console.log(decimalNumber); // =============> write your explanation here +//So, when I run the code, I got "SyntaxError: Identifier 'decimalNumber' has already been declared" and that the same as my prediction above. I didn't think that actually we don't need to declare "decimalNumber" again inside the function, and I removed line "const decimalNumber = 0.5; to fix the problem. THen, I called the function with the argument 0.5 in console.log and this worked fine. // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage(decimalNumber) { + return `${decimalNumber * 100}%`; +} + +console.log(convertToPercentage(0.5)); diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..c14baed38 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,25 @@ - // 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 +//I think that's an error because there is no declared variable "num". -function square(3) { - return num * num; -} +// function square(3) { +// return num * num; +// } // =============> write the error message here +// SyntaxError: Unexpected number // =============> explain this error message here +// As I can see now after running the code, I missed the very first error - we can't use a number as a parameter name, and JavaScript even didn't go after the very first mistake to check others errors (and my predicted error about undeclared "num") // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} - +console.log(square(12)); // 144 diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..e4e2d0f95 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,14 +1,18 @@ // Predict and explain first... -// =============> write your prediction here +// =============> I think the result will be undefined because in the function there is no return statement. -function multiply(a, b) { - console.log(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 +// =============> Its interesting - function log the result but doesn't return it. So when we see the output 320 and "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-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..6268d81b7 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,13 +1,17 @@ // Predict and explain first... -// =============> write your prediction here +// =============> I guess that the output will be undefined because there is a semicolon after the "return" and this breaks the line of code, and after return statement nothing can be returned +// function sum(a, b) { +// return; +// a + b; +// } + +// console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); + +// =============> The output is "The sum of 10 and 32 is undefined" as I predicted above +// Finally, correct the code to fix the problem function sum(a, b) { - return; - a + b; + return a + b; } console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); - -// =============> write your explanation here -// Finally, correct the code to fix the problem -// =============> write your new code here diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..552355a89 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,24 +1,43 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> The output will be: +// 1. The last digit of 42 is 3 +// 2. The last digit of 105 is 3 +// 3. The last digit of 806 is 3 +// Why? Because we don't pass any parameter to the function ant it always uses the variable "num", which defined in the global scope, so function has access to it and the last digit of 103 is 3. -const num = 103; +// const num = 103; -function getLastDigit() { - return num.toString().slice(-1); -} +// function getLastDigit() { +// 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)}`); +// 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)}`); // 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 +// =============> Because we use the global variable "num" in the function instead of passing the parameter to the function. // 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. +// +// Now output looks correctly: +// The last digit of 42 is 2 +// The last digit of 105 is 5 +// The last digit of 806 is 6 +// // Explain why getLastDigit is not working properly - correct the problem +// Actually, maybe its not the good idea to use the same name "num" for different variables in different scopes, because it can be confusing. It works correctly but next time I'll use for the global scope some more meaningful name. diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..782c3738d 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,10 @@ // 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 -} \ No newline at end of file + bmi = Number((weight / height ** 2).toFixed(1)); + return bmi; +} + +console.log(calculateBMI(70, 1.73)); // 23.4 +console.log(calculateBMI(120, 1.82)); // 36.2 +console.log(calculateBMI(53, 1.68)); // 18.8 diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..a712785e8 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,12 @@ // 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(str) { + transformedStr = str.toUpperCase().replaceAll(" ", "_"); + return transformedStr; +} + +console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS" +console.log(toUpperSnakeCase("code your future is great")); // "CODE_YOUR_FUTURE_IS_GREAT" diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..badeb6db8 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -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 toPounds(penceStr) { + const penceStrWithoutTrailingP = penceStr.substring(0, penceStr.length - 1); + const paddedPenceNumberString = penceStrWithoutTrailingP.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("123p")); // £1.23 +console.log(toPounds("1002p")); // £10.02 +console.log(toPounds("0p")); // £0.00 +console.log(toPounds("99p")); // £0.99 +console.log(toPounds("250p")); // £2.50 +console.log(toPounds("7p")); // £0.07 +console.log(toPounds("48p")); // £0.48 +console.log(toPounds("53647868p")); // £536478.68 diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..5df63562a 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -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" // 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". The last time pad works on remainingSeconds which is "1" if the input (remainingSeconds) is 61: remainingSeconds = seconds % 60, modulo operator gives the remainder of the division of seconds by 60, so 61 % 60 = 1. // 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". THe pad(1) is called, converting 1 to string and transform it to "01". diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..32d686a3e 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -3,11 +3,16 @@ // Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find. function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; - } - return `${time} am`; + const hours24 = Number(time.slice(0, 2)); + const minutes = time.slice(3).padStart(2, "0"); + const timeSuffix = hours24 >= 12 ? "pm" : "am"; + + let hours12 = hours24 % 12; + if (hours12 === 0) hours12 = 12; + + const formattedHours12 = String(hours12).padStart(2, "0"); + + return `${formattedHours12}:${minutes} ${timeSuffix}`; } const currentOutput = formatAs12HourClock("08:00"); @@ -23,3 +28,26 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +const currentOutput3 = formatAs12HourClock("00:00"); +const targetOutput3 = "12:00 am"; +console.assert( + currentOutput3 === targetOutput3, + `current output: ${currentOutput3}, target output: ${targetOutput3}` +); + +const currentOutput4 = formatAs12HourClock("12:00"); +const targetOutput4 = "12:00 pm"; +console.assert( + currentOutput4 === targetOutput4, + `current output: ${currentOutput4}, target output: ${targetOutput4}` +); + +const currentOutput5 = formatAs12HourClock("19:12"); +const targetOutput5 = "07:12 pm"; +console.assert( + currentOutput5 === targetOutput5, + `current output: ${currentOutput5}, target output: ${targetOutput5}` +); + +// I found that when I added minutes, the output became incorrect because the original function only changed the hours. I fixed this bug by introducing a new variable called minutes and using it as the second part of the output after the hours. I also added some formatting for the minutes and defined the period of time (am/pm) in a variable called timeSuffix to make the output display correctly.