diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..2c0ec877e 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,5 @@ // Predict and explain first... -// =============> write your prediction here +// =============> Prediction: The function capitalise is possibly meant to capitalise the first letter in a string. // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -9,5 +9,10 @@ function capitalise(str) { return str; } -// =============> write your explanation here +// =============> =============> write your explanation here +// The 'str' variable on the left hand side is the same used in the function name implementation which should not be. // =============> write your new code here +function capitalise(str) { + let capFunction = `${str[0].toUpperCase()}${str.slice(1)}`; + return capFunction; +} \ 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 f2d56151f..fef6a3cad 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,7 +1,7 @@ // Predict and explain first... // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> I suspect than an error will occur // Try playing computer with the example to work out what is going on @@ -14,7 +14,10 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here - +// =============> write your explanation here. The functions takes a decimal number as input and converts it to percentage by multiplying by 100 // Finally, correct the code to fix the problem // =============> write your new code here +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} \ No newline at end of file diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..f5021c8a5 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -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 +// =============> write your prediction of the error here. The error is likely to be caused by square(3), '3' is supposed to be replaced by a string representing the number. function square(3) { return num * num; } // =============> write the error message here - +// function square(3) { +// ^ +// SyntaxError: Unexpected number // =============> explain this error message here - +// The muber '3' should be used when calling the function and it should not be used as a parameter. // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..451ba98c5 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,6 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here. It may likely have issues as console.log() is within function implementation, instead of using'return' function multiply(a, b) { console.log(a * b); @@ -8,7 +8,10 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here. Instead of having console.log() is within function implementation, it should be replaced with 'return' // Finally, correct the code to fix the problem // =============> write your new code here +function multiply(a, b) { + return (a * b); +} \ No newline at end of file diff --git a/Sprint-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..9c8926d7a 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,6 @@ // Predict and explain first... -// =============> write your prediction here +// =============> write your prediction here. An error will result becasue of the ';' between return and a + b + function sum(a, b) { return; @@ -8,6 +9,9 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> write your explanation here. The ideal statement should be 'return a + b; ' but this statement is separated by ';' // Finally, correct the code to fix the problem // =============> write your new code here +function sum(a, b) { + return a + b; +} \ 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 57d3f5dc3..735bb2e6d 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,10 @@ // Predict and explain first... + // Predict the output of the following code: -// =============> Write your prediction here +// =============> Write your prediction here. +// //ANS: To return the last digit of num as a string. + const num = 103; @@ -15,10 +18,35 @@ 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 +// =============> write your explanation here. +// //ANS: +// num is a constant variable, a hardcoded reference. +//The function getLastDigit is currently ignoring the arguments you pass to it(42, 105, 806) because of two main issues: + +//Missing Parameter: The function definition function getLastDigit() does not have a parameter inside the parentheses.In JavaScript, if you pass an argument to a function that hasn't defined a parameter to receive it, the function simply ignores that input. + +//Scope and Variable Reference: Inside the function, you are explicitly calling num.toString().Since num isn't defined inside the function, the engine looks at the Global Scope, finds const num = 103, and uses that value every single time the function runs. + // Finally, correct the code to fix the problem // =============> write your new code here +const num = 103; // This is now ignored by the function + +// We add 'n' as a parameter to "catch" the numbers we pass in +function getLastDigit(n) { + // We call toString() on 'n' instead of the global 'num' + return n.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 +//ANS: Done above. +// ANS:getLastDigit was not working properly because the value for num is fixed at '103' + diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..b598100a6 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,13 @@ // 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 = kg / m^2 + const bmi = weight / (height * height); + + // .toFixed(1) rounds to 1 decimal place and returns a string + return bmi.toFixed(1); +} + +// Example usage: +const myBMI = calculateBMI(70, 1.73); +console.log(`The calculated BMI is: ${myBMI}`); // Output: "23.4" \ 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 5b0ef77ad..2e2a55a6a 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,15 @@ // 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) { + // 1. replaceAll(' ', '_') swaps every space for an underscore + // 2. toUpperCase() converts the letters to all caps + return text.replaceAll(' ', '_').toUpperCase(); +} + +// Examples: +console.log(toUpperSnakeCase("hello there")); // "HELLO_THERE" +console.log(toUpperSnakeCase("lord of the rings")); // "LORD_OF_THE_RINGS" +console.log(toUpperSnakeCase("javascript is fun")); // "JAVASCRIPT_IS_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 6265a1a70..075762f98 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,40 @@ // 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 + +/** + * Converts a pence string (e.g., "399p") into a formatted pound string (e.g., "£3.99") + * @param {string} penceString - The input string ending in 'p' + */ +function toPounds(penceString) { + // 1. Remove the trailing 'p' + const penceStringWithoutTrailingP = penceString.substring( + 0, + penceString.length - 1 + ); + + // 2. Pad with leading zeros to ensure at least 3 characters (e.g., "5p" -> "005") + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + + // 3. Extract the pounds (everything except the last two digits) + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + ); + + // 4. Extract the pence (the last two digits) + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + + // 5. Return the formatted string + return `£${pounds}.${pence}`; +} + +// --- Testing the function with different inputs --- + +console.log(toPounds("399p")); // Expected: £3.99 +console.log(toPounds("50p")); // Expected: £0.50 +console.log(toPounds("1250p")); // Expected: £12.50 +console.log(toPounds("8p")); // Expected: £0.08 +console.log(toPounds("1002p")); // Expected: £10.02 \ 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 7c98eb0e8..a8b118bca 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -17,18 +17,23 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> write your answer here. +// //ANS: 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 +// =============> write your answer here. +// ANS: the value assigned to num when pad is called for the first time is 0 // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> write your answer here. +// //ANS: the return value of pad is called for the first time 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 +// =============> write your answer here. +// ANS: the value assigned to num when pad is called for the last time 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 +// =============> write your answer here. +// ANS: Sthe return value assigned to num when pad is called for the last time in this program is '01'