From ea913028e9b0bf3c001e2bfef68619162ce1d148 Mon Sep 17 00:00:00 2001 From: Elfredah <83861738+Elfredah@users.noreply.github.com> Date: Tue, 18 Mar 2025 13:21:45 +0000 Subject: [PATCH] I made changes and corrected codes that had errors --- Sprint-1/1-key-exercises/1-count.js | 1 + Sprint-1/1-key-exercises/2-initials.js | 8 ++--- Sprint-1/1-key-exercises/3-paths.js | 4 +-- Sprint-1/1-key-exercises/4-random.js | 12 +++++++ Sprint-1/2-mandatory-errors/0.js | 4 +-- Sprint-1/2-mandatory-errors/1.js | 4 +++ Sprint-1/2-mandatory-errors/2.js | 8 +++-- Sprint-1/2-mandatory-errors/3.js | 13 ++++++- Sprint-1/2-mandatory-errors/4.js | 7 ++-- .../1-percentage-change.js | 5 +++ Sprint-2/1-key-errors/0.js | 6 ++-- Sprint-2/1-key-errors/1.js | 14 ++++++-- Sprint-2/1-key-errors/2.js | 9 +++-- Sprint-2/2-mandatory-debug/0.js | 12 +++++-- Sprint-2/2-mandatory-debug/1.js | 12 ++++--- Sprint-2/4-mandatory-interpret/time-format.js | 19 ++++++++--- Sprint-2/5-stretch-extend/format-time.js | 34 +++++++++++++++++++ Sprint-3/1-key-implement/1-get-angle-type.js | 9 +++-- .../1-key-implement/2-is-proper-fraction.js | 23 +++++++++++-- Sprint-3/1-key-implement/3-get-card-value.js | 6 +++- .../3-mandatory-practice/implement/count.js | 8 ++++- 21 files changed, 175 insertions(+), 43 deletions(-) diff --git a/Sprint-1/1-key-exercises/1-count.js b/Sprint-1/1-key-exercises/1-count.js index 117bcb2b6..a6511fc2a 100644 --- a/Sprint-1/1-key-exercises/1-count.js +++ b/Sprint-1/1-key-exercises/1-count.js @@ -4,3 +4,4 @@ count = count + 1; // Line 1 is a variable declaration, creating the count variable with an initial value of 0 // Describe what line 3 is doing, in particular focus on what = is doing +//Line 3 is updating the value of count. Therefore, line 3 is just increasing the value of count by 1. \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/2-initials.js b/Sprint-1/1-key-exercises/2-initials.js index 47561f617..ad11ea3b2 100644 --- a/Sprint-1/1-key-exercises/2-initials.js +++ b/Sprint-1/1-key-exercises/2-initials.js @@ -1,11 +1,11 @@ -let firstName = "Creola"; -let middleName = "Katherine"; -let lastName = "Johnson"; +let firstName = "Elfredah"; +let middleName = "Kevin"; +let lastName = "Alerechi"; // Declare a variable called initials that stores the first character of each string. // This should produce the string "CKJ", but you must not write the characters C, K, or J in the code of your solution. -let initials = ``; +let initials = "firstName[0] + middleName[0] + lastName[0]"; // https://www.google.com/search?q=get+first+character+of+string+mdn diff --git a/Sprint-1/1-key-exercises/3-paths.js b/Sprint-1/1-key-exercises/3-paths.js index ab90ebb28..4505867f3 100644 --- a/Sprint-1/1-key-exercises/3-paths.js +++ b/Sprint-1/1-key-exercises/3-paths.js @@ -17,7 +17,7 @@ console.log(`The base part of ${filePath} is ${base}`); // Create a variable to store the dir part of the filePath variable // Create a variable to store the ext part of the variable -const dir = ; -const ext = ; +const dir ="filePath.slice(0, lastSlashIndex)"; +const ext = base.split(".").pop(); // https://www.google.com/search?q=slice+mdn \ No newline at end of file diff --git a/Sprint-1/1-key-exercises/4-random.js b/Sprint-1/1-key-exercises/4-random.js index 292f83aab..1ed22abe2 100644 --- a/Sprint-1/1-key-exercises/4-random.js +++ b/Sprint-1/1-key-exercises/4-random.js @@ -7,3 +7,15 @@ const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; // Try breaking down the expression and using documentation to explain what it means // It will help to think about the order in which expressions are evaluated // Try logging the value of num and running the program several times to build an idea of what the program is doing + + +//num represents a random whole number between 1 and 100 (inclusive). +//Math.random(): This generates a random decimal number between 0 (inclusive) and 1 (exclusive). So, it could be something like 0.23, 0.89, etc. + +//Math.random() * (maximum - minimum + 1): This scales the random number to be between 0 and 100 (because maximum - minimum + 1 = 100). So, it could give a result like 23.5 or 89.7. + +//Math.floor(): This function rounds down the number to the nearest whole number. So 23.5 becomes 23 or 89.7 becomes 89. + +//+ minimum: Finally, it adds 1 (the minimum) to ensure the number starts at 1. So, Math.floor(Math.random() * 100) gives a number from 0 to 99, and adding 1 ensures the range is from 1 to 100. + +//Result: num will be a random integer between 1 and 100 each time you run the code. \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/0.js b/Sprint-1/2-mandatory-errors/0.js index cf6c5039f..65ad3030d 100644 --- a/Sprint-1/2-mandatory-errors/0.js +++ b/Sprint-1/2-mandatory-errors/0.js @@ -1,2 +1,2 @@ -This is just an instruction for the first activity - but it is just for human consumption -We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file +//This is just an instruction for the first activity - but it is just for human consumption +//We don't want the computer to run these 2 lines - how can we solve this problem? \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/1.js b/Sprint-1/2-mandatory-errors/1.js index 7a43cbea7..a4b31152f 100644 --- a/Sprint-1/2-mandatory-errors/1.js +++ b/Sprint-1/2-mandatory-errors/1.js @@ -2,3 +2,7 @@ const age = 33; age = age + 1; + +let age = 33; // Using let to declare a variable that can be reassigned +age = age + 4; // Now I am reassigning the value of age +console.log(age); // This will print 37 to the console \ No newline at end of file diff --git a/Sprint-1/2-mandatory-errors/2.js b/Sprint-1/2-mandatory-errors/2.js index e09b89831..badd8647e 100644 --- a/Sprint-1/2-mandatory-errors/2.js +++ b/Sprint-1/2-mandatory-errors/2.js @@ -1,5 +1,9 @@ // Currently trying to print the string "I was born in Bolton" but it isn't working... // what's the error ? -console.log(`I was born in ${cityOfBirth}`); -const cityOfBirth = "Bolton"; +//The error occurs because you are trying to use the variable cityOfBirth before it is defined. +// In JavaScript, the line const cityOfBirth = "Bolton"; +// needs to run before you try to use cityOfBirth in the console.log() statement. See the corrected code below: + +const cityOfBirth = "Bolton"; // First, define the variable +console.log(`I was born in ${cityOfBirth}`); // Then, use it diff --git a/Sprint-1/2-mandatory-errors/3.js b/Sprint-1/2-mandatory-errors/3.js index ec101884d..fdd83ee62 100644 --- a/Sprint-1/2-mandatory-errors/3.js +++ b/Sprint-1/2-mandatory-errors/3.js @@ -1,5 +1,5 @@ const cardNumber = 4533787178994213; -const last4Digits = cardNumber.slice(-4); +const last4Digits = cardNumber.slice(-4); // This will throw an error // The last4Digits variable should store the last 4 digits of cardNumber // However, the code isn't working @@ -7,3 +7,14 @@ const last4Digits = cardNumber.slice(-4); // Then run the code and see what error it gives. // Consider: Why does it give this error? Is this what I predicted? If not, what's different? // Then try updating the expression last4Digits is assigned to, in order to get the correct value + +//The code will not work because cardNumber is a number, and the .slice() method is used for strings, not numbers. +// The .slice() method cannot be used directly on a number, which will lead to an error. +//TypeError: cardNumber.slice is not a function. +//This happens because .slice() is a method that only works on strings and arrays, but cardNumber is a number. +// You can't call .slice() on a number. +//To fix this, we need to convert the number to a string before using .slice() to extract the last 4 digits: + +const cardNumber = 4533787178994213; +const last4Digits = String(cardNumber).slice(-4); // Convert to string first +console.log(last4Digits); // This will output "4213" diff --git a/Sprint-1/2-mandatory-errors/4.js b/Sprint-1/2-mandatory-errors/4.js index 21dad8c5d..ff54bb471 100644 --- a/Sprint-1/2-mandatory-errors/4.js +++ b/Sprint-1/2-mandatory-errors/4.js @@ -1,2 +1,5 @@ -const 12HourClockTime = "20:53"; -const 24hourClockTime = "08:53"; \ No newline at end of file + +const twelveHourClockTime = "20:53"; // Use letters instead of starting with a number +const twentyFourHourClockTime = "08:53"; // Use letters instead of starting with a number + + diff --git a/Sprint-1/3-mandatory-interpret/1-percentage-change.js b/Sprint-1/3-mandatory-interpret/1-percentage-change.js index e24ecb8e1..2a662e7fb 100644 --- a/Sprint-1/3-mandatory-interpret/1-percentage-change.js +++ b/Sprint-1/3-mandatory-interpret/1-percentage-change.js @@ -20,3 +20,8 @@ console.log(`The percentage change is ${percentageChange}`); // d) Identify all the lines that are variable declarations // e) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression? + +//There are two function calls in the file. They are: + +//a. carPrice.replaceAll(",", "") – This calls the replaceAll() method to remove commas from the carPrice string. +//priceAfterOneYear.replaceAll(",", "") – This also calls the replaceAll() method to remove commas from the priceAfterOneYear string. \ No newline at end of file diff --git a/Sprint-2/1-key-errors/0.js b/Sprint-2/1-key-errors/0.js index 653d6f5a0..01b3b8052 100644 --- a/Sprint-2/1-key-errors/0.js +++ b/Sprint-2/1-key-errors/0.js @@ -1,13 +1,13 @@ // Predict and explain first... -// =============> write your prediction here +// =============> I feel the spring will count 0 to 1 // call the function capitalise with a string input -// interpret the error message and figure out why an error is occurring +// There is an error message that say the 'str' has already been declared. function capitalise(str) { let str = `${str[0].toUpperCase()}${str.slice(1)}`; return str; } - +capitalise("elfredah") // =============> write your explanation here // =============> write your new code here diff --git a/Sprint-2/1-key-errors/1.js b/Sprint-2/1-key-errors/1.js index f2d56151f..f099490b8 100644 --- a/Sprint-2/1-key-errors/1.js +++ b/Sprint-2/1-key-errors/1.js @@ -1,7 +1,8 @@ // Predict and explain first... +//My prediction: The program will throw an error when it runs. Specifically, it will result in a SyntaxError or ReferenceError // Why will an error occur when this program runs? -// =============> write your prediction here +// =============> An error will occur because the function parameter decimalNumber is redeclared inside the function using const, which is not allowed in JavaScript. // Try playing computer with the example to work out what is going on @@ -14,7 +15,14 @@ function convertToPercentage(decimalNumber) { console.log(decimalNumber); -// =============> write your explanation here +// =============> The error happens because the function tries to redeclare decimalNumber inside its own scope, which isn't allowed in JavaScript. Also, console.log(decimalNumber); is trying to print a variable that doesn't exist in the global scope, causing another error. // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> function convertToPercentage(decimalNumber) { + const percentage = `${decimalNumber * 100}%`; + return percentage; +} + +const result = convertToPercentage(0.5); +console.log(result); +//The code above fixes the issue by removing the redeclaration of decimalNumber inside the function and moving the console.log(decimalNumber); outside the function. This way, the function can access the decimalNumber parameter passed to it and return the correct result. The console.log() statement will then print the result to the console. \ No newline at end of file diff --git a/Sprint-2/1-key-errors/2.js b/Sprint-2/1-key-errors/2.js index aad57f7cf..5561fc469 100644 --- a/Sprint-2/1-key-errors/2.js +++ b/Sprint-2/1-key-errors/2.js @@ -3,15 +3,14 @@ // this function should square any number but instead we're going to get an error -// =============> write your prediction of the error here - -function square(3) { +// =============> There seem to be no error in the function itself. +function square(num) { return num * num; } -// =============> write the error message here +// =============> . The function square(num) is correctly defined and will work as expected when called with a valid number. -// =============> explain this error message here +// =============> I ran the code, and no errors occurred. The function square(num) is correctly defined // Finally, correct the code to fix the problem diff --git a/Sprint-2/2-mandatory-debug/0.js b/Sprint-2/2-mandatory-debug/0.js index b27511b41..00a205147 100644 --- a/Sprint-2/2-mandatory-debug/0.js +++ b/Sprint-2/2-mandatory-debug/0.js @@ -1,6 +1,6 @@ // Predict and explain first... -// =============> write your prediction here +// =============> The program will throw an error when it runs. Specifically, it will result in a SyntaxError or ReferenceError function multiply(a, b) { console.log(a * b); @@ -8,7 +8,13 @@ function multiply(a, b) { console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`); -// =============> write your explanation here +// =============> The error happens because the function multiply(a, b) doesn't return a value, so the console.log() statement is trying to print undefined to the console. This is because the function doesn't have a return statement, so it returns undefined by default. // 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..eed14c0ba 100644 --- a/Sprint-2/2-mandatory-debug/1.js +++ b/Sprint-2/2-mandatory-debug/1.js @@ -1,6 +1,5 @@ // Predict and explain first... -// =============> write your prediction here - +// =============> The code is trying to return the sum of two numbers, but it will throw an error when it runs. Specifically, it will result in a SyntaxError or ReferenceError function sum(a, b) { return; a + b; @@ -8,6 +7,11 @@ function sum(a, b) { console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); -// =============> write your explanation here +// =============> The code is trying to sum two numbers, but it will throw an error when it runs. Specifically, it will result in a SyntaxError or ReferenceError. This is because the return statement is followed by a semicolon, which ends the statement and prevents the code after it from running. This means that the function doesn't return a value, so it returns undefined by default. // Finally, correct the code to fix the problem -// =============> write your new code here +// =============> function sum(a, b) { + return a + b; // Ensure the return statement is on the same line as the expression +//} + +console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); + diff --git a/Sprint-2/4-mandatory-interpret/time-format.js b/Sprint-2/4-mandatory-interpret/time-format.js index 7c98eb0e8..05a48c053 100644 --- a/Sprint-2/4-mandatory-interpret/time-format.js +++ b/Sprint-2/4-mandatory-interpret/time-format.js @@ -17,18 +17,27 @@ function formatTimeDisplay(seconds) { // Questions // a) When formatTimeDisplay is called how many times will pad be called? -// =============> write your answer here +// =============> The function pad(num) is called three times each time formatTimeDisplay(seconds) is executed. + + // 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 +// =============> When pad is called for the first time inside formatTimeDisplay(seconds), the value assigned to num is totalHours. // c) What is the return value of pad is called for the first time? -// =============> write your answer here +// =============> The return value of the first call to pad will be the result of totalHours.toString().padStart(2, "0"). // 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 assigned to num when pad is called for the last time is the remainder of seconds divided by 60. // 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 assigned when pad is called for the last time is 01. +// The last time pad(num) is called, num is assigned the value of remainingSeconds. +// This happens because pad is called three times in this order: + +//First call → pad(totalHours) +//Second call → pad(remainingMinutes) +//Third (last) call → pad(remainingSeconds) + diff --git a/Sprint-2/5-stretch-extend/format-time.js b/Sprint-2/5-stretch-extend/format-time.js index 32a32e66b..bc5403792 100644 --- a/Sprint-2/5-stretch-extend/format-time.js +++ b/Sprint-2/5-stretch-extend/format-time.js @@ -23,3 +23,37 @@ console.assert( currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}` ); + +//=========>function formatAs12HourClock(time) { + let hours = Number(time.slice(0, 2)); + let minutes = time.slice(3, 5); + let period = hours >= 12 ? "pm" : "am"; + + // Convert 24-hour format to 12-hour format + if (hours === 0) { + hours = 12; // Midnight case + } else if (hours > 12) { + hours -= 12; + } + + return `${hours}:${minutes} ${period}`; +} + +// Test Cases +const testCases = [ + { input: "00:00", expected: "12:00 am" }, // Midnight + { input: "12:00", expected: "12:00 pm" }, // Noon + { input: "08:00", expected: "8:00 am" }, // Standard AM case + { input: "13:00", expected: "1:00 pm" }, // Standard PM case + { input: "23:00", expected: "11:00 pm" }, // Late PM case + { input: "01:00", expected: "1:00 am" }, // Early morning case +]; + +// Run tests +testCases.forEach(({ input, expected }) => { + const output = formatAs12HourClock(input); + console.assert( + output === expected, + `Test failed for input ${input}. Output: ${output}, Expected: ${expected}` + ); +}); diff --git a/Sprint-3/1-key-implement/1-get-angle-type.js b/Sprint-3/1-key-implement/1-get-angle-type.js index 08d1f0cba..847665a18 100644 --- a/Sprint-3/1-key-implement/1-get-angle-type.js +++ b/Sprint-3/1-key-implement/1-get-angle-type.js @@ -43,14 +43,17 @@ assertEquals(acute, "Acute angle"); // When the angle is greater than 90 degrees and less than 180 degrees, // Then the function should return "Obtuse angle" const obtuse = getAngleType(120); -// ====> write your test here, and then add a line to pass the test in the function above +// ====> const obtuse = getAngleType(120); +//assertEquals(obtuse, "Obtuse angle"); // Case 4: Identify Straight Angles: // When the angle is exactly 180 degrees, // Then the function should return "Straight angle" -// ====> write your test here, and then add a line to pass the test in the function above +// ====> const straight = getAngleType(180); +//assertEquals(straight, "Straight angle"); // Case 5: Identify Reflex Angles: // When the angle is greater than 180 degrees and less than 360 degrees, // Then the function should return "Reflex angle" -// ====> write your test here, and then add a line to pass the test in the function above \ No newline at end of file +// ====> const straight = getAngleType(180); +//assertEquals(straight, "Straight angle"); \ No newline at end of file diff --git a/Sprint-3/1-key-implement/2-is-proper-fraction.js b/Sprint-3/1-key-implement/2-is-proper-fraction.js index 91583e941..9320f96c1 100644 --- a/Sprint-3/1-key-implement/2-is-proper-fraction.js +++ b/Sprint-3/1-key-implement/2-is-proper-fraction.js @@ -40,14 +40,33 @@ assertEquals(improperFraction, false); // target output: true // Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true. const negativeFraction = isProperFraction(-4, 7); -// ====> complete with your assertion +// ====> const equalFraction = isProperFraction(3, 3); +assertEquals(equalFraction, false); + // Equal Numerator and Denominator check: // Input: numerator = 3, denominator = 3 // target output: false // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. const equalFraction = isProperFraction(3, 3); -// ====> complete with your assertion +// ====> function isProperFraction(numerator, denominator) { + if (denominator === 0) return "Undefined (denominator cannot be zero)"; // Handle division by zero + return numerator < denominator; +} + +// Helper function for assertions +//function assertEquals(actualOutput, targetOutput) { + //console.assert( + // actualOutput === targetOutput, + // `Expected ${actualOutput} to equal ${targetOutput}` + // ); +//} + +// Test for Equal Numerator and Denominator +//const equalFraction = isProperFraction(3, 3); +//assertEquals(equalFraction, false); + // Stretch: // What other scenarios could you test for? +//====>Test cases should include zero denominators, zero numerators, negative fractions, and improper fractions to ensure isProperFraction handles all scenarios correctly. \ No newline at end of file diff --git a/Sprint-3/1-key-implement/3-get-card-value.js b/Sprint-3/1-key-implement/3-get-card-value.js index aa1cc9f90..868bcb0a2 100644 --- a/Sprint-3/1-key-implement/3-get-card-value.js +++ b/Sprint-3/1-key-implement/3-get-card-value.js @@ -33,7 +33,11 @@ assertEquals(aceofSpades, 11); // When the function is called with such a card, // Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5). const fiveofHearts = getCardValue("5♥"); -// ====> write your test here, and then add a line to pass the test in the function above +// ====> function getCardValue(card) { + // const rank = card[0]; // Extracts the first character (rank) from the card string + // if (!isNaN(rank)) return Number(rank); +//} + // Handle Face Cards (J, Q, K): // Given a card with a rank of "10," "J," "Q," or "K", diff --git a/Sprint-3/3-mandatory-practice/implement/count.js b/Sprint-3/3-mandatory-practice/implement/count.js index fce249650..77585cc03 100644 --- a/Sprint-3/3-mandatory-practice/implement/count.js +++ b/Sprint-3/3-mandatory-practice/implement/count.js @@ -2,4 +2,10 @@ function countChar(stringOfCharacters, findCharacter) { return 5 } -module.exports = countChar; \ No newline at end of file +module.exports = countChar; + +//=====> function countChar(stringOfCharacters, findCharacter) { + return stringOfCharacters.split(findCharacter).length - 1; +//} + +//module.exports = countChar;