diff --git a/week-3/debug/format-as-12-hours.js b/week-3/debug/format-as-12-hours.js index 56b83a5b..8c6d0f80 100644 --- a/week-3/debug/format-as-12-hours.js +++ b/week-3/debug/format-as-12-hours.js @@ -1,6 +1,9 @@ function formatAs12HourClock(time) { if (Number(time.slice(0, 2)) > 12) { - return `${Number(time.slice(0, 2)) - 12}:00 pm`; + let hours = Number(time.slice(0, 2)) - 12; + let minutes = Number(time.slice(3, 5)); + minutes = minutes.toString().padStart(2, "0"); + return `${hours}:${minutes} pm`; } return `${time} am`; } @@ -26,5 +29,23 @@ console.assert( // formatAs12HourClock currently has a 🐛 // a) Write an assertion to check the return value of formatAs12HourClock when it is called with an input "17:42" + +const currentOutput3 = formatAs12HourClock("17:42"); +const targetOutput3 = "5:42 pm"; +console.assert( + currentOutput3 === targetOutput3, + "current output: %s, target output: %s", + currentOutput3, + targetOutput3 +); + // b) Check the assertion output and explain what the bug is + +/*Assertion failed: current output: 5:00 pm, target output: 5:42 pm.This because the +equality sign is a strict equality and the value on both sides are not equal. +current output doesn't display the minutes shown in the target output. + Instead of 5:42 pm it displays 5:00pm. + +*/ + // c) Now fix the bug and re-run all your assertions diff --git a/week-3/implement/get-angle-type.js b/week-3/implement/get-angle-type.js index 9dd3a210..ff97d911 100644 --- a/week-3/implement/get-angle-type.js +++ b/week-3/implement/get-angle-type.js @@ -21,3 +21,23 @@ // 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"; + } +} + +console.log(`This is an/a ${getAngleType(90)}.`); +console.log(`This is an/a ${getAngleType(45)}.`); +console.log(`This is an/a ${getAngleType(120)}.`); +console.log(`This is an/a ${getAngleType(180)}.`); +console.log(`This is an/a ${getAngleType(240)}.`); diff --git a/week-3/implement/get-card-value.js b/week-3/implement/get-card-value.js index 0dd74fbc..cc64c358 100644 --- a/week-3/implement/get-card-value.js +++ b/week-3/implement/get-card-value.js @@ -29,3 +29,23 @@ // 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(cardGiven) { + if (cardGiven === "A♠") { + return 11; + } else if (cardGiven > 2 && cardGiven < 10) { + return Number(cardGiven); + } else if (cardGiven === "J" || cardGiven === "Q" || cardGiven === "K") { + return 10; + } else if (cardGiven === "Ace" || cardGiven === "A") { + return 11; + } else { + return "Invalid card rank."; + } +} + +console.assert(getCardValue("A♠")); +console.assert(getCardValue(6)); +console.assert(getCardValue("Q")); +console.assert(getCardValue("Ace")); +console.assert(getCardValue("W")); diff --git a/week-3/implement/is-proper-fraction.js b/week-3/implement/is-proper-fraction.js index 31da32b5..f2203c2a 100644 --- a/week-3/implement/is-proper-fraction.js +++ b/week-3/implement/is-proper-fraction.js @@ -33,3 +33,34 @@ // 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 (numerator < denominator && denominator !== 0) { + return "This is true"; + } else if (numerator > denominator && denominator !== 0) { + return "This is false"; + } else if (denominator === 0) { + return "Error!!, Denominator cannot be zero"; + } else if (denominator > Math.abs(numerator)) { + return "This is true"; + } else if (numerator === denominator) { + return "This is false"; + } else { + return "Error!!"; + } +} + +// Proper Fraction check: +console.assert(isProperFraction(2, 6)); + +//Improper Fraction check: +console.assert(isProperFraction(9, 6)); + +// Zero Denominator check: +console.assert(isProperFraction(5, 0)); + +// Negative Fraction check: +console.assert(isProperFraction(-9, 9)); + +// Equal Numerator and Denominator check: +console.assert(isProperFraction(5, 5)); diff --git a/week-3/implement/is-valid-triangle.js b/week-3/implement/is-valid-triangle.js index 7b22836b..09bd4c1d 100644 --- a/week-3/implement/is-valid-triangle.js +++ b/week-3/implement/is-valid-triangle.js @@ -38,3 +38,54 @@ // 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. + +//--------------------SOLUTION------------------------------------------- +//where a,b,c denotes the length of each side of a triangle. + +//false if sum of 2 sides is less than 1 side +//true if sum of 2 sides is greater than 1 side +//false if any side is <= 0 + +function isValidTriangle(a, b, c) { + if (a + b > c && b + c > a && a + c > b) { + return true; + } else if (a <= 0 || b <= 0 || c <= 0) { + return false; + } else if (a + b <= c || b + c <= a || a + c <= b) { + return false; + } else { + return "Error"; + } +} + +// sum of 2 sides is less than 1 side + +const currentOutput = isValidTriangle(2, 3, 6); +const targetOutput = false; +console.assert(currentOutput === targetOutput, currentOutput, targetOutput); + +// sum of 2 sides is greater than 1 side +const currentOutput1 = isValidTriangle(4, 5, 2); +const targetOutput1 = true; +console.assert(currentOutput1 === targetOutput1, currentOutput1, targetOutput1); + +// if any side is <= 0 +const currentOutput2 = isValidTriangle(0, 3, 3); +const targetOutput2 = false; +console.assert( + currentOutput2 === targetOutput2, + + currentOutput2, + targetOutput2 +); + +//if any side is <= 0 + +const currentOutput3 = isValidTriangle(-1, 4, 7); +const targetOutput3 = false; +console.assert( + currentOutput3 === targetOutput3, + + currentOutput3, + targetOutput3 +); diff --git a/week-3/refactor/format-as-12-hours.js b/week-3/refactor/format-as-12-hours.js index 41603122..58534d27 100644 --- a/week-3/refactor/format-as-12-hours.js +++ b/week-3/refactor/format-as-12-hours.js @@ -3,4 +3,45 @@ // That implementation currently uses the expression Number(time.slice(0, 2)) twice // Store this expression in a variable and reference it twice in the function in the correct place +function formatAs12HourClock(time) { + const slicedHourConvertedToNumber = Number(time.slice(0, 2)); + + if (slicedHourConvertedToNumber > 12) { + let hours = slicedHourConvertedToNumber - 12; + let minutes = Number(time.slice(3, 5)); + minutes = minutes.toString().padStart(2, "0"); + return `${hours}:${minutes} 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 +); + // Explain why it makes more sense to store this expression in a variable + +// We tend to write less code than we should and just like function we can call the variable as many times as we want to within the function. diff --git a/week-3/refactor/is-vowel.js b/week-3/refactor/is-vowel.js index db675d2b..a5c7a4fd 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,7 @@ function isVowel(letter) { // here is an implementation of isVowel - this function checks if a letter is a vowel -console.log("case: letter a..."); +console.log(`case: letter a is ${isVowel("a")}`); const currentOutput = isVowel("a"); const targetOutput = true; console.assert( @@ -21,7 +20,7 @@ console.assert( targetOutput ); -console.log("case: letter e..."); +console.log(`case: letter e is ${isVowel("e")}`); const currentOutput2 = isVowel("e"); const targetOutput2 = true; console.assert( @@ -31,7 +30,7 @@ console.assert( targetOutput2 ); -console.log("case: letter k..."); +console.log(`case: letter k is ${isVowel("k")}`); const currentOutput3 = isVowel("k"); const targetOutput3 = false; console.assert( diff --git a/week-3/stretch/rotate-char.js b/week-3/stretch/rotate-char.js index f274ce1e..24a1c148 100644 --- a/week-3/stretch/rotate-char.js +++ b/week-3/stretch/rotate-char.js @@ -6,6 +6,23 @@ // This function is commonly used for weak text encryption and decryption, // where shifting characters by a certain value can obscure their meaning or reveal hidden messages. +function rotateCharacter(character, shift) { + const alphabetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const alphabetLower = "abcdefghijklmnopqrstuvwxyz"; + + if (alphabetUpper.includes(character)) { + const position = alphabetUpper.indexOf(character); + const newPosition = (position + shift) % 26; + return alphabetUpper[newPosition]; + } else if (alphabetLower.includes(character)) { + const position = alphabetLower.indexOf(character); + const newPosition = (position + shift) % 26; + return alphabetLower[newPosition]; + } + + return character; +} + // Acceptance criteria: // Given a character (char) and a shift value (shift),