diff --git a/week-3/debug/format-as-12-hours.js b/week-3/debug/format-as-12-hours.js index 56b83a5b..a966c6e1 100644 --- a/week-3/debug/format-as-12-hours.js +++ b/week-3/debug/format-as-12-hours.js @@ -1,6 +1,10 @@ function formatAs12HourClock(time) { - if (Number(time.slice(0, 2)) > 12) { - return `${Number(time.slice(0, 2)) - 12}:00 pm`; + let timeFormat = (Number(time.slice(0, 2)) - 12) + if (Number(time.slice(0, 2)) > 12) { + if (timeFormat < 10) + return `0${timeFormat}:${time.slice(3,5)} pm`; + else + return `${timeFormat}:${time.slice(3,5)} pm`; } return `${time} am`; } @@ -23,8 +27,18 @@ console.assert( targetOutput2 ); +const currentOutput3 = formatAs12HourClock("17:42"); +const targetOutput3 = "05: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" -// b) Check the assertion output and explain what the bug is +// b) Check the assertion output and explain what the bug is // it wasnt returning the minutes and wasnt adding a zero infront of 5 // 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 deleted file mode 100644 index 9dd3a210..00000000 --- a/week-3/implement/get-angle-type.js +++ /dev/null @@ -1,23 +0,0 @@ -// Implement a function getAngleType, and tests for each of the acceptance criteria. - -// Acceptance criteria: - -// Identify Right Angles: -// When the angle is exactly 90 degrees, -// Then the function should return "Right angle" - -// Identify Acute Angles: -// When the angle is less than 90 degrees, -// Then the function should return "Acute angle" - -// Identify Obtuse Angles: -// When the angle is greater than 90 degrees and less than 180 degrees, -// Then the function should return "Obtuse angle" - -// Identify Straight Angles: -// When the angle is exactly 180 degrees, -// Then the function should return "Straight angle" - -// Identify Reflex Angles: -// When the angle is greater than 180 degrees and less than 360 degrees, -// Then the function should return "Reflex angle" diff --git a/week-3/implement/get-angle-type.test.js b/week-3/implement/get-angle-type.test.js new file mode 100644 index 00000000..356f802a --- /dev/null +++ b/week-3/implement/get-angle-type.test.js @@ -0,0 +1,55 @@ +// Implement a function getAngleType, and tests for each of the acceptance criteria. + +// Acceptance criteria: + +// Identify Right Angles: +// When the angle is exactly 90 degrees, +// Then the function should return "Right angle" + +// Identify Acute Angles: +// When the angle is less than 90 degrees, +// Then the function should return "Acute angle" + +// Identify Obtuse Angles: +// When the angle is greater than 90 degrees and less than 180 degrees, +// Then the function should return "Obtuse angle" + +// Identify Straight Angles: +// When the angle is exactly 180 degrees, +// Then the function should return "Straight angle" + +// Identify Reflex Angles: +// When the angle is greater than 180 degrees and less than 360 degrees, +// Then the function should return "Reflex angle" + +function triangleAngle(degrees){ + if (degrees === 90) + return 'Right angle'; + + else if (degrees < 90) + return 'Acute angle'; + + else if (degrees > 90 && degrees < 180) + return 'Obtuse'; + + else if (degrees === 180) + return 'Straight angle' + + else if (degrees > 180 && degrees < 360) + return 'Reflex angle' + + } + + // console.log(triangleAngle(90)); + // console.log(triangleAngle(45)); + // console.log(triangleAngle(105)); + // console.log(triangleAngle(180)); + // console.log(triangleAngle(220)); + + test("idenity the angle type", function () { + expect(triangleAngle(90)).toBe("Right angle"); + expect(triangleAngle(45)).toBe("Acute angle"); + expect(triangleAngle(105)).toBe("Obtuse"); + expect(triangleAngle(180)).toBe("Straight angle"); + expect(triangleAngle(220)).toBe("Reflex angle"); + }); \ No newline at end of file diff --git a/week-3/implement/get-card-value.js b/week-3/implement/get-card-value.js deleted file mode 100644 index 0dd74fbc..00000000 --- a/week-3/implement/get-card-value.js +++ /dev/null @@ -1,31 +0,0 @@ -// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck - -// You will need to implement a function getCardValue - -// You need to write assertions for your function to check it works in different cases - -// Acceptance criteria: - -// Given a card string in the format "A♠" (representing a card in blackjack), -// When the function getCardValue is called with this card string as input, -// Then it should return the numerical card value - -// Handle Number Cards (2-10): -// Given a card with a rank between "2" and "10", -// 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): -// Given a card with a rank of "J," "Q," or "K", -// When the function is called with such a card, -// Then it should return the value 10, as these cards are worth 10 points each in blackjack. - -// Handle Ace (A): -// Given a card with a rank of "A", -// When the function is called with an Ace, -// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. - -// Handle Invalid Cards: -// 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." diff --git a/week-3/implement/get-card-value.test.js b/week-3/implement/get-card-value.test.js new file mode 100644 index 00000000..16e305da --- /dev/null +++ b/week-3/implement/get-card-value.test.js @@ -0,0 +1,82 @@ +// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck + +// You will need to implement a function getCardValue + +// You need to write assertions for your function to check it works in different cases + +// Acceptance criteria: + +// Given a card string in the format "A♠" (representing a card in blackjack), +// When the function getCardValue is called with this card string as input, +// Then it should return the numerical card value + +// Handle Number Cards (2-10): +// Given a card with a rank between "2" and "10", +// 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): +// Given a card with a rank of "J," "Q," or "K", +// When the function is called with such a card, +// Then it should return the value 10, as these cards are worth 10 points each in blackjack. + +// Handle Ace (A): +// Given a card with a rank of "A", +// When the function is called with an Ace, +// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack. + +// Handle Invalid Cards: +// 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(cardString){ + + if (cardString > 1 && cardString < 11) + return +cardString; + + else if (cardString === 'J' || cardString === 'Q' || cardString === 'K') + return 10 + + else if (cardString === 'A') + return 11 + + else + return 'Invalid card rank' + + } + + // console.log(getCardValue(10)); + // console.log(getCardValue('J')); + // console.log(getCardValue('A')); + // console.log(getCardValue('Z')); + +// const currentOutput = getCardValue('K'); +// const targetOutput = 5 +// const targetOutput1 = 10 +// const targetOutput2 = 11 +// const targetOutput3 = "Invalid card rank" + +// console.assert( +// currentOutput === targetOutput, +// `current output: ${currentOutput}, target output: ${targetOutput}` +// ) +// console.assert( +// currentOutput === targetOutput1, +// `current output: ${currentOutput}, target output: ${targetOutput1}` +// ) +// console.assert( +// currentOutput === targetOutput2, +// `current output: ${currentOutput}, target output: ${targetOutput2}` +// ) +// console.assert( +// currentOutput === targetOutput3, +// `current output: ${currentOutput}, target output: ${targetOutput3}` +// ); + +test("checks card value", function () { + expect(getCardValue(10)).toBe(10); + expect(getCardValue(`J`)).toBe(10); + expect(getCardValue(`A`)).toBe(11); + expect(getCardValue(`Z`)).toBe('Invalid card rank'); + }); \ No newline at end of file diff --git a/week-3/implement/is-proper-fraction.js b/week-3/implement/is-proper-fraction.js deleted file mode 100644 index 31da32b5..00000000 --- a/week-3/implement/is-proper-fraction.js +++ /dev/null @@ -1,35 +0,0 @@ -// You wil need to implement a function isProperFraction -// You need to write assertions for your function to check it works in different cases - -// Terms: -// Fractions: https://www.bbc.co.uk/bitesize/topics/zt9n6g8/articles/zjxpp4j -// Written here like this: 1/2 == Numerator/Denominator - -// 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. - -// 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. - -// 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. - -// 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. - -// 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. - -// 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-proper-fraction.test.js b/week-3/implement/is-proper-fraction.test.js new file mode 100644 index 00000000..b2dda5c6 --- /dev/null +++ b/week-3/implement/is-proper-fraction.test.js @@ -0,0 +1,108 @@ +// You wil need to implement a function isProperFraction +// You need to write assertions for your function to check it works in different cases + +// Terms: +// Fractions: https://www.bbc.co.uk/bitesize/topics/zt9n6g8/articles/zjxpp4j +// Written here like this: 1/2 == Numerator/Denominator + +// 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. + +// 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. + +// 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. + +// 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. + +// 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. + +// 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 properFraction(num,den){ + + if (den === 0) + return 'Error'; + + if (num < den) + return 'True'; + + if (num > den || num === den) + return 'False'; + + if (num < 0 && num < den) + return 'True'; + + if (num === den) + return 'False'; + + } + + // console.log(properFraction(2,3)); + // console.log(properFraction(5,2)); + // console.log(properFraction(3,0)); + // console.log(properFraction(-4,7)); + // console.log(properFraction(3,3)); + +// const currentOutput = properFraction(2,3); +// const targetOutput = 'True' + +// console.assert( +// currentOutput === targetOutput, +// `current output: ${currentOutput}, target output: ${targetOutput}` +// ) + +// const currentOutput1 = properFraction(5,2); +// const targetOutput1 = 'False' + +// console.assert( +// currentOutput1 === targetOutput1, +// `current output: ${currentOutput1}, target output: ${targetOutput1}` +// ) + +// const currentOutput2 = properFraction(3,0); +// const targetOutput2 = 'Error' + +// console.assert( +// currentOutput2 === targetOutput2, +// `current output: ${currentOutput2}, target output: ${targetOutput2}` +// ) + +// const currentOutput3 = properFraction(-4,7); +// const targetOutput3 = 'True' + +// console.assert( +// currentOutput3 === targetOutput3, +// `current output: ${currentOutput3}, target output: ${targetOutput3}` +// ) + +// const currentOutput4 = properFraction(3,3); +// const targetOutput4 = 'False' + +// console.assert( +// currentOutput4 === targetOutput4, +// `current output: ${currentOutput4}, target output: ${targetOutput4}` +// ) + +test("checks if fraction is proper fraction", function () { + expect(properFraction(2,3)).toBe(`True`); + expect(properFraction(5,2)).toBe(`False`); + expect(properFraction(3,0)).toBe(`Error`); + expect(properFraction(-4,7)).toBe('True'); + expect(properFraction(3,3)).toBe('False'); + }); \ No newline at end of file diff --git a/week-3/implement/is-valid-triangle.js b/week-3/implement/is-valid-triangle.test.js similarity index 64% rename from week-3/implement/is-valid-triangle.js rename to week-3/implement/is-valid-triangle.test.js index 7b22836b..361c239f 100644 --- a/week-3/implement/is-valid-triangle.js +++ b/week-3/implement/is-valid-triangle.test.js @@ -38,3 +38,46 @@ // 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 + b <= c || a + c <= b || b + c <= a) + return 'False'; + + else if (a <=0 || b <=0 || c <=0) + return 'False'; + + else if (a + b > c || b + c > a || a + c > b) + return 'True'; + + } + +// const currentOutput = isValidTriangle(3,3,3); +// const targetOutput = 'True' + +// console.assert( +// currentOutput === targetOutput, +// `current output: ${currentOutput}, target output: ${targetOutput}` +// ) + +// const currentOutput1 = isValidTriangle(3,3,6); +// const targetOutput1 = 'False' + +// console.assert( +// currentOutput1 === targetOutput1, +// `current output: ${currentOutput1}, target output: ${targetOutput1}` +// ) + +// const currentOutput2 = isValidTriangle(3,3,0); +// const targetOutput2 = 'False' + +// console.assert( +// currentOutput2 === targetOutput2, +// `current output: ${currentOutput2}, target output: ${targetOutput2}` +// ) + +test("checks is triangle is valid", function () { + expect(isValidTriangle(3,3,3)).toBe(`True`); + expect(isValidTriangle(3,3,6)).toBe(`False`); + expect(isValidTriangle(3,3,0)).toBe(`False`); + }); \ No newline at end of file diff --git a/week-3/refactor/format-as-12-hours.js b/week-3/refactor/format-as-12-hours.js index 41603122..1125194e 100644 --- a/week-3/refactor/format-as-12-hours.js +++ b/week-3/refactor/format-as-12-hours.js @@ -3,4 +3,43 @@ // 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 -// Explain why it makes more sense to store this expression in a variable +// Explain why it makes more sense to store this expression in a variable - can be used frequently in other places, makes it look more neat + + +function formatAs12HourClock(time) { + let timeFormat = (Number(time.slice(0, 2)) - 12) + if (Number(time.slice(0, 2)) > 12) { + if (timeFormat < 10) + return `0${timeFormat}:${time.slice(3,5)} pm`; + else + return `${timeFormat}:${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 = "05:42 pm"; + console.assert( + currentOutput3 === targetOutput3, + "current output: %s, target output: %s", + currentOutput3, + targetOutput3 + ); \ No newline at end of file diff --git a/week-3/refactor/is-vowel.js b/week-3/refactor/is-vowel.js index db675d2b..2cd2142b 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" ); diff --git a/week-4/implement/count.test.js b/week-4/implement/count.test.js index 77a713a1..21231dfd 100644 --- a/week-4/implement/count.test.js +++ b/week-4/implement/count.test.js @@ -15,3 +15,19 @@ // And a character char that does not exist within the case-sensitive str, // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str. + +function countChar(str, char){ + let count = 0; + for (let i = 0; i < str.length; i++) { + if (str[i] === char) { + count++; + } + } + return count; +} + +test("checks number of character in a string", function () { + expect(countChar("hello", "l")).toBe(2); + expect(countChar("princess", "c")).toBe(1); + expect(countChar("snow white", "w")).toBe(2); + }); \ No newline at end of file diff --git a/week-4/implement/get-ordinal-number.test.js b/week-4/implement/get-ordinal-number.test.js index 4e735d0b..c98844fa 100644 --- a/week-4/implement/get-ordinal-number.test.js +++ b/week-4/implement/get-ordinal-number.test.js @@ -2,3 +2,18 @@ // continue testing and implementing getOrdinalNumber for additional cases // Write your tests using Jest - remember to run your tests often for continual feedback + +function getOrdinalNumber(number) { + if (number === 11){ + return `${number}th`; + } + if (number%10 === 1) { + return `${number}st`; + } +} + +test("works for any number ending in 1", function () { + expect(getOrdinalNumber(1)).toBe("1st"); + expect(getOrdinalNumber(11)).toBe("11th"); + expect(getOrdinalNumber(21)).toBe("21st"); + }); diff --git a/week-4/implement/is-prime.test.js b/week-4/implement/is-prime.test.js index 90199887..286f8b71 100644 --- a/week-4/implement/is-prime.test.js +++ b/week-4/implement/is-prime.test.js @@ -1,3 +1,20 @@ // Given a positive integer num, // When the isPrime function is called with num as input, // Then it should return a boolean representing whether the num is prime + +function isPrime(num){ + if (num < 2) { + return false; + } + for (var i = 2; i < num; i++){ + if(num%i===0) + return false; + } + return true; +} + +test("checks if number is prime number", function () { + expect(isPrime(3)).toBe(true); + expect(isPrime(11)).toBe(true); + expect(isPrime(22)).toBe(false); +}); \ No newline at end of file diff --git a/week-4/implement/repeat.test.js b/week-4/implement/repeat.test.js index 0b2b0a3e..e057da00 100644 --- a/week-4/implement/repeat.test.js +++ b/week-4/implement/repeat.test.js @@ -23,3 +23,27 @@ // Given a target string str and a negative integer count, // When the repeat function is called with these inputs, // Then it should throw an error or return an appropriate error message, as negative counts are not valid. + +function repeat(str,count){ + if (count > 0 ){ + return str.repeat(count); + } + if (count === 1){ + return str; + } + if (count === 0){ + return " "; + } + + if (count < 0){ + return "Error-Negative counts not valid"; + } +} + + +test("checks how many times a str is repeated based on count", function () { + expect(repeat("hello",3)).toBe("hellohellohello"); + expect(repeat("ola",1)).toBe("ola"); + expect(repeat("bonjour",0)).toBe(" "); + expect(repeat("salam",-4)).toBe("Error-Negative counts not valid"); + }); \ No newline at end of file