From 527eeb57ace4ae1d181c9da525990016e9023d0c Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 24 Nov 2023 22:00:40 +0000 Subject: [PATCH 1/3] week 3 completed --- week-3/debug/format-as-12-hours.js | 20 ++++++-- week-3/implement/get-angle-type.js | 24 ++++++++++ week-3/implement/get-card-value.js | 44 +++++++++++++++++ week-3/implement/is-proper-fraction.js | 65 ++++++++++++++++++++++++++ week-3/implement/is-valid-triangle.js | 37 +++++++++++++++ week-3/refactor/format-as-12-hours.js | 41 +++++++++++++++- week-3/refactor/is-vowel.js | 1 - 7 files changed, 227 insertions(+), 5 deletions(-) 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 index 9dd3a210..9f2cb4b2 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 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)); \ 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..9fa1fdad 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(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}` +); \ 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..69ea4507 100644 --- a/week-3/implement/is-proper-fraction.js +++ b/week-3/implement/is-proper-fraction.js @@ -33,3 +33,68 @@ // 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 (num < den) + return 'True'; + + else if (num > den || num === den) + return 'False'; + + else if (den == 0) + return 'Error'; + + else if (num < 0 && num < den) + return 'True'; + + else 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}` +) \ 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..eebbbeda 100644 --- a/week-3/implement/is-valid-triangle.js +++ b/week-3/implement/is-valid-triangle.js @@ -38,3 +38,40 @@ // 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}` +) \ 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" ); From ea398ebb91fb3febe2f488310109f65f8a4c824e Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 6 Dec 2023 20:06:53 +0000 Subject: [PATCH 2/3] completed wk 4 recommended order exercises --- week-4/implement/count.test.js | 16 ++++++++++++++ week-4/implement/get-ordinal-number.test.js | 15 +++++++++++++ week-4/implement/is-prime.test.js | 17 +++++++++++++++ week-4/implement/repeat.test.js | 24 +++++++++++++++++++++ 4 files changed, 72 insertions(+) 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 From 20e3c981d6af200f8d3f2fa48a20cd88d3a52d48 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 6 Dec 2023 20:32:32 +0000 Subject: [PATCH 3/3] implemented tests for week 3 exercises --- ...t-angle-type.js => get-angle-type.test.js} | 18 +++- ...t-card-value.js => get-card-value.test.js} | 51 +++++----- ...fraction.js => is-proper-fraction.test.js} | 98 ++++++++++--------- ...-triangle.js => is-valid-triangle.test.js} | 52 +++++----- 4 files changed, 124 insertions(+), 95 deletions(-) rename week-3/implement/{get-angle-type.js => get-angle-type.test.js} (67%) rename week-3/implement/{get-card-value.js => get-card-value.test.js} (65%) rename week-3/implement/{is-proper-fraction.js => is-proper-fraction.test.js} (58%) rename week-3/implement/{is-valid-triangle.js => is-valid-triangle.test.js} (72%) diff --git a/week-3/implement/get-angle-type.js b/week-3/implement/get-angle-type.test.js similarity index 67% rename from week-3/implement/get-angle-type.js rename to week-3/implement/get-angle-type.test.js index 9f2cb4b2..356f802a 100644 --- a/week-3/implement/get-angle-type.js +++ b/week-3/implement/get-angle-type.test.js @@ -40,8 +40,16 @@ function triangleAngle(degrees){ } - console.log(triangleAngle(90)); - console.log(triangleAngle(45)); - console.log(triangleAngle(105)); - console.log(triangleAngle(180)); - console.log(triangleAngle(220)); \ No newline at end of file + // 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.test.js similarity index 65% rename from week-3/implement/get-card-value.js rename to week-3/implement/get-card-value.test.js index 9fa1fdad..16e305da 100644 --- a/week-3/implement/get-card-value.js +++ b/week-3/implement/get-card-value.test.js @@ -51,25 +51,32 @@ function getCardValue(cardString){ // 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}` -); \ No newline at end of file +// 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.test.js similarity index 58% rename from week-3/implement/is-proper-fraction.js rename to week-3/implement/is-proper-fraction.test.js index 69ea4507..b2dda5c6 100644 --- a/week-3/implement/is-proper-fraction.js +++ b/week-3/implement/is-proper-fraction.test.js @@ -36,19 +36,19 @@ function properFraction(num,den){ + if (den === 0) + return 'Error'; + if (num < den) return 'True'; - else if (num > den || num === den) + if (num > den || num === den) return 'False'; - else if (den == 0) - return 'Error'; - - else if (num < 0 && num < den) + if (num < 0 && num < den) return 'True'; - else if (num === den) + if (num === den) return 'False'; } @@ -59,42 +59,50 @@ function properFraction(num,den){ // 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}` -) \ No newline at end of file +// 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 72% rename from week-3/implement/is-valid-triangle.js rename to week-3/implement/is-valid-triangle.test.js index eebbbeda..361c239f 100644 --- a/week-3/implement/is-valid-triangle.js +++ b/week-3/implement/is-valid-triangle.test.js @@ -52,26 +52,32 @@ function isValidTriangle(a,b,c){ } -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}` -) \ No newline at end of file +// 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