From 5abf1f56e30e9f68145924aa3b4abdfb78a80565 Mon Sep 17 00:00:00 2001 From: Seti Mussa Date: Sat, 7 Mar 2026 19:02:44 +0000 Subject: [PATCH 1/4] 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 653d6f5a07..fb4c078733 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 f2d56151f4..ddaae9d092 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 aad57f7cfe..696bd74b0e 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 b27511b417..d3c7109245 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 37cedfbcfd..a4d5d22aa3 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 57d3f5dc35..c7eecb5703 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 17b1cbde1b..0f0d404889 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 5b0ef77ad9..425f388689 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 6265a1a703..cd1f813321 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 7c98eb0e8c..4063821dde 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. From 2e49fad50c964d3604fab9444c3c3849d2eace0e Mon Sep 17 00:00:00 2001 From: Seti Mussa Date: Fri, 13 Mar 2026 17:34:49 +0000 Subject: [PATCH 2/4] Write test for getAngleType --- .../implement/1-get-angle-type.js | 31 ++++++++++++++++++- package.json | 6 ++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js index 9e05a871e2..7a145deaad 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -15,9 +15,31 @@ // execute the code to ensure all tests pass. function getAngleType(angle) { - // TODO: Implement this function + if (angle <= 0 || angle >= 360) return "invalid angle"; } +if (angle < 90) { + return "acute angle"; +} + +if (angle === 90) { + return "right angle"; +} + +if (angle < 180) { + return "obtuse angle"; +} + +if (angle === 180) { + return " straight angle"; +} + +if (angle < 360) { + return "reflext angle"; +} + +// TODO: Implement this function + // The line below allows us to load the getAngleType function into tests in other files. // This will be useful in the "rewrite tests with jest" step. module.exports = getAngleType; @@ -30,6 +52,13 @@ function assertEquals(actualOutput, targetOutput) { `Expected ${actualOutput} to equal ${targetOutput}` ); } +// Test +assertEquals(getAngleType(45), "Acute angle"); +assertEquals(getAngleType(90), "Right angle"); +assertEquals(getAngleType(120), "Obtuse angle"); +assertEquals(getAngleType(180), "Straight angle"); +assertEquals(getAngleType(270), "Reflex angle"); +assertEquals(getAngleType(390), "invalid angle"); // TODO: Write tests to cover all cases, including boundary and invalid cases. // Example: Identify Right Angles diff --git a/package.json b/package.json index 0657e22dd8..6c91c8de47 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "keywords": [], "author": "Code Your Future", "license": "ISC", - "dependencies": { - "jest": "^29.7.0" + "devDependencies": { + "jest": "^30.3.0" } -} \ No newline at end of file +} From 2dcf04219843080a0aa4381880cd93a0536ac8bf Mon Sep 17 00:00:00 2001 From: Seti Mussa Date: Sat, 14 Mar 2026 11:47:26 +0000 Subject: [PATCH 3/4] I have correct the error --- .../implement/1-get-angle-type.js | 4 ++-- .../implement/2-is-proper-fraction.js | 21 ++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js index 7a145deaad..be0f7d058c 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -37,12 +37,12 @@ if (angle === 180) { if (angle < 360) { return "reflext angle"; } - +module.exports = getAngleType; // TODO: Implement this function // The line below allows us to load the getAngleType function into tests in other files. // This will be useful in the "rewrite tests with jest" step. -module.exports = getAngleType; + // This helper function is written to make our assertions easier to read. // If the actual output matches the target output, the test will pass diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js index 970cb9b641..dea9ed55dd 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -11,8 +11,15 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - // TODO: Implement this function + + if (numerator < denominator) { + return true; + } else { + return false; + } } + // TODO: Implement this function + // The line below allows us to load the isProperFraction function into tests in other files. // This will be useful in the "rewrite tests with jest" step. @@ -29,5 +36,17 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all cases. // What combinations of numerators and denominators should you test? +//test // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); +// Proper fractions +assertEquals(isProperFraction(1, 2), true); +assertEquals(isProperFraction(3, 5), true); + +// Not proper fractions +assertEquals(isProperFraction(5, 5), false); +assertEquals(isProperFraction(7, 3), false); + +// Edge case +assertEquals(isProperFraction(0, 5), true); +What the function does From 542ca6d659b2ae7e2661daebdc0e2680eb7099bb Mon Sep 17 00:00:00 2001 From: Seti Mussa Date: Sat, 14 Mar 2026 23:56:52 +0000 Subject: [PATCH 4/4] Sprint 3 implementations --- .../implement/1-get-angle-type.js | 1 - .../implement/2-is-proper-fraction.js | 13 +++++----- .../implement/3-get-card-value.js | 25 ++++++++++++++++++- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js index be0f7d058c..df36e01660 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -43,7 +43,6 @@ module.exports = getAngleType; // The line below allows us to load the getAngleType function into tests in other files. // This will be useful in the "rewrite tests with jest" step. - // This helper function is written to make our assertions easier to read. // If the actual output matches the target output, the test will pass function assertEquals(actualOutput, targetOutput) { diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js index dea9ed55dd..dc3504e908 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -11,15 +11,13 @@ // execute the code to ensure all tests pass. function isProperFraction(numerator, denominator) { - - if (numerator < denominator) { + if (Math.abs(numerator) < Math.abs(denominator)) { return true; - } else { + } else { return false; } } - // TODO: Implement this function - +// TODO: Implement this function // The line below allows us to load the isProperFraction function into tests in other files. // This will be useful in the "rewrite tests with jest" step. @@ -36,7 +34,8 @@ function assertEquals(actualOutput, targetOutput) { // TODO: Write tests to cover all cases. // What combinations of numerators and denominators should you test? -//test +// Tests + // Example: 1/2 is a proper fraction assertEquals(isProperFraction(1, 2), true); // Proper fractions @@ -49,4 +48,4 @@ assertEquals(isProperFraction(7, 3), false); // Edge case assertEquals(isProperFraction(0, 5), true); -What the function does +assertEquals(isProperFraction(-1, 2), true); diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js index c7559e787e..76b59b13af 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js @@ -22,6 +22,17 @@ // execute the code to ensure all tests pass. function getCardValue(card) { + const rank = card.slice(0, -1); + if (rank === "A") { + return 11; + } + if (rank === "J" || rank === "Q" || rank === "K") { + return 10; + } + if (rank >= "2" && rank <= "10") { + return parseInt(rank); + } + throw new Error("Invalid card"); // TODO: Implement this function } @@ -38,10 +49,18 @@ function assertEquals(actualOutput, targetOutput) { } // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. + // Examples: + +//Test + assertEquals(getCardValue("9♠"), 9); +assertEquals(getCardValue("A♥"), 11); +assertEquals(getCardValue("J♦"), 10); +assertEquals(getCardValue("Q♣"), 10); +assertEquals(getCardValue("K♦"), 10); +// invalid cards test -// Handling invalid cards try { getCardValue("invalid"); @@ -50,3 +69,7 @@ try { } catch (e) {} // What other invalid card cases can you think of? +try { + getCardValue("11♠"); // invalid rank + console.error("Error was not thrown"); +} catch (e) {}