diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..784831243 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -10,4 +10,16 @@ function capitalise(str) { } // =============> write your explanation here + +// Looking at the function declaration, it shows that str is already a parameter and has already been declared. +// let cannot declare a new variable in the same scope. Therefore, a syntax error occurs. + + // =============> write your new code here + +// We need to remove let from the function declaration so we can modify the value of the existing str variable + +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..b9f7b68de 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -16,5 +16,25 @@ console.log(decimalNumber); // =============> write your explanation here +// In this function declaration, we can see that `decimalNumber` is already a parameter of the function. +// We cannot redeclare the same variable name in the same scope using `const` or `let`. + +// When I originally called `console.log(decimalNumber);` outside the function, +// It printed the value `0.5` because `decimalNumber` was declared in the global scope. + // Finally, correct the code to fix the problem // =============> write your new code here + +// Corrected Code: +// To fix the issue, we define the value of `decimalNumber` outside the function and pass it in. +// This ensures the variable exists in the global scope and avoids redeclaring it inside the function. +// We also change the console log to call the function properly. + +const decimalNumber = 0.5; + +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} + +console.log(convertToPercentage(decimalNumber)); // Output: "50%" diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..52283442c 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -11,10 +11,34 @@ function square(3) { // =============> write the error message here +Uncaught SyntaxError: Unexpected number + return num * num; + return num * num; + ^^^^^^ + +Uncaught SyntaxError: Illegal return statement + + // =============> explain this error message here +// The error states that there is an unexpected number. This means the parameter is invalid. +// We cannot use a number as a parameter name. Function parameters must be valid variable names. + +// In the return statement, we used 'num', but it was never defined. +// Since 'num' is not declared as a parameter, this causes a ReferenceError. + // Finally, correct the code to fix the problem -// =============> write your new code here +// To fix the code: +// We need to change the parameter from the number 3 to a proper variable name, such as 'num'. +// This allows the function 'square' to define and use 'num' correctly as the input. +// We also need to pass a value (like 3) to the function when calling it inside console.log, +// so it prints the correct result. +// =============> write your new code here + +function square(num) { + return num * num; +} +console.log(square(3)); // Output: 9 diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..ea83dd81a 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -10,5 +10,26 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here -// Finally, correct the code to fix the problem +// I can see that the function does not return a value; it only logs the result. +// Inside the function call multiply(10, 32), a = 10 and b = 32. +// It runs console.log(a * b), which simply logs 320 to the console but does not return anything. +// When a function doesn't explicitly return a value, it returns undefined by default. +// Therefore, the result of multiplying 10 and 32 is undefined when used inside the template string. +// The console output will be: +320 +The result of multiplying 10 and 32 is undefined + + // =============> write your new code here + +// The original function only logged the result using console.log and did not return a value. +// As a result, using it inside a template string would return 'undefined'. +// To fix this, we add a return statement so that the function returns the result of the multiplication. + +function multiply(a, b) { + return a * b; +} + +// Now the function returns the result, and it can be correctly used inside the template literal. + +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..5613a9e49 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -9,5 +9,20 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here + +// I can see that there is a semicolon after the function return. This tells JavaScript that the function stops there. Any line of codes after that will be ignored. + + // Finally, correct the code to fix the problem + +//We need to remove the semicolon so the function can continue to the following line. + + + // =============> 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-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..b4bf2180b 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -3,6 +3,9 @@ // Predict the output of the following code: // =============> Write your prediction here +// I can see from the function call that num is defined as a fixed number, which is 103. +// So the function num.toString() will use 103 for num always, no matter what number is entered as a parameter. + const num = 103; function getLastDigit() { @@ -14,11 +17,55 @@ 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 + +// Done. +// It does exactly what I expected. Whatever number you pass into getLastDigit as an argument, it still returns 3. + // =============> write the output here + +console.log(`The last digit of 42 is ${getLastDigit(42)}`); +The last digit of 42 is 3 + +console.log(`The last digit of 105 is ${getLastDigit(105)}`); +The last digit of 105 is 3 + +console.log(`The last digit of 806 is ${getLastDigit(806)}`); +The last digit of 806 is 3 + // Explain why the output is the way it is // =============> write your explanation here + +// The function getLastDigit does not accept any arguments. +// A global variable, num, is already set to 103. +// So the function always uses this value instead of the numbers passed in. +// Here's what happens: +103.toString() = "103" +"103".slice(-1) = "3" + + // Finally, correct the code to fix the problem + +// We need to assign a parameter to the function getLastDigit(), which we will call "number". +// Inside the function, it converts "number" to a string and gets the last character using .slice(-1). + // =============> write your new code here +function getLastDigit(number) { + return number.toString().slice(-1); +} + +console.log(`The last digit of 42 is ${getLastDigit(42)}`); // "2" +console.log(`The last digit of 105 is ${getLastDigit(105)}`); // "5" +console.log(`The last digit of 806 is ${getLastDigit(806)}`); // "6" + +//Output + +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 + +// The function getLastDigit doesn't accept any parameters; it works with the variable num, which is fixed as 103. +// No matter what number is entered into getLastDigit, it ignores it and returns the last digit of 103 every time. diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..d00ea2839 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -16,4 +16,44 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height -} \ No newline at end of file +} + +// Answer: + +// BMI is calculated as weight divided by height squared: +// BMI = weight / (height * height) + +// First, we create a variable to store the BMI value: +const bmi = weight / (height * height); + +// To format the result to 1 decimal place, we use the .toFixed(1) method. +// This converts the number to a string rounded to one decimal place: +// bmi.toFixed(1) + +// Since .toFixed() returns a string, we convert it back to a number +// using parseFloat so we can use the result in further arithmetic operations. + +// We can define the function as follows: + +function calculateBMI(weight, height) { + const bmi = weight / (height * height); + return parseFloat(bmi.toFixed(1)); +} + +// What we will enter: +function calculateBMI(70, 1.73) { + const bmi = 70 / (1.73 * 1.73); + return parseFloat(bmi.toFixed(1)); +} +console.log(`The BMI for a weight of 70kg and height of 1.73m is ${calculateBMI(70, 1.73)}`); + +// The results: + +// The BMI for a weight of 70kg and height of 1.73m is 23.4 + + +// Weight = 70 kg, height = 1.73 m. +// • BMI = 70 ÷ (1.73 × 1.73) = 70 ÷ 2.9929 ≈ 23.41 +// • Rounded to 1 decimal = 23.4 +// • The function returns 23.4 + diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..06f379df4 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,26 @@ // 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 + +// Answer: + +// Requirements: +// 1. Replace spaces with underscores (_) +// 2. Convert all letters to uppercase + +// We will call the function toUpperSnakeCase(). +// We'll use the .replace() method to replace all spaces with underscores. +// We'll use .toUpperCase() to convert all letters to uppercase. +// The /g flag in the regular expression ensures that all spaces in the string are matched and replaced, +// not just the first one. Without /g, only the first space would be replaced. + + +function toUpperSnakeCase(str) { + return str.replace(/ /g, '_').toUpperCase(); +} + +// Example usage: + +console.log(toUpperSnakeCase("hello there")); // Output: "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // Output: "LORD_OF_THE_RINGS" +console.log(toUpperSnakeCase("all upper case")); // Output: "ALL_UPPER_CASE" diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..7e8c556d6 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,36 @@ // 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")); // Output: £3.99 +console.log(toPounds("9p")); // Output: £0.09 +console.log(toPounds("75p")); // Output: £0.75 +console.log(toPounds("1234p")); // Output: £12.34 + + +... }console.log(toPounds("399p")); +£3.99 +undefined +> console.log(toPounds("9p")); +£0.09 +undefined +> console.log(toPounds("1234p")); +£12.34 +undefined +> diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..52d6b0f7b 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -19,16 +19,51 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}` +// pad is called three times +// pad(totalHours) +// pad(remainingMinutes) +// pad(remainingSeconds) + // 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: ${pad(totalHours)} +// totalHours = (1 - 1) / 60 = 0 / 60 = 0 +// So, pad(totalHours) becomes pad(0) +// Therefore, the value assigned to num is 0 + // c) What is the return value of pad is called for the first time? // =============> write your answer here +// pad(0) is called +// inside the function: +// num = 0 +// num.toString() = "0" +// "0".padStart(2, "0") means make the string at least two characters long. +// if it is shorter, add "0" at the beginning. +// Since "0" is one character long, a "0" is added → "00" +// So, the return value is "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 last call is for seconds: pad(remainingSeconds) +// remainingSeconds = 61 % 60 = 1, so the function call is pad(1) +// Inside this call: +// num = 1 +// num.toString() = "1" +// "1".padStart(2, "0") = "01" +// Therefore, the value assigned to num is 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 + +// remainingSeconds = 1 +// pad(1) converts 1 into the string "1" +// Then it pads it to two digits with "0" in front → "01" +// This makes sure time values are always two digits like "01", "09", "12", etc., which is standard in time formatting (HH:MM:SS). +// So, the return value is "01"