From b470233ffe4304aaa34369911fb84c235e58ed56 Mon Sep 17 00:00:00 2001 From: "[Halimatou Waghe]" <[waghehalimatou@gmail.com]> Date: Wed, 18 Jun 2025 21:55:07 +0100 Subject: [PATCH 1/5] 1-key errors --- Sprint-2/1-key-errors/0.js | 12 ++++++++++++ Sprint-2/1-key-errors/1.js | 12 ++++++++++++ Sprint-2/1-key-errors/2.js | 10 ++++++++++ 3 files changed, 34 insertions(+) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..7d377d084 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,6 +1,8 @@ // Predict and explain first... // =============> write your prediction here +// I think the code will not rum properly because let in line 10 should be declared before the function in line 9 is called. + // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring @@ -10,4 +12,14 @@ function capitalise(str) { } // =============> write your explanation here + +// The error is: Uncaught SyntaxError: Identifier 'str' has already been declared. It means that we cannot declare a variable with the same name as a parameter inside the function. In this case, We don't need the declaration let as str is already declared in the function's parameter. + // =============> write your new code here + +function capitalise(str) { + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} + +console.log(capitalise("hello world")); diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..645e20d85 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -3,6 +3,8 @@ // Why will an error occur when this program runs? // =============> write your prediction here +//I think an error will occur when this program runs because the variable decimalNumber is declared in line 10 in the parameter of the function and on line 11 as a constant. + // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { @@ -16,5 +18,15 @@ console.log(decimalNumber); // =============> write your explanation here +// The error is: Uncaught SyntaxError: Identifier 'decimalNumber' has already been declared. This means that we cannot declare a variable with the same name as a parameter inside the function. In this case, we don't need the declaration const as decimalNumber is already declared in the function's parameter. + // 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 aad57f7cf..03557c44e 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 +//The error will probably be related to the digit 3 in the function because 3 is not considered as a number in javascript. We need to explicitly declare it as a number for the code to run. function square(3) { return num * num; } // =============> write the error message here +// The error message is: Uncaught SyntaxError: Unexpected number // =============> explain this error message here +//The error message means that we cannot use a digit number as a parameter name in a function. In this case, we need to use a valid variable name that Javascript recognises like num instead of the digit number 3. // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num; +} + +console.log(square(3)); + + From 05297a1099329875b478ec2c1848de3f58ee05b9 Mon Sep 17 00:00:00 2001 From: "[Halimatou Waghe]" <[waghehalimatou@gmail.com]> Date: Thu, 19 Jun 2025 10:54:40 +0100 Subject: [PATCH 2/5] Mandatory-debug --- Sprint-2/2-mandatory-debug/0.js | 11 +++++++++++ Sprint-2/2-mandatory-debug/1.js | 12 ++++++++++++ Sprint-2/2-mandatory-debug/2.js | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..6b7db59bc 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -2,6 +2,8 @@ // =============> write your prediction here +// I think the error will probably be because the function is not followed by a return statement. + function multiply(a, b) { console.log(a * b); } @@ -10,5 +12,14 @@ console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// The message printed is "The result of multiplying 10 and 32 is undefined". As the function multiply does not have a return statement, console.log cannot print the result of the multiplication. +// To allow the function to return the result of the multiplication, we need to add a return statement inside the function. + // 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 37cedfbcf..d44101d7f 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,6 +1,8 @@ // Predict and explain first... // =============> write your prediction here +// I think the error will probably be because the return statement is not correctly placed in the function. + function sum(a, b) { return; a + b; @@ -9,5 +11,15 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here + +// The message printed is "The sum of 10 and 32 is undefined". As the expression of the return statement is placed in a new line, it is not being executed. +// To allow the function to return the result of the sum, we need to place the expression in the same line as the return statement. + // 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)}`); diff --git a/Sprint-2/2-mandatory-debug/2.js b/Sprint-2/2-mandatory-debug/2.js index 57d3f5dc3..08d50334c 100644 --- a/Sprint-2/2-mandatory-debug/2.js +++ b/Sprint-2/2-mandatory-debug/2.js @@ -3,6 +3,8 @@ // Predict the output of the following code: // =============> Write your prediction here +// I think the error will probably be because num as been defined as a constant with a value. So the function will only return the output with the constant num as a value. + const num = 103; function getLastDigit() { @@ -15,10 +17,27 @@ 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 is: +// 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 output is like this because the function parameter is not being used. So, the function is using the constant num with a value of 103. That's why the last digit of each number is always 3, which is the last digit of 103. + // Finally, correct the code to fix the problem // =============> write your new code here +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 From 153ef6d726824ea777d73aa7444bf6e3d55b212d Mon Sep 17 00:00:00 2001 From: "[Halimatou Waghe]" <[waghehalimatou@gmail.com]> Date: Thu, 19 Jun 2025 12:37:08 +0100 Subject: [PATCH 3/5] 3-mandatory implement --- Sprint-2/3-mandatory-implement/1-bmi.js | 9 +++++-- Sprint-2/3-mandatory-implement/2-cases.js | 7 ++++++ Sprint-2/3-mandatory-implement/3-to-pounds.js | 24 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..94d27cf88 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -15,5 +15,10 @@ // 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 + // return the BMI of someone based off their weight and height + + const bmi = weight / (height * height); + return bmi.toFixed(1); +} + +console.log(calculateBMI(70, 1.73)); diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..9c58714f6 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/3-mandatory-implement/2-cases.js @@ -14,3 +14,10 @@ // 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 convertToUpperSnakeCase(input) { + return input.toUpperCase().replaceAll(" ", "_"); +} + +console.log(convertToUpperSnakeCase("hello there")); +console.log(convertToUpperSnakeCase("lord of the rings")); diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..4dd010c04 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,27 @@ // 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("8p")); +console.log(toPounds("77p")); +console.log(toPounds("1007p")); From c511a6920305a867c7e95ca6fd68333236fa58af Mon Sep 17 00:00:00 2001 From: "[Halimatou Waghe]" <[waghehalimatou@gmail.com]> Date: Thu, 19 Jun 2025 13:24:49 +0100 Subject: [PATCH 4/5] 4-mandatory-interpret --- Sprint-2/4-mandatory-interpret/time-format.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..a1a0baf3c 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -11,6 +11,8 @@ function formatTimeDisplay(seconds) { return `${pad(totalHours)}:${pad(remainingMinutes)}:${pad(remainingSeconds)}`; } +console.log(formatTimeDisplay(61)); + // You will need to play computer with this example - use the Python Visualiser https://pythontutor.com/visualize.html#mode=edit // to help you answer these questions @@ -18,17 +20,22 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// pad will be called 3 times, one for totalHours, one for remainingMinutes and one for 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 +// It is 0 because it is calling for totalHours which is 0 in this case. // c) What is the return value of pad is called for the first time? // =============> write your answer here +// It is '00' because the function converts the number 0 to a string and pads it with zeros to make sure it is at least 2 characters long. // 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 +// It is 1 because it is calling for remainingSeconds which is 1 in this case. // 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 +// It is '01' because the function converts the number 1 to a string and pads it with zeros to make sure it is at least 2 characters long. From 7c407995ecb47286ac76ae2c395794a0a861ad78 Mon Sep 17 00:00:00 2001 From: "[Halimatou Waghe]" <[waghehalimatou@gmail.com]> Date: Thu, 3 Jul 2025 22:09:47 +0100 Subject: [PATCH 5/5] Correcting the function in bmi to include parseFloat, so that it returns a number --- Sprint-2/3-mandatory-implement/1-bmi.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 94d27cf88..bfce6be62 100644 --- a/Sprint-2/3-mandatory-implement/1-bmi.js +++ b/Sprint-2/3-mandatory-implement/1-bmi.js @@ -18,7 +18,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); + return parseFloat(bmi.toFixed(1)); } console.log(calculateBMI(70, 1.73));