diff --git a/week-3/debug/format-as-12-hours.js b/week-3/debug/format-as-12-hours.js index 56b83a5b..8c222e17 100644 --- a/week-3/debug/format-as-12-hours.js +++ b/week-3/debug/format-as-12-hours.js @@ -1,6 +1,6 @@ function formatAs12HourClock(time) { if (Number(time.slice(0, 2)) > 12) { - return `${Number(time.slice(0, 2)) - 12}:00 pm`; + return `${Number(time.slice(0, 2)) - 12}:${time.slice(3, 5)} pm`; } return `${time} am`; } @@ -23,6 +23,15 @@ console.assert( targetOutput2 ); +const currentOutput3 = formatAs12HourClock("17:42"); +const targetOutput3 = "5:42 pm"; +console.assert( + currentOutput3 === targetOutput3, + "current output: %s, target output: %s", + currentOutput3, + targetOutput3 +); + // formatAs12HourClock currently has a 🐛 // a) Write an assertion to check the return value of formatAs12HourClock when it is called with an input "17:42" diff --git a/week-3/implement/get-angle-type.js b/week-3/implement/get-angle-type.js index 9dd3a210..ffff7584 100644 --- a/week-3/implement/get-angle-type.js +++ b/week-3/implement/get-angle-type.js @@ -21,3 +21,27 @@ // Identify Reflex Angles: // When the angle is greater than 180 degrees and less than 360 degrees, // Then the function should return "Reflex angle" + +function getAngleType(angle) { + let angleType = ""; + + if (angle < 90) { + angleType = "Acute"; + } else if (angle === 90) { + angleType = "Right"; + } else if (angle < 180) { + angleType = "Obtuse"; + } else if (angle === 180) { + angleType = "Straight"; + } else if (angle > 180) { + angleType = "Reflex"; + } + + return `${angleType} angle`; +} + +console.assert(getAngleType(50) === "Acute angle", "1"); +console.assert(getAngleType(90) === "Right angle", "2"); +console.assert(getAngleType(170) === "Obtuse angle", "3"); +console.assert(getAngleType(180) === "Straight angle", "4"); +console.assert(getAngleType(290) === "Reflex angle", "5"); diff --git a/week-3/implement/get-card-value.js b/week-3/implement/get-card-value.js index 0dd74fbc..caa61b31 100644 --- a/week-3/implement/get-card-value.js +++ b/week-3/implement/get-card-value.js @@ -29,3 +29,47 @@ // 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(cards) { + const number = [ + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "10", + "J", + "Q", + "K", + "A", + ]; + const type = ["♣", "♦", "♥", "♠"]; + let num = cards.slice(0, -1); + + if (number.includes(num) && type.includes(cards.slice(-1))) { + if (num === "A") { + result = 11; + } else if (num === "J") { + result = 10; + } else if (num === "Q") { + result = 10; + } else if (num === "K") { + result = 10; + } else { + result = Number(num); + } + return result; + } else { + return "Invalid card rank."; + } +} + +console.assert(getCardValue("A♠") === 11, "11"); +console.assert(getCardValue("5♠") === 5, "5"); +console.assert(getCardValue("J♥") === 10, "10j"); +console.assert(getCardValue("Q♠") === 10, "10q"); +console.assert(getCardValue("K♠") === 10, "10k"); +console.assert(getCardValue("2a") === "Invalid card rank.", "6"); diff --git a/week-3/implement/is-proper-fraction.js b/week-3/implement/is-proper-fraction.js index 31da32b5..fbe45a45 100644 --- a/week-3/implement/is-proper-fraction.js +++ b/week-3/implement/is-proper-fraction.js @@ -5,31 +5,60 @@ // Fractions: https://www.bbc.co.uk/bitesize/topics/zt9n6g8/articles/zjxpp4j // Written here like this: 1/2 == Numerator/Denominator +function isProperFraction(numerator, denominator) { + const fraction = numerator / denominator; + if (denominator === 0) { + return "Error (Denominator cannot be zero)"; + } else if ( + (fraction < 1 && fraction > 0) || + (fraction > -1 && fraction < 0) + ) { + return true; + } else { + return false; + } +} + // Acceptance criteria: // Proper Fraction check: // Input: numerator = 2, denominator = 3 // target output: true // Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true. - +console.assert( + isProperFraction(2, 3) === true, + "proper fraction check failed, input 2, 3" +); // Improper Fraction check: // Input: numerator = 5, denominator = 2 // target output: false // Explanation: The fraction 5/2 is an improper fraction, where the numerator is greater than or equal to the denominator. The function should return false. - +console.assert( + isProperFraction(5, 2) === false, + "improper fraction check failed, input 5, 2" +); // Zero Denominator check: // Input: numerator = 3, denominator = 0 // No target output: Error (Denominator cannot be zero) // Explanation: The function should throw an error when the denominator is zero, as it's not a valid fraction. - +console.assert( + isProperFraction(3, 0) === "Error (Denominator cannot be zero)", + "zero dominator check failed, input 3, 0" +); // Negative Fraction check: // Input: numerator = -4, denominator = 7 // 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. - +console.assert( + isProperFraction(-4, 7) === true, + "negative fraction check failed, input -4, 7" +); // 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. - +console.assert( + isProperFraction(3, 3) === false, + "equal numerator and denominator check failed, input 3, 3" +); // 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. diff --git a/week-3/implement/is-valid-triangle.js b/week-3/implement/is-valid-triangle.js index 7b22836b..6d8d9f6d 100644 --- a/week-3/implement/is-valid-triangle.js +++ b/week-3/implement/is-valid-triangle.js @@ -1,4 +1,15 @@ // Implement a function isValidTriangle +function isValidTriangle(a, b, c) { + if (a <= 0 && b <= 0 && c <= 0) { + return false; + } else { + if (a + b > c && b + c > a && c + a > b) { + return true; + } else { + return false; + } + } +} // 🗝️ Terms // the Triangle Inequality says: the sum of any two sides is always greater than the third side. @@ -25,16 +36,26 @@ // Given the side lengths a, b, and c, // When the sum of any two side lengths is less than or equal to the length of the third side (i.e., a + b <= c, a + c <= b, b + c <= a), // Then it should return false because these conditions violate the Triangle Inequality, which states that the sum of the lengths of any two sides of a triangle must be greater than the length of the third side. - +console.assert( + isValidTriangle(3, 3, 6) === false && isValidTriangle(2, 3, 6) === false, + "When the sum of any two side lengths is less than or equal to the length of the third side" +); // scenario: invalid triangle // Check for Valid Input: // Given the sides a, b, and c, // When any of the sides are less than or equal to zero, // Then it should return false because a triangle cannot have zero or negative side lengths. +console.assert( + isValidTriangle(3, 3, -3) === false && isValidTriangle(1, 1, 0) === false, + "When any of the sides are less than or equal to zero" +); // scenario: valid triangle // Given valid side lengths where the sum of any two sides is greater than the third side, // When the function is called with these values as input, // Then it should return true because the input forms a valid triangle. - +console.assert( + isValidTriangle(3, 3, 3) === true && isValidTriangle(3, 4, 5) === true, + "When valid side lengths where the sum of any two sides is greater than the third side" +); // 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. diff --git a/week-3/refactor/format-as-12-hours.js b/week-3/refactor/format-as-12-hours.js index 41603122..dd49b649 100644 --- a/week-3/refactor/format-as-12-hours.js +++ b/week-3/refactor/format-as-12-hours.js @@ -4,3 +4,38 @@ // 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 hour = time.slice(0, 2); + if (Number(hour) > 12) { + return `${Number(hour) - 12}:${time.slice(3, 5)} pm`; + } + return `${time} am`; +} + +const currentOutput = formatAs12HourClock("08:00"); +const targetOutput = "08: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 currentOutput3 = formatAs12HourClock("17:42"); +const targetOutput3 = "5:42 pm"; +console.assert( + currentOutput3 === targetOutput3, + "current output: %s, target output: %s", + currentOutput3, + targetOutput3 +); diff --git a/week-3/stretch/rotate-char.js b/week-3/stretch/rotate-char.js index f274ce1e..bfb526ca 100644 --- a/week-3/stretch/rotate-char.js +++ b/week-3/stretch/rotate-char.js @@ -8,6 +8,74 @@ // Acceptance criteria: +function rotateCharacter(char, shift) { + const alphabetLower = [ + "a", + "b", + "c", + "d", + "e", + "f", + "g", + "h", + "i", + "j", + "k", + "l", + "m", + "n", + "o", + "p", + "q", + "r", + "s", + "t", + "u", + "v", + "w", + "x", + "y", + "z", + ]; + const alphabetUpper = [ + "A", + "B", + "C", + "D", + "E", + "F", + "G", + "H", + "I", + "J", + "K", + "L", + "M", + "N", + "O", + "P", + "Q", + "R", + "S", + "T", + "U", + "V", + "W", + "X", + "Y", + "Z", + ]; + if (alphabetLower.includes(char)) { + const value = (alphabetLower.indexOf(char) + shift) % 26; + return alphabetLower[value]; + } else if (alphabetUpper.includes(char)) { + const value = (alphabetUpper.indexOf(char) + shift) % 26; + return alphabetUpper[value]; + } else { + return char; + } +} + // Given a character (char) and a shift value (shift), // When the function rotateCharacter is called with these inputs, // Then it should: