From 4f08500ba3200c01d980c6b2de0e2cc2e0b3ff30 Mon Sep 17 00:00:00 2001 From: Asanda Date: Sat, 26 Sep 2026 21:47:56 +0200 Subject: [PATCH] sprint 3 --- Sprint-3/1-key-errors/0.js | 7 +++++++ Sprint-3/1-key-errors/1.js | 10 ++++++++++ Sprint-3/1-key-errors/2.js | 9 ++++++++- Sprint-3/2-mandatory-debug/0.js | 8 ++++++++ Sprint-3/2-mandatory-debug/1.js | 7 +++++++ Sprint-3/2-mandatory-debug/2.js | 19 +++++++++++++++++++ Sprint-3/3-mandatory-implement/1-bmi.js | 3 +++ Sprint-3/3-mandatory-implement/2-cases.js | 4 ++++ Sprint-3/3-mandatory-implement/3-to-pounds.js | 10 ++++++++++ Sprint-3/4-mandatory-interpret/time-format.js | 6 +++++- 10 files changed, 81 insertions(+), 2 deletions(-) diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..82701d392 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -10,4 +10,11 @@ function capitalise(str) { } // =============> write your explanation here + +// The code will produce a SyntaxError because str is declared twice as a function parameter and again with let. // =============> write your new code here + +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..fd08615cb 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -3,6 +3,9 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// An error will occur because decimalNumber is declared twice inside the function, and +// decimalNumber is also not accessible outside the function. + // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { @@ -18,3 +21,10 @@ console.log(decimalNumber); // 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-3/1-key-errors/2.js b/Sprint-3/1-key-errors/2.js index aad57f7cf..153d65e66 100644 --- a/Sprint-3/1-key-errors/2.js +++ b/Sprint-3/1-key-errors/2.js @@ -4,17 +4,24 @@ // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// A SyntaxError will occur because 3 cannot be used as a function parameter. function square(3) { return num * num; } // =============> write the error message here +// SyntaxError: Invalid or unexpected token // =============> explain this error message here +// Function parameters must be variable names and not a number, +// hence this is a syntaxError + // Finally, correct the code to fix the problem // =============> write your new code here - +function square(num) { + return num * num; +} diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..e7736f7d6 100644 --- a/Sprint-3/2-mandatory-debug/0.js +++ b/Sprint-3/2-mandatory-debug/0.js @@ -1,6 +1,7 @@ // Predict and explain first... // =============> write your prediction here +// It will probably print 320 so it will multiply the 10 by 32 and then undefined because the multiply() is not returning anything function multiply(a, b) { console.log(a * b); @@ -9,6 +10,13 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// The function prints 320, but because there is no return statement, multiply(10, 32) evaluates to 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-3/2-mandatory-debug/1.js b/Sprint-3/2-mandatory-debug/1.js index 37cedfbcf..3f9c19d23 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,5 +1,6 @@ // Predict and explain first... // =============> write your prediction here +// The function will return undefined, function sum(a, b) { return; @@ -9,5 +10,11 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// The return; statement ends the function immediately. Therefore, a + b is never executed. // 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-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..3122fe79b 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -2,6 +2,9 @@ // Predict the output of the following code: // =============> Write your prediction here +// The last digit of 42 is 3 +// The last digit of 105 is 3 +// The last digit of 806 is 3 const num = 103; @@ -15,10 +18,26 @@ 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 + +// The function doesn't accept a parameter. It always uses the global variable: // Finally, correct the code to fix the problem // =============> write your new code here +const num = 103; + +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 diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..ff103913c 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -16,4 +16,7 @@ function calculateBMI(weight, height) { // return the BMI of someone based off their weight and height + const bmi = weight / (height * height); + return bmi.toFixed(1); } + diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..c30a604e2 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,7 @@ // 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) { + return str.toUpperCase().replaceAll(" ", "_"); +} \ No newline at end of file diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..6961a9acc 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,13 @@ // 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(pence) { + const pounds = Math.floor(pence / 100); + const remainingPence = pence % 100; + + return `£${pounds}.${remainingPence.toString().padStart(2, "0")}`; +} + +console.log(toPounds(399)); +console.log(toPounds(100)); +console.log(toPounds(1250)); \ No newline at end of file diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..c59a90171 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -22,17 +22,21 @@ 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 +// 1 // c) What is the return value of pad when it is called for the first time? // =============> write your answer here +// "01" // 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 call to pad is for remainingSeconds, which is 1 when seconds is 61. // 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 +// "01" — pad(1) converts 1 to a string and adds "0" to the beginning.