diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..eb25856af 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 +// =============> This code will give an error message // 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 new code here +// =============> it gave an error because the variable str is being declared twice one with the function and another one with anew variable +// =============> this can be fixed by changing the variable name inside the function +function capitalise(str) { + let capitalised = `${str[0].toUpperCase()}${str.slice(1)}`; + return capitalised; +} +console.log(capitalise("sophia")); // Output: "Sophia" \ 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..b6e7d7382 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 can see that the variable decimalNumber is being declared twice once as a function parameter and again as a constant // Try playing computer with the example to work out what is going on @@ -14,7 +14,18 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here +// =============> so the function is to convert a decimal number into a percentage +// so if the number is 0.5 it is multiplied by 100 +// % sign is added at the end to make it 50% // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> the problem can be fixed by removing the redeclaration of the variable decimalNumber inside the function +function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} + +// Call the function and store the result in a variable +const result = convertToPercentage(0.5); +console.log(result); + diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..c7090da81 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,18 +3,29 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here +// =============> First there is a number inside the function which is not allowed in JavaScript it should be a variable name +//the second mistake is num is not declared function square(3) { return num * num; } -// =============> write the error message here +// =============> Uncaught SyntaxError: Unexpected number +// =============> we put a number (3)where JavaScript expected a variable name +//Uncaught SyntaxError: Illegal return statement -// =============> explain this error message here +//we cant use return inside the function without declaring the function properly // Finally, correct the code to fix the problem -// =============> write your new code here + +// =============> +function square(num) { + return num * num; +} +console.log(square(3)); // the answer should be 9 +console.log(square(5)); // the answer should be 25 +console.log(square(10)); // the answer should be 100 + diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..783868df3 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,7 @@ // Predict and explain first... -// =============> write your prediction here +// =============> console.log will print the result because log()will write the message to the console. +// function multiply(a, b) { console.log(a * b); @@ -8,7 +9,12 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here - +// =============> When testing the code in Node.js, the output was 320 without the message because it was undefined "The result of multiplying 10 and 32 is undefined". +//we need to use return to store the result then print it out. // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> // Corrected Code: +function multiply(a, b) { + return a * b; // Use 'return' instead of 'console.log' +} +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..86cb04116 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,5 +1,5 @@ // Predict and explain first... -// =============> write your prediction here +// =============> i think there will be and error message because at doesn't show what value to return so it will be undefined. function sum(a, b) { return; @@ -8,6 +8,10 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// ============= it should be in the same line after return statement to return the value // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> corrected code: +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..0545a6e82 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -1,7 +1,7 @@ // Predict and explain first... // Predict the output of the following code: -// =============> Write your prediction here +// =============> I predict that the code might not work because I am declaring num outside the function also I am giving num a constant value means it will always be 103 const num = 103; @@ -14,11 +14,19 @@ 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 code worked but the answer was 3 i predicted an error message. // Explain why the output is the way it is -// =============> write your explanation here +// =============> the output was 3 because at the beginning it we declared that num value as 103 // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> corrected code is running the code without giving num any value and add num in the function + +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 +// Explain why getLastDigit is not working properly - it was not working at the start because every time we run the code would slice the last number of the 103 because we declared num as a constant with value 103 diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..7234694e5 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -14,6 +14,18 @@ // Then when we call this function with the weight and height // 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 +// I have made a small change to the test cases to ensure they match the expected output to 1 decimal place + function calculateBMI(weight, height) { + // Calculate BMI using the formula: weight (kg) / (height (m) * height (m)) + const bmi = weight / (height * height); + + // Round to 1 decimal place and return + return Number(bmi.toFixed(1)); +} + +// Test cases +console.log(calculateBMI(85, 1.54)); // 35.8 +console.log(calculateBMI(60, 1.65)); // 22.0 +console.log(calculateBMI(72, 1.80)); // 22.2 +// now the function is reusable and correct. +console.log(typeof calculateBMI(85, 1.54)); \ 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..e2387d568 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,14 @@ // 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 + +// There is 2 things we need to do: change the space to_ and change the letters from lowercase to uppercase + +function toUpperSnakeCase(str) { + return str + .replaceAll(' ', '_') // replace spaces with underscores + .toUpperCase(); // make everything uppercase +} + +console.log(toUpperSnakeCase("hello there")); +console.log(toUpperSnakeCase("lord of the rings")); \ 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..86d48ff1f 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,50 @@ // 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 + +// I thought adding "lbs" need to be inside a string . +// i converted the string back to number + +//function toPounds(kg) { + //const pounds = kg * 2.20462; + //return Number(pounds.toFixed(2)); +//} +//console.log(`${toPounds(5)} lbs`); + + +// more Test cases +//console.log(`${toPounds(1)} lbs`); // 2.20 lbs +//console.log(`${toPounds(5)} lbs`); // 11.02 lbs +//console.log(`${toPounds(10)} lbs`); // 22.05 lbs + +// I have misunderstood the requirement + +// Original code from interpret/to-pounds.js + +function toPounds(penceString) { + // Remove the 'p' + const penceStringWithoutTrailingP = penceString.substring(0, penceString.length - 1); + + // Make sure there are at least 3 digits (e.g. "5" becomes "005") + const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0"); + + // Extract pounds (everything except the last two digits) + const pounds = paddedPenceNumberString.substring( + 0, + paddedPenceNumberString.length - 2 + ); + + // Extract pence + const pence = paddedPenceNumberString + .substring(paddedPenceNumberString.length - 2) + .padEnd(2, "0"); + + // 5️⃣ Return the formatted value + return `£${pounds}.${pence}`; +} + +// ✅ Test the function with examples +console.log(toPounds("399p")); // £3.99 +console.log(toPounds("5p")); // £0.05 +console.log(toPounds("50p")); // £0.50 +console.log(toPounds("1234p")); // £12.34 diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..1adacd2ca 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -17,18 +17,22 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> 3 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 +// =============> 0 first value is totalHours // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> "00" because the target length is 2 digits // 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 this is last value is remaining in Seconds // 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 because the target length is 2 digits