diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a07..9c817d2439 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,21 @@ // Predict and explain first... // =============> write your prediction here +// I predict it will throw a SyntaxError because the variable 'str' has already being declared in the function parameter. And JavaScript will not allow it. // 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; } // =============> write your explanation here +/* it throws a SyntaxError: Identifier 'str' has already been declared because the function parameter 'str' is already a variable in the function scope. +The line `let str = ...` tries to create a new variable str in the same scope, which is not allowed in JavaScript. To fix this, we can either rename the parameter or the variable inside the function. */ + // =============> write your new code here +function capitalise(str) { + return str[0].toUpperCase() + str.slice(1); +} \ No newline at end of file diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f4..0e75dfeede 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -2,6 +2,9 @@ // Why will an error occur when this program runs? // =============> write your prediction here +/* I predict that it will throw a SyntaxError because decimalNumber is declared twice inside the same function, +and JvaScript does not allow redeclaring a variable with const in the same scope. +It will also throw a ReferenceError because decimalNumber is logged outside the function, where it is not defined. */ // Try playing computer with the example to work out what is going on @@ -15,6 +18,16 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); // =============> write your explanation here +/* The function receives decimalNumber as a parameter, so that variable already exists in the function scope. Declaring const decimalNumber = 0.5; +inside the function attempts to create a second variable with the same name, which causes a SyntaxError. Additionally, the console.log(decimalNumber) is outside the function, so JavaScript cannot +find a variable called decimalNumber in that scope, which causes a ReferenceError. */ // 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)); + diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cfe..f72f95e683 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -4,17 +4,27 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +/* I predict that it will throw a SyntaxError because the function parameter is written as 3, which is not a valid variable +name. Function parameters should be names (like num, x,) or any other valid identifier, not a number. */ function square(3) { return num * num; } + // =============> write the error message here +// SyntaxError: Unexpected number // =============> explain this error message here +/* The function is declared with 3 as its parameter, but JavaScript only allows variable names(identifiers) +in that position. Using a number causes a SyntaxError before the code is run. +Also, the function tries to t=return num * num, but num is not defined, which would cause a ReferenceError if the first error did not occur.*/ // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} - +console.log(square(25)); diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b417..cdb039bf2a 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,8 @@ // Predict and explain first... // =============> write your prediction here +/* The code will return "The result of multiplying 10 and 32 is undefined" because the function logs the result instead of returning it. +Multiply(10, 32) returns udefined, and that's what gets inserted into the template string. */ function multiply(a, b) { console.log(a * b); @@ -9,6 +11,15 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +/* Inside the function, console.log(a * b) prints the answer to the console, but the function does not return any value, so it returns undefined. +when we call multiply(10, 32) ,the function prints 320 to the console, but the return value of the function is undefined. +console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); inserts the returned value, not the logged value. Since the function returns undefined, +the final output becomes "The result of multiplying 10 and 32 is 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 37cedfbcfd..7117097e10 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,7 @@ // Predict and explain first... // =============> write your prediction here +/* Because the function has a return statement with nothing after it, the code will return: The sum of 10 and 32 is udefined. +The line a + b will never run because it comes after the return. */ function sum(a, b) { return; @@ -9,5 +11,14 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +/* When JavaScript sees a return statement, it immediately stops the function. So the function exits before it ever reaches a + b. +Because nothing is returned, JavaScript returns undefined. The template string uses the returned value. The line: console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); +inserts whatever the function returns. since the function returns undefined, the final output becomes: The sum of 10 and 32 is undefined. Even though a + b is written in the function, it returns 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)}`); \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc35..79b4eff043 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -2,6 +2,13 @@ // Predict the output of the following code: // =============> Write your prediction here +/* All the three lines will output the last digits of 103, which is "3". So the output will be: +The last digit of 42 is 3 +The last digit of 105 is 3 +The last digit of 806 is 3 + +This is because the function getLastDigit() does not use the number passed into it. +Instead, it is always uses the global variable 'num', which is 103. therefore, every call returns the last digit of 103. */ const num = 103; @@ -15,10 +22,45 @@ 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 output will be: +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 + +/* The function getLastDigit() is supposed to return the last digit of the number passed to it by the user. However, the function does not accept any parameters. +Even though the function is called with values like getLastDigit(42), getLastDigit(105), and getLastDigit(806), the argument is ignored because the function definition does not include a parameter. +Instead, the function always uses the global variable 'num', which is 103. +The last digit of 103 is "3", so every call returns "3" regardless of the number passed in. */ + + // Finally, correct the code to fix the problem // =============> write your new code here +function getLastDigit(number) { + return number.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 + +/* getLastDigit is not working properly because it does not accept any parameters in its definition. +As a result, it always uses the global variable 'num', which is set to 103, instead of using the number passed to it when called. +The correct code now includes a parameter 'number' in the function definition of getLastDigit(). +This allows the function to accept the number passed to it when called. + +Correct Output: +The last digit of 42 is 2 +The last digit of 105 is 5 +The last digit of 806 is 6 + */ \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1b..78d825aee7 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,8 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file + const bmi = (weight / height **2); + return bmi.toFixed(1); +} + +console.log(calculateBMI(90, 1.80)); \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad9..aeba1443a1 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,11 @@ // 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) { + return text.toUpperCase().replaceAll(" ", "_"); +} + +console.log(toUpperSnakeCase("hello there")); +console.log(toUpperSnakeCase("lord of the rings")); +console.log(toUpperSnakeCase("javascript is challenging and fun")); \ No newline at end of file diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a703..7916c23271 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,19 @@ // 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("5p")); +console.log(toPounds("99p")); +console.log(toPounds("250p")); \ No newline at end of file diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 17127bc01e..92fb7bf5ef 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -22,17 +22,22 @@ function formatTimeDisplay(seconds) { // 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 +// The value assigned is "1" because remainingSeconds was calculated as 61 % 60, which equals 1, and that is passed into pad last. // 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 pad always ensures te output is atleast 2 characters long, so 1 becomes "01". diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b8..3477d6ed9d 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -23,3 +23,64 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +// Midnight +console.assert( + formatAs12HourClock("00:00") === "12:00 am", + "00:00 should be 12:00 am" +); + +// Noon +console.assert( + formatAs12HourClock("12:00") === "12:00 pm", + "12:00 should be 12:00 pm" +); + +// Minutes included +console.assert( + formatAs12HourClock("14:45") === "2:45 pm", + "14:45 should be 2:45 pm" +); + +// Single-digit hour AM +console.assert( + formatAs12HourClock("09:30") === "9:30 am", + "09:30 should be 9:30 am" +); + +// Single-digit hour PM +console.assert( + formatAs12HourClock("13:05") === "1:05 pm", + "13:05 should be 1:05 pm" +); + +// Edge: 12:59 PM +console.assert( + formatAs12HourClock("12:59") === "12:59 pm", + "12:59 should be 12:59 pm" +); + +// Edge: 00:59 AM +console.assert( + formatAs12HourClock("00:59") === "12:59 am", + "00:59 should be 12:59 am" +); + +function formatAs12HourClock(time) { + const [hourStr, minuteStr] = time.split(":"); + let hours = Number(hourStr); + const minutes = minuteStr; + + let period = "am"; + + if (hours === 0) { + hours = 12; // midnight + } else if (hours === 12) { + period = "pm"; // noon + } else if (hours > 12) { + hours -= 12; + period = "pm"; + } + + return `${hours}:${minutes} ${period}`; +} \ No newline at end of file