From 5abf1f56e30e9f68145924aa3b4abdfb78a80565 Mon Sep 17 00:00:00 2001 From: Seti Mussa Date: Sat, 7 Mar 2026 19:02:44 +0000 Subject: [PATCH] Done coursework/sprint-2 --- Sprint-2/1-key-errors/0.js | 12 ++++++++++++ Sprint-2/1-key-errors/1.js | 13 +++++++++++++ Sprint-2/1-key-errors/2.js | 11 ++++++++++- Sprint-2/2-mandatory-debug/0.js | 12 ++++++++++++ Sprint-2/2-mandatory-debug/1.js | 8 ++++++++ Sprint-2/2-mandatory-debug/2.js | 16 +++++++++++++++- Sprint-2/3-mandatory-implement/1-bmi.js | 9 +++++++-- Sprint-2/3-mandatory-implement/2-cases.js | 4 ++++ Sprint-2/3-mandatory-implement/3-to-pounds.js | 5 +++++ Sprint-2/4-mandatory-interpret/time-format.js | 6 +++++- 10 files changed, 91 insertions(+), 5 deletions(-) diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..fb4c07873 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,5 +1,7 @@ // Predict and explain first... // =============> write your prediction here +// The program will give an error.The error happens because `str`is written twice inside the function. + // 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 function already has an input called `str` +// Inside the function, the code tries to create another variable called `str`using `let` +//JavaScript does not allow the same varible name to be created again in same place. +//Because of this, the program throwns an error. + // =============> write your new code here + +function captialise(str){ + str = `${str[0].toUpperCase()}${str.slice(1)}`; + return str; +} diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..ddaae9d09 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -3,6 +3,9 @@ // Why will an error occur when this program runs? // =============> write your prediction here +// The program will give an error because of the `decimalNumber` is used outside the function. +// Even though it is only created inside the function. + // Try playing computer with the example to work out what is going on function convertToPercentage(decimalNumber) { @@ -16,5 +19,15 @@ console.log(decimalNumber); // =============> write your explanation here +//The variable `decimalNumber`is only insdie the function. +// At the end of the program, console.log( decimalNumber) tries to print it , +// however, JavaScript cannot find that varible outside the function. +// Because of this, the program throws an error. + // 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..696bd74b0 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -1,20 +1,29 @@ // Predict and explain first BEFORE you run any code... +// The program will give a SyntazError because a number (3) is used as peramter name in the function. // this function should square any number but instead we're going to get an error // =============> write your prediction of the error here +// My perdiction is that program will not run and show a SyntaxError because 3 is not a valid variable name. function square(3) { return num * num; } // =============> write the error message here +// SyntaxError: Unexpected number // =============> explain this error message here +// Funtion parameters must be variable names. +// You cannot use a number like 3 as name of parameter. +//Javascript required a valid varible name inside the parentheses. // 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-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..d3c710924 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,9 @@ // Predict and explain first... // =============> write your prediction here +// The program will print 320 first. +// Then it will " The result of mutiplying 10 and 32 is undefined". + function multiply(a, b) { console.log(a * b); @@ -9,6 +12,15 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); // =============> write your explanation here +// The function multiply is used console.log(a * b) to show the result +// however, it does not resturn the value . +// when the function is used inside the sentence with ${multiply(10, 32)} +// JavaScript expects the function to give back a value. +//Becuse the function does not return anything, the result becomes 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-2/2-mandatory-debug/1.js b/Sprint-2/2-mandatory-debug/1.js index 37cedfbcf..a4d5d22aa 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 +//My perdiction the code will show "undefined" becuse the function does not return the sum numbers. function sum(a, b) { return; @@ -9,5 +10,12 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); // =============> write your explanation here +// The return statemment is ematy and the "c=b" is written after return. +//When JavaScript see return it stops the function, so a+b is never used. + // 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..c7eecb570 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 +//The code will always show 3 as the last digit because the function does not use the number inside getLastDigit(). const num = 103; @@ -15,10 +15,24 @@ 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 does not take any input parameter, it always uses the global variavle num = 103. +// Because of this the last digit returend is always 3, regardless of the number passed in the function call. + // 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 $(getlastDigita(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 was fixed by adding a parameter so it can return the last digit of the given number. diff --git a/Sprint-2/3-mandatory-implement/1-bmi.js b/Sprint-2/3-mandatory-implement/1-bmi.js index 17b1cbde1..0f0d40488 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 +} + +function calculateBMI(weight, height) { + const bmi = weight / (height * height); + return Number(bmi.toFixed(1)); +} diff --git a/Sprint-2/3-mandatory-implement/2-cases.js b/Sprint-2/3-mandatory-implement/2-cases.js index 5b0ef77ad..425f38868 100644 --- a/Sprint-2/3-mandatory-implement/2-cases.js +++ b/Sprint-2/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 convertToUpperSnakeCase(str) { + return str.toUpperCase().trim().split("").join("_"); +} diff --git a/Sprint-2/3-mandatory-implement/3-to-pounds.js b/Sprint-2/3-mandatory-implement/3-to-pounds.js index 6265a1a70..cd1f81332 100644 --- a/Sprint-2/3-mandatory-implement/3-to-pounds.js +++ b/Sprint-2/3-mandatory-implement/3-to-pounds.js @@ -4,3 +4,8 @@ // 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(kilograms) { + const pounds = kilograms * 2.20462; + return Number(pounds.toFixed(2)); +} diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..4063821dd 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -18,17 +18,21 @@ function formatTimeDisplay(seconds) { // a) When formatTimeDisplay is called how many times will pad be called? // =============> write your answer here +// Pad wil be called 3 times because the return statement uses pad for hours, mintues and seconds. // 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 +// The value is 0 because totalHours is 0 when the input is 61 seconds. // c) What is the return value of pad is called for the first time? // =============> write your answer here +// The return value is "00" because padStart makes the number two digits by adding a 0 at the start. // 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 - +// The value is 1 because the last call to pad uses remainingSeconds, which is 1 when the input is 61. // 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 +// The return value is "01" because padStart adds a 0 infornt of the single digit number.