diff --git a/week-3/debug/format-as-12-hours.js b/week-3/debug/format-as-12-hours.js index 56b83a5b..7af3d2e1 100644 --- a/week-3/debug/format-as-12-hours.js +++ b/week-3/debug/format-as-12-hours.js @@ -24,7 +24,62 @@ console.assert( ); // formatAs12HourClock currently has a 🐛 +const result = formatAs12HourClock("17:42"); +asserts.strictEqual(result, "5:42 pm", "conversion to 12-hour clock is incorrect"); + // a) Write an assertion to check the return value of formatAs12HourClock when it is called with an input "17:42" + +const result = formatAs12HourClock("17:42"); +console.assert( + result === "5:42 pm", + "Conversion to 12-hour clock is incorrect" +); + + + // b) Check the assertion output and explain what the bug is +The bug is in the formatAs12HourClock function. +It seems that the minutes are not being considered, and the function always +returns ":00 pm" or ":00 am". + Additionally, it doesn't handle noon and midnight correctly. + + + + // c) Now fix the bug and re-run all your assertions + +function formatAs12HourClock(time) { + const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3); + const period = hours >= 12 ? "pm" : "am"; + const formattedHours = hours % 12 || 12; // Handle 12:00 as 12 pm + + return `${formattedHours}:${minutes} ${period}`; +} + +// Re-run assertions + +const currentOutput = formatAs12HourClock("08:00"); +const targetOutput = "8:00 am"; +console.assert( + currentOutput === targetOutput, + "current output: %s, target output: %s", + currentOutput, + targetOutput +); + +const currentOutput2 = formatAs12HourClock("23:00"); +const targetOutput2 = "11:00 pm"; +console.assert( + currentOutput2 === targetOutput2, + "current output: %s, target output: %s", + currentOutput2, + targetOutput2 +); + +const result = formatAs12HourClock("17:42"); +console.assert( + result === "5:42 pm", + "Conversion to 12-hour clock is incorrect" +); diff --git a/week-3/implement/get-angle-type.js b/week-3/implement/get-angle-type.js index 9dd3a210..7efc6584 100644 --- a/week-3/implement/get-angle-type.js +++ b/week-3/implement/get-angle-type.js @@ -21,3 +21,61 @@ // Identify Reflex Angles: // When the angle is greater than 180 degrees and less than 360 degrees, // Then the function should return "Reflex angle" +// Function to get the type of angle +function getAngleType(angle) { + if (angle === 90) { + return "right angel"; + } else if (angle < 90) { + return "Acute angle"; + } else if (angle === 180) { + return "straight angle"; + } else if (angle > 90 && angle < 180) { + return "obtuse angle"; + } else if (angle > 180 && angle < 360) { + return "Reflex angle"; + } else { + return "Invalid angle"; + } +} + +// Tests for each acceptance criterion + +// Test for Right Angle +const rightAngleTest = getAngleType(90); +console.assert( + rightAngleTest === "Right angel", + `Expected "Right angle" but got "${rightAngleTest}"` +); + +// Test for Acute Angle +const acuteAngleTest = getAngleType(60); +console.assert( + acuteAngleTest === "Acute angle", + `Expected "Acute angle" but got "${acuteAngleTest}"` +); + + +// Test for Obtuse Angle +const obtuseAngleTest = getAngleType(120); +console.assert( + obtuseAngleTest === "Obtuse angle", + `Expected "Obtuse angle" but got "${obtuseAngleTest}"` +); + + +// Test for Straight Angle +const straightAngleTest = getAngleType(180); +console.assert( + straightAngleTest === "straight angle", + `Expected "Straight angle" but got "${straightAngleTest}"` +); + + +// Test for Reflex Angle +const reflexAngleTest = getAngleType(270); +console.assert( + reflexAngleTest === "Reflex angle", + `Expected "Reflex angle" but got "${reflexAngleTest}"` +); + + diff --git a/week-3/implement/get-card-value.js b/week-3/implement/get-card-value.js index 0dd74fbc..40b0617e 100644 --- a/week-3/implement/get-card-value.js +++ b/week-3/implement/get-card-value.js @@ -29,3 +29,53 @@ // Given a card with an invalid rank (neither a number nor a recognized face card), // When the function is called with such a card, // Then it should throw an error indicating "Invalid card rank." + +function getCardValue(card) { + const rank = card.slice(0, -1); +} + +// Extracting the rank part of the card string +if (/[2-9]|10/.test(rank)) { + // Handle Number Cards (2-10) + return parseInt(rank, 10); +} else if (/^[JQK]$/.test(rank)) { + // Handle Face Cards (J, Q, K) + return 10; +} else if (rank === 'A') { + // Handle Ace (A) + return 11; +} else { + // Handle Invalid Cards + throw new Error("Invalid card rank."); +} + + +// Assertions for each acceptance criterion + +// Test for Numeric Card +const numericCardTest = getCardValue("5♠"); +console.assert( + numericCardTest === 5, + `Expected 5 for numeric card, but got ${numericCardTest}` +); + +// Test for Face Card +const faceCardTest = getCardValue("Q♦"); +console.assert( + faceCardTest === 10, + `Expected 10 for face card, but got ${faceCardTest}` +); + +// Test for Ace +const aceCardTest = getCardValue("A♥"); +console.assert( + aceCardTest === 11, + `Expected 11 for Ace, but got ${aceCardTest}` +); + +// Test for Invalid Card +console.assert( + () => getCardValue("X♣"), + new Error("Invalid card rank."), + "Function did not throw expected error for invalid card" +); diff --git a/week-3/implement/is-proper-fraction.js b/week-3/implement/is-proper-fraction.js index 31da32b5..77ea8c6e 100644 --- a/week-3/implement/is-proper-fraction.js +++ b/week-3/implement/is-proper-fraction.js @@ -33,3 +33,47 @@ // Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false. // These acceptance criteria cover a range of scenarios to ensure that the isProperFraction function handles both proper and improper fractions correctly and handles potential errors such as a zero denominator. +function isProperFraction(numerator, denominator) { + if (denominator === 0) { + throw new Error("Denominator cannot be zero"); + } + + return Math.abs(numerator) < Math.abs(denominator); +} + +// Assertions for each acceptance criterion + +// Test for Proper Fraction +const properFractionTest = isProperFraction(2, 3); +console.assert( + properFractionTest === true, + "Expected true for a proper fraction, but got false" +); + +// Test for Improper Fraction +const improperFractionTest = isProperFraction(5, 2); +console.assert( + improperFractionTest === false, + "Expected false for an improper fraction, but got true" +); + +// Test for Zero Denominator +console.assert( + () => isProperFraction(3, 0), + new Error("Denominator cannot be zero"), + "Function did not throw expected error for zero denominator" +); + +// Test for Negative Fraction +const negativeFractionTest = isProperFraction(-4, 7); +console.assert( + negativeFractionTest === true, + "Expected true for a negative proper fraction, but got false" +); + +// Test for Equal Numerator and Denominator +const equalNumeratorDenominatorTest = isProperFraction(3, 3); +console.assert( + equalNumeratorDenominatorTest === false, + "Expected false for equal numerator and denominator, but got true" +); diff --git a/week-3/implement/is-valid-triangle.js b/week-3/implement/is-valid-triangle.js index 7b22836b..3f9cde1c 100644 --- a/week-3/implement/is-valid-triangle.js +++ b/week-3/implement/is-valid-triangle.js @@ -38,3 +38,42 @@ // Then it should return true because the input forms a valid triangle. // This specification outlines the behavior of the isValidTriangle function for different input scenarios, ensuring it properly checks for invalid side lengths and whether they form a valid triangle according to the Triangle Inequality Theorem. +function isValidTriangle(a, b, c) { + if (a <= 0 || b <= 0 || c <= 0) { + return false; + // Check for zero or negative side lengths + } + + return a + b > c && a + c > b && b + c > a; + // Check the Triangle Inequality +} + +// Assertions for each acceptance criterion + +// Test for Invalid Triangle (violates Triangle Inequality) +const invalidTriangleTest = isValidTriangle(3, 3, 7); +console.assert( + invalidTriangleTest === false, + "Expected false for an invalid triangle, but got true" +); + +// Test for Invalid Triangle (negative side length) +const negativeSideLengthTest = isValidTriangle(3, -4, 5); +console.assert( + negativeSideLengthTest === false, + "Expected false for negative side length, but got true" +); + +// Test for Invalid Triangle (zero side length) +const zeroSideLengthTest = isValidTriangle(0, 4, 5); +console.assert( + zeroSideLengthTest === false, + "Expected false for zero side length, but got true" +); + +// Test for Valid Triangle +const validTriangleTest = isValidTriangle(3, 4, 5); +console.assert( + validTriangleTest === true, + "Expected true for a valid triangle, but got false" +); diff --git a/week-3/refactor/format-as-12-hours.js b/week-3/refactor/format-as-12-hours.js index 41603122..83e15170 100644 --- a/week-3/refactor/format-as-12-hours.js +++ b/week-3/refactor/format-as-12-hours.js @@ -4,3 +4,25 @@ // Store this expression in a variable and reference it twice in the function in the correct place // Explain why it makes more sense to store this expression in a variable + +function formatAs12HourClock(time) { + const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3); + + // Store the expression in a variable for better readability + const formattedHours = hours % 12 || 12; + const period = hours >= 12 ? "pm" : "am"; + + return `${formattedHours}:${minutes} ${period}`; +} + + + //By storing the result of Number(time.slice(0, 2)) in the hours variable, + //we avoid recalculating it multiple times within the function. + //This not only improves readability but also enhances performance by eliminating redundant computations. + //Storing the result in a variable makes the code cleaner, more concise, and easier to maintain. + + + + + diff --git a/week-3/refactor/is-vowel.js b/week-3/refactor/is-vowel.js index db675d2b..3a709ebf 100644 --- a/week-3/refactor/is-vowel.js +++ b/week-3/refactor/is-vowel.js @@ -40,3 +40,4 @@ console.assert( currentOutput3, targetOutput3 ); + diff --git a/week-3/stretch/rotate-char.js b/week-3/stretch/rotate-char.js index f274ce1e..69018e66 100644 --- a/week-3/stretch/rotate-char.js +++ b/week-3/stretch/rotate-char.js @@ -40,3 +40,28 @@ console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter) // Then it should correctly rotate the character by shift positions within the alphabet while handling the wraparound, // And the function should return the rotated character as a string (e.g., 'z' rotated by 3 should become 'c', 'Z' rotated by 3 should become 'C'). console.log(rotateCharacter("z", 1)); // Output: "a" (unchanged, not a letter) + +function rotateCharacter(char, shift) { + // Check if the character is a lowercase letter + if (char >= "a" && char <= "z") { + const rotatedCharCode = ((char.charCodeAt(0) - "a".charCodeAt(0) + shift) % 26 + 26) % 26 + "a".charCodeAt(0); + return String.fromCharCode(rotatedCharCode); + } + + // Check if the character is an uppercase letter + if (char >= "A" && char <= "Z") { + const rotatedCharCode = ((char.charCodeAt(0) - "A".charCodeAt(0) + shift) % 26 + 26) % 26 + "A".charCodeAt(0); + return String.fromCharCode(rotatedCharCode); + } + + // If the character is not a letter, return it unchanged + return char; +} + +// Test cases +console.log(rotateCharacter("a", 3)); // Output: "d" +console.log(rotateCharacter("f", 1)); // Output: "g" +console.log(rotateCharacter("A", 3)); // Output: "D" +console.log(rotateCharacter("F", 1)); // Output: "G" +console.log(rotateCharacter("7", 5)); // Output: "7" (unchanged, not a letter) +console.log(rotateCharacter("z", 1)); // Output: "a"