From 163a0fb92d0d195a87a4cc915b9a59435e237c7d Mon Sep 17 00:00:00 2001 From: Liyema Mfengwana <67449042+solohuns@users.noreply.github.com> Date: Thu, 17 Sep 2026 21:37:44 +0200 Subject: [PATCH] complete coursework --- Sprint-3/1-key-errors/0.js | 12 +++++++++- Sprint-3/1-key-errors/1.js | 11 ++++++++- Sprint-3/1-key-errors/2.js | 7 ++++++ Sprint-3/2-mandatory-debug/0.js | 8 +++++++ Sprint-3/2-mandatory-debug/1.js | 11 ++++++++- Sprint-3/2-mandatory-debug/2.js | 23 +++++++++++++++++++ Sprint-3/3-mandatory-implement/1-bmi.js | 8 ++++--- Sprint-3/3-mandatory-implement/2-cases.js | 10 ++++++++ Sprint-3/3-mandatory-implement/3-to-pounds.js | 17 +++++++++++++- Sprint-3/4-mandatory-interpret/time-format.js | 8 ++++++- 10 files changed, 107 insertions(+), 8 deletions(-) diff --git a/Sprint-3/1-key-errors/0.js b/Sprint-3/1-key-errors/0.js index 653d6f5a0..2e3ba809b 100644 --- a/Sprint-3/1-key-errors/0.js +++ b/Sprint-3/1-key-errors/0.js @@ -1,13 +1,23 @@ // Predict and explain first... -// =============> write your prediction here +// =============> It should take a string and capitalise the first letter but i predict it will throw an // call the function capitalise with a string input // interpret the error message and figure out why an error is occurring +/*The error occurs because str is declared twice. The function already receives str as a parameter, and then let str tries to create another variable with the same name inside the same scope. JavaScript does not allow redeclaring a variable with let, so it throws an error.*/ + function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } // =============> write your explanation here + +/* This function takes a string and capitalise the first letter. It uses `str[0].toUpperCase()` to convert the first character to uppercase and `str.slice(1)` to get the rest of the word unchanged. The function then joins both parts together and returns the new string. When `console.log(capitalise("hello"), capitalise("world"))` is run, the function executes twice and prints `Hello World`. + */ + // =============> write your new code here +function capitalise(str) { + return `${str[0].toUpperCase()}${str.slice(1)}`; +} +console.log(capitalise("hello"), capitalise("world")); diff --git a/Sprint-3/1-key-errors/1.js b/Sprint-3/1-key-errors/1.js index f2d56151f..04663032e 100644 --- a/Sprint-3/1-key-errors/1.js +++ b/Sprint-3/1-key-errors/1.js @@ -1,5 +1,4 @@ // Predict and explain first... - // Why will an error occur when this program runs? // =============> write your prediction here @@ -16,5 +15,15 @@ console.log(decimalNumber); // =============> write your explanation here +/*An error will occur when the program runs because decimalNumber is declared twice. The function already receives decimalNumber as a parameter, but then const decimalNumber = 0.5 tries to create another variable with the same name inside the same scope. JavaScript does not allow redeclaring variables with const. +Another error will happen at console.log(decimalNumber) because decimalNumber only exists inside the function and cannot be accessed outside of it.*/ + // 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..0ea310abc 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 +/*An error will occur because a number was used as a function parameter instead of a variable name.*/ function square(3) { return num * num; } // =============> write the error message here +//SyntaxError: Unexpected number // =============> explain this error message here +/*The error happens because JavaScript expects a variable name inside the function brackets, but it found a number instead.*/ // Finally, correct the code to fix the problem // =============> write your new code here +function square(num) { + return num * num +} +console.log(square(3)); diff --git a/Sprint-3/2-mandatory-debug/0.js b/Sprint-3/2-mandatory-debug/0.js index b27511b41..184767c3c 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 +/*The code will print a correct multiplication inside the function*/ 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 result shows undefined because the function prints the answer with console.log instead of returning it, so nothing is passed back into the template string.*/ // 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..d28b1df21 100644 --- a/Sprint-3/2-mandatory-debug/1.js +++ b/Sprint-3/2-mandatory-debug/1.js @@ -1,6 +1,6 @@ // Predict and explain first... // =============> write your prediction here - +//The sum of 10 and 32 is undefined function sum(a, b) { return; a + b; @@ -9,5 +9,14 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here + +/*The function returns undefined because the return; statement ends the function immediately, so the calculation a + b never runs or gets returned.*/ + // 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-3/2-mandatory-debug/2.js b/Sprint-3/2-mandatory-debug/2.js index 57d3f5dc3..0ee01c7f4 100644 --- a/Sprint-3/2-mandatory-debug/2.js +++ b/Sprint-3/2-mandatory-debug/2.js @@ -2,6 +2,11 @@ // Predict the output of the following code: // =============> Write your prediction here +/*The output will be: + +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 +20,28 @@ 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 always returns the last digit of 103 because it ignores the input parameter and uses a fixed global variable instead of the number passed into it.*/ + // Finally, correct the code to fix the problem // =============> write your new code here +function getLastDigit(number) { + return number.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 + +/*The function is not working properly because it ignores the input values and always uses the global variable num (which is 103), so every result returns the last digit of 103 instead of the number passed into the function.*/ diff --git a/Sprint-3/3-mandatory-implement/1-bmi.js b/Sprint-3/3-mandatory-implement/1-bmi.js index 58b1085f1..6ff5f6d1b 100644 --- a/Sprint-3/3-mandatory-implement/1-bmi.js +++ b/Sprint-3/3-mandatory-implement/1-bmi.js @@ -6,14 +6,16 @@ // squaring your height: 1.73 x 1.73 = 2.99 // dividing 70 by 2.99 = 23.41 -// Your result will be displayed to 1 decimal place, for example '23.4'. +// Your result will be displayed to 1 decimal place, for example 23.4. // You will need to implement a function that calculates the BMI of someone based off their weight and height // Given someone's weight in kg and height in metres // Then when we call this function with the weight and height -// It should return a string of their Body Mass Index to 1 decimal place +// 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 + const bmi = weight / (height * height); + return Number(bmi.toFixed(1)); // return the BMI of someone based off their weight and height } +console.log(calculateBMI(80, 1.73)); diff --git a/Sprint-3/3-mandatory-implement/2-cases.js b/Sprint-3/3-mandatory-implement/2-cases.js index 5b0ef77ad..6764997dc 100644 --- a/Sprint-3/3-mandatory-implement/2-cases.js +++ b/Sprint-3/3-mandatory-implement/2-cases.js @@ -14,3 +14,13 @@ // 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 + .split(" ") + .map((word) => word.toUpperCase()) + .join("_"); +} + +console.log(toUpperSnakeCase("hello there")); +console.log(toUpperSnakeCase("lord of the rings")); diff --git a/Sprint-3/3-mandatory-implement/3-to-pounds.js b/Sprint-3/3-mandatory-implement/3-to-pounds.js index 10754da73..97e8b43b7 100644 --- a/Sprint-3/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-3/3-mandatory-implement/3-to-pounds.js @@ -1,6 +1,21 @@ -// In Sprint-1, there is a program written in 3-mandatory-interpret/3-to-pounds.js +// In Sprint-1, there is a program written in interpret/to-pounds.js // You will need to take this code and turn it into a reusable block of code. // 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 withoutP = penceString.slice(0, -1); + + const padded = withoutP.padStart(3, "0"); + + const pounds = padded.slice(0, -2); + const pence = padded.slice(-2); + + return `£${pounds}.${pence}`; +} + +console.log(toPounds("399p")); +console.log(toPounds("45p")); +console.log(toPounds("5p")); diff --git a/Sprint-3/4-mandatory-interpret/time-format.js b/Sprint-3/4-mandatory-interpret/time-format.js index c0dd9c9a5..40dd94e86 100644 --- a/Sprint-3/4-mandatory-interpret/time-format.js +++ b/Sprint-3/4-mandatory-interpret/time-format.js @@ -22,17 +22,23 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// a) 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 +// b) 0 -// c) What is the return value of pad when it is called for the first time? +// c) What is the return value of pad is called for the first time? // =============> write your answer here +// c) "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 +// d) 1, because the last time pad is called it is for the remaining seconds which is 1 second in this case // 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 + +// e) "01", because the last time pad is called it is for the remaining seconds which is 1 second in this case, and pad adds a leading zero to make it two digits