diff --git a/week-3/debug/format-as-12-hours.js b/week-3/debug/format-as-12-hours.js index 56b83a5b..5c8b61bc 100644 --- a/week-3/debug/format-as-12-hours.js +++ b/week-3/debug/format-as-12-hours.js @@ -1,8 +1,25 @@ +// function formatAs12HourClock(time) { +// if (Number(time.slice(0, 2)) > 12) { +// return `${Number(time.slice(0, 2)) - 12}:${Number(time.slice(3))} pm`; +// } +// return `${time} am`; +// } + function formatAs12HourClock(time) { - if (Number(time.slice(0, 2)) > 12) { - return `${Number(time.slice(0, 2)) - 12}:00 pm`; + const hours = Number(time.slice(0, 2)); + const minutes = Number(time.slice(3)); + + if (hours >= 12) { + const formattedHoursAfternoon = + hours === 12 ? hours : hours - 12 < 10 ? `0${hours - 12}` : hours - 12; + const formattedMinutesAfternoon = minutes < 10 ? `0${minutes}` : minutes; + return `${formattedHoursAfternoon}:${formattedMinutesAfternoon} pm`; } - return `${time} am`; + + const formattedHoursMorning = + hours === 0 ? hours : hours < 10 ? `0${hours}` : hours; + const formattedMinutesMorning = minutes < 10 ? `0${minutes}` : minutes; + return `${formattedHoursMorning}:${formattedMinutesMorning} am`; } const currentOutput = formatAs12HourClock("08:00"); @@ -28,3 +45,48 @@ console.assert( // a) Write an assertion to check the return value of formatAs12HourClock when it is called with an input "17:42" // b) Check the assertion output and explain what the bug is // c) Now fix the bug and re-run all your assertions + +const currentOutput3 = formatAs12HourClock("17:42"); +const targetOutput3 = "05:42 pm"; + +console.assert( + currentOutput3 === targetOutput3, + "current output: %s, target output: %s", + currentOutput3, + targetOutput3 +); + +// Answer: + +// b) When the hours part are greater than 12, the function adds "00" to show the minutes part which is not correct. +// Also the function does not appropriately work to display the four digit number formatting + +const currentOutput4 = formatAs12HourClock("00:00"); +const targetOutput4 = "00:00 am"; + +console.assert( + currentOutput3 === targetOutput3, + "current output: %s, target output: %s", + currentOutput3, + targetOutput3 +); + +const currentOutput5 = formatAs12HourClock("01:02"); +const targetOutput5 = "01:02 am"; + +console.assert( + currentOutput3 === targetOutput3, + "current output: %s, target output: %s", + currentOutput3, + targetOutput3 +); + +const currentOutput6 = formatAs12HourClock("23:23"); +const targetOutput6 = "11:23 pm"; + +console.assert( + currentOutput3 === targetOutput3, + "current output: %s, target output: %s", + currentOutput3, + targetOutput3 +); diff --git a/week-3/implement/get-angle-type.js b/week-3/implement/get-angle-type.js index 9dd3a210..b62e3ab8 100644 --- a/week-3/implement/get-angle-type.js +++ b/week-3/implement/get-angle-type.js @@ -21,3 +21,26 @@ // 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) { + if (angle === 90) { + return "Right angle"; + } else if (angle < 90) { + return "Acute angle"; + } else if (angle > 90 && angle < 180) { + return "Obtuse angle"; + } else if (angle === 180) { + return "Straight angle"; + } else if (angle > 180 && angle < 360) { + return "Reflex angle"; + } else { + return "Invalid angle"; + } +} + +console.assert(getAngleType(90) === "Right angle"); +console.assert(getAngleType(45) === "Acute angle"); +console.assert(getAngleType(120) === "Obtuse angle"); +console.assert(getAngleType(180) === "Straight angle"); +console.assert(getAngleType(270) === "Reflex angle"); +console.assert(getAngleType(360) === "Invalid angle"); diff --git a/week-3/implement/get-card-value.js b/week-3/implement/get-card-value.js index 0dd74fbc..64f5038a 100644 --- a/week-3/implement/get-card-value.js +++ b/week-3/implement/get-card-value.js @@ -29,3 +29,24 @@ // 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); + + if (Number(rank) >= 2 && Number(rank) <= 10) { + return Number(rank); + } else if (rank === "J" || rank === "Q" || rank === "K") { + return 10; + } else if (rank === "A") { + return 11; + } else { + return "Invalid card rank"; + } +} + +console.assert(getCardValue("5♠") === 5, "Test 1"); +console.assert(getCardValue("J♦") === 10, "Test 2"); +console.assert(getCardValue("A♥") === 11, "Test 3"); +console.assert(getCardValue("1♣") === "Invalid card rank", "Test 4"); +console.assert(getCardValue("11♣") === "Invalid card rank", "Test 5"); +console.assert(getCardValue("M♣") === "Invalid card rank", "Test 6"); diff --git a/week-3/implement/is-proper-fraction.js b/week-3/implement/is-proper-fraction.js index 31da32b5..1882c9a0 100644 --- a/week-3/implement/is-proper-fraction.js +++ b/week-3/implement/is-proper-fraction.js @@ -33,3 +33,28 @@ // 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"); + } else { + if (Math.abs(numerator) < Math.abs(denominator)) { + return true; + } else { + return false; + } + } +} + +console.assert(isProperFraction(2, 3) === true, "Test 1"); +console.assert(isProperFraction(5, 2) === false, "Test 2"); +console.assert(isProperFraction(-4, 7) === true, "Test 3"); +console.assert(isProperFraction(3, 3) === false, "Test 4"); +console.assert(() => isProperFraction(3, 0), "Test 5"); + +console.log(isProperFraction(-3, 1)); +console.log(isProperFraction(3, 11)); +console.log(isProperFraction(1, 1)); +console.log(isProperFraction(2, -21)); +console.log(isProperFraction(-31, -1)); +console.log(isProperFraction(-3, 0)); diff --git a/week-3/implement/is-valid-triangle.js b/week-3/implement/is-valid-triangle.js index 7b22836b..366d0aa5 100644 --- a/week-3/implement/is-valid-triangle.js +++ b/week-3/implement/is-valid-triangle.js @@ -38,3 +38,28 @@ // 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(sideA, sideB, sideC) { + if ( + typeof sideA !== `number` || + typeof sideB !== `number` || + typeof sideC !== `number` || + sideA <= 0 || + sideB <= 0 || + sideC <= 0 || + sideA + sideB <= sideC || + sideA + sideC <= sideB || + sideB + sideC <= sideA + ) { + return false; + } else { + return true; + } +} + +console.log(isValidTriangle(3, 3, 3)); +console.log(isValidTriangle(3, 3, 5)); +console.log(isValidTriangle(3, 4, 5)); +console.log(isValidTriangle(1, 1, 3)); +console.log(isValidTriangle(-2, 2, 5)); +console.log(isValidTriangle(7, "apple", 5)); diff --git a/week-3/refactor/format-as-12-hours.js b/week-3/refactor/format-as-12-hours.js index 41603122..e8ca13f1 100644 --- a/week-3/refactor/format-as-12-hours.js +++ b/week-3/refactor/format-as-12-hours.js @@ -4,3 +4,61 @@ // 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 + +// Given code in the file in debug folder: + +// function formatAs12HourClock(time) { +// if (Number(time.slice(0, 2)) > 12) { +// return `${Number(time.slice(0, 2)) - 12}:00 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 +// ); + +// Improved and fixed version of it: + +function formatAs12HourClock(time) { + const hours = Number(time.slice(0, 2)); + const minutes = Number(time.slice(3)); + + if (hours >= 12) { + const formattedHoursAfternoon = + hours === 12 ? hours : hours - 12 < 10 ? `0${hours - 12}` : hours - 12; + const formattedMinutesAfternoon = minutes < 10 ? `0${minutes}` : minutes; + return `${formattedHoursAfternoon}:${formattedMinutesAfternoon} pm`; + } else { + const formattedHoursMorning = + hours === 0 ? hours : hours < 10 ? `0${hours}` : hours; + const formattedMinutesMorning = minutes < 10 ? `0${minutes}` : minutes; + return `${formattedHoursMorning}:${formattedMinutesMorning} am`; + } +} + +// Explanation: + +// * USE OF VARIABLES: The introduction of hours and minutes variables makes the code more readable, +// and the variable names clearly convey their purpose. + +// * TERNARY OPERATOR: The use of ternary operators for conditional expressions, +// simplifies the logic and reduces the need for nested if statements. + +// * CONSISTENT FORMATTING: The consistent formatting of hours and minutes with leading zeros ensures that the output follows a standard format. + +// * CLARITY IN LOGIC: The logic for handling afternoon and morning hours is clear and concise. diff --git a/week-3/refactor/is-vowel.js b/week-3/refactor/is-vowel.js index db675d2b..80878c6a 100644 --- a/week-3/refactor/is-vowel.js +++ b/week-3/refactor/is-vowel.js @@ -3,7 +3,6 @@ function isVowel(letter) { letter === "a" || letter === "e" || letter === "i" || - letter === "i" || letter === "o" || letter === "u" ); @@ -11,7 +10,17 @@ function isVowel(letter) { // here is an implementation of isVowel - this function checks if a letter is a vowel -console.log("case: letter a..."); +let letter = "a"; + +console.log( + "It is " + + (isVowel(letter) ? "true" : "false") + + " that letter '" + + letter + + "' is a vowel." +); + +// console.log("case: letter a..."); const currentOutput = isVowel("a"); const targetOutput = true; console.assert( @@ -21,7 +30,17 @@ console.assert( targetOutput ); -console.log("case: letter e..."); +letter = "e"; + +console.log( + "It is " + + (isVowel(letter) ? "true" : "false") + + " that letter '" + + letter + + "' is a vowel." +); + +// console.log("case: letter e..."); const currentOutput2 = isVowel("e"); const targetOutput2 = true; console.assert( @@ -31,7 +50,17 @@ console.assert( targetOutput2 ); -console.log("case: letter k..."); +letter = "k"; + +console.log( + "It is " + + (isVowel(letter) ? "true" : "false") + + " that letter '" + + letter + + "' is a vowel." +); + +// console.log("case: letter k..."); const currentOutput3 = isVowel("k"); const targetOutput3 = false; console.assert(