diff --git a/week-3/debug/format-as-12-hours.js b/week-3/debug/format-as-12-hours.js index 56b83a5b..adbc214e 100644 --- a/week-3/debug/format-as-12-hours.js +++ b/week-3/debug/format-as-12-hours.js @@ -1,9 +1,9 @@ -function formatAs12HourClock(time) { - if (Number(time.slice(0, 2)) > 12) { - return `${Number(time.slice(0, 2)) - 12}:00 pm`; - } - return `${time} am`; -} +// 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"; @@ -22,9 +22,41 @@ console.assert( currentOutput2, targetOutput2 ); - // formatAs12HourClock currently has a 🐛 // 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 === targetOutput2, + "current output: %s, target output: %s", + currentOutput3, + targetOutput3 +); +// Current implementation of function is missing minutes part (:42). +// The condition Number(time.slice(0, 2)) > 12 doesn't account "12:00" or "00:00". +// "12:00" should be "12:00 pm". +// "00:00" should be "12:00 am". + +function formatAs12HourClock(time){ + let hour = Number(time.slice(0,2)); + let minute = time.slice(3); + let dayTime = "AM"; + if(hour >= 12){ + dayTime = "PM"; + } + if(hour === 0){ + hour = 12; + } + else if(hour > 12){ + hour -= 12; + } + return `${hour.toString().padStart(2,"0")}:${minute} ${dayTime}`; +} +console.log(formatAs12HourClock("09:12")); +console.log(formatAs12HourClock("12:00")); +console.log(formatAs12HourClock("18:22")); +console.log(formatAs12HourClock("00:00")); +console.log(formatAs12HourClock("20:20")); \ No newline at end of file diff --git a/week-3/implement/get-angle-type.js b/week-3/implement/get-angle-type.js index 9dd3a210..a23943bb 100644 --- a/week-3/implement/get-angle-type.js +++ b/week-3/implement/get-angle-type.js @@ -21,3 +21,25 @@ // 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" + } + if(angle < 90){ + return "Acute angle" + } + if(angle > 90 && angle < 180){ + return "Obtuse angle" + } + if(angle === 180){ + return "Straight angle" + } + if(angle > 180){ + return "Reflex angle" + } +} +console.log(getAngleType(47)); +console.log(getAngleType(90)); +console.log(getAngleType(145)); +console.log(getAngleType(180)); +console.log(getAngleType(190)); \ No newline at end of file diff --git a/week-3/implement/get-card-value.js b/week-3/implement/get-card-value.js index 0dd74fbc..e859c8f3 100644 --- a/week-3/implement/get-card-value.js +++ b/week-3/implement/get-card-value.js @@ -12,7 +12,7 @@ // Handle Number Cards (2-10): // Given a card with a rank between "2" and "10", -// When the function is called with such a card, +// 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). // Handle Face Cards (J, Q, K): @@ -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(card){ + // Removing last character if input contains (♠, ♥, ♦, ♣) + const selectFirstChar = card.slice(-1); + const suits = ["♠", "♥", "♦", "♣"]; + // If the last character is not present + let rank; + if(suits.includes(selectFirstChar)){ + rank = card.slice(0,-1); // remove last character + } + else{ + rank = card; + } + // handel number card 2-10 + if(!isNaN(rank)){ + const numberValue = parseInt(rank); + if(numberValue >=2 && numberValue <=10){ + return numberValue; + } + } + // Handle Face Cards (J, Q, K) + if(rank === "J" || rank === "Q" || rank === "K"){ + return 10; + } + // Handle Ace (A) + if(rank === "A"){ + return 11; + } + throw new Error("invalid card rank") +} +try{ + console.log(getCardValue("2♣")); + console.log(getCardValue("10♦")); + console.log(getCardValue("7")); + + console.log(getCardValue("A")); + console.log(getCardValue("J♠")); + console.log(getCardValue("Q♦")); + console.log(getCardValue("K")); + // throw an error + console.log(getCardValue("1♣")); +} +catch(e){ + console.log("Caught error:", e.message); +} \ No newline at end of file diff --git a/week-3/implement/is-proper-fraction.js b/week-3/implement/is-proper-fraction.js index 31da32b5..ffc1def7 100644 --- a/week-3/implement/is-proper-fraction.js +++ b/week-3/implement/is-proper-fraction.js @@ -1,4 +1,4 @@ -// You wil need to implement a function isProperFraction +// You wil need to implement a function // You need to write assertions for your function to check it works in different cases // Terms: @@ -33,3 +33,20 @@ // 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 can not be zero"); + } + return Math.abs(Numerator) < Math.abs(Denominator); + } + console.assert(isProperFraction(2,3) === true , "Text passed : 2/3 should return true"); + console.assert(isProperFraction(5,3) === false , "Test failed : 5/3 should return false"); + try{ + isProperFraction(3,0); + console.assert(false , "3/0 should return false") + }catch(error){ + console.assert(error.message === "Test Failed: Expected error message for zero denominator"); + } + console.assert(isProperFraction(-4,7) === true , "Test Failed: -4/7 should return true"); + console.assert(isProperFraction(3,3) === false , "Test Failed: 3/3 should return true"); + console.log("All assertions passed if no errors are shown."); \ No newline at end of file diff --git a/week-3/implement/is-valid-triangle.js b/week-3/implement/is-valid-triangle.js index 7b22836b..fd74b190 100644 --- a/week-3/implement/is-valid-triangle.js +++ b/week-3/implement/is-valid-triangle.js @@ -38,3 +38,17 @@ // 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; + } + if(a + b > c && a + c > b && b + c > a ){ + return true; + } + return false; +} +console.log(isValidTriangle(3,3,3)); +console.log(isValidTriangle(1, 2, 3)); // false because plus of two sided is equal to 3 and other side also three +console.log(isValidTriangle(0, 4, 5)); // false +console.log(isValidTriangle(3, -4, 5)); // false side length cannot be negative +console.log(isValidTriangle(7, 10, 5)); // true Given valid side lengths where the sum of any two sides is greater than the third side diff --git a/week-3/refactor/format-as-12-hours.js b/week-3/refactor/format-as-12-hours.js index 41603122..05f4d523 100644 --- a/week-3/refactor/format-as-12-hours.js +++ b/week-3/refactor/format-as-12-hours.js @@ -4,3 +4,24 @@ // 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){ + let hour = Number(time.slice(0,2)); + let minute = time.slice(3); + let dayTime = "AM"; + if(hour >= 12){ + dayTime = "PM"; + } + if(hour === 0){ + hour = 12; + } + else if(hour > 12){ + hour -= 12; + } + return `${hour.toString().padStart(2,"0")}:${minute} ${dayTime}`; +} +console.log(formatAs12HourClock("09:12")); +console.log(formatAs12HourClock("12:00")); +console.log(formatAs12HourClock("18:22")); +console.log(formatAs12HourClock("00:00")); +console.log(formatAs12HourClock("20:20")); diff --git a/week-3/refactor/is-vowel.js b/week-3/refactor/is-vowel.js index db675d2b..24e1f3a6 100644 --- a/week-3/refactor/is-vowel.js +++ b/week-3/refactor/is-vowel.js @@ -1,16 +1,18 @@ -function isVowel(letter) { - return ( - letter === "a" || - letter === "e" || - letter === "i" || - letter === "i" || - letter === "o" || - letter === "u" - ); -} +// function isVowel(letter) { +// return ( +// letter === "a" || +// letter === "e" || +// letter === "i" || +// letter === "o" || +// letter === "u" +// ); +// } +function isVowel(letter){ + const vowels = ["a","e","i","o","u"]; + return vowels.includes(letter.toLowerCase()); +} // here is an implementation of isVowel - this function checks if a letter is a vowel - console.log("case: letter a..."); const currentOutput = isVowel("a"); const targetOutput = true; @@ -40,3 +42,5 @@ console.assert( currentOutput3, targetOutput3 ); + +console.log(isVowel("k")); \ No newline at end of file diff --git a/week-3/stretch/rotate-char.js b/week-3/stretch/rotate-char.js index f274ce1e..5bb48e6a 100644 --- a/week-3/stretch/rotate-char.js +++ b/week-3/stretch/rotate-char.js @@ -40,3 +40,19 @@ 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(character, shiftAmount){ + const isLowerCase = character >= "a" && character <="z"; + const isUpperCase = character >= "A" && character <= "Z"; + if(isLowerCase){ + const lowerCaseStart = "a".charCodeAt(0); + const rotatedCode = ((character.charCodeAt(0)-lowerCaseStart + shiftAmount) % 26) + lowerCaseStart; + return String.fromCharCode(rotatedCode); + } + if (isUpperCase) { + const uppercaseStart = "A".charCodeAt(0); + const rotatedCode =((character.charCodeAt(0) - uppercaseStart + shiftAmount) % 26) + uppercaseStart; + return String.fromCharCode(rotatedCode); + } + return character; +} \ No newline at end of file