-
-
Notifications
You must be signed in to change notification settings - Fork 240
Birmingham | 25-ITP-Sep | Ahmad Ehsas | Sprint 3 | Implement-and-rewrite-tests #735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
65fe894
532fd66
0cba6bb
8d6eb95
2479c4f
3fafedf
7a5fe08
68f5551
5dd3b88
62ef41d
1443cbf
1e2318b
edea881
dbf4e92
fad2100
1154f89
047df5d
ba0f73e
5aeb5c0
550f44d
e76a59c
3415822
bf5f7f8
4f1e86a
e5699a5
63d4861
e749f57
d34b464
fb90283
8b3fd7a
b1e528f
e956cdc
c57fbd0
7490201
154071d
30dfbd8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,11 +8,21 @@ | |
| // Then, write the next test! :) Go through this process until all the cases are implemented | ||
|
|
||
| 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 && angle < 360) return "Reflex angle"; | ||
|
|
||
| // read to the end, complete line 36, then pass your test here | ||
|
|
||
| if (angle === 90) { | ||
| return "Right angle"; | ||
| } | ||
|
Comment on lines
+12
to
22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have some redundant code here. |
||
| // Run the tests, work out what Case 2 is testing, and implement the required code here. | ||
| // Then keep going for the other cases, one at a time. | ||
|
|
||
| } | ||
|
|
||
| // The line below allows us to load the getAngleType function into tests in other files. | ||
|
|
@@ -50,14 +60,19 @@ assertEquals(acute, "Acute angle"); | |
| // When the angle is greater than 90 degrees and less than 180 degrees, | ||
| // Then the function should return "Obtuse angle" | ||
| const obtuse = getAngleType(120); | ||
| assertEquals(obtuse, "Obtuse angle"); | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
|
|
||
| // Case 4: Identify Straight Angles: | ||
| // When the angle is exactly 180 degrees, | ||
| // Then the function should return "Straight angle" | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
| const straight = getAngleType(180); | ||
| assertEquals(straight, "Straight angle"); | ||
|
|
||
| // Case 5: Identify Reflex Angles: | ||
| // When the angle is greater than 180 degrees and less than 360 degrees, | ||
| // Then the function should return "Reflex angle" | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
| const reflex = getAngleType(270); | ||
| assertEquals(reflex, "Reflex angle"); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,14 @@ | |
| // write one test at a time, and make it pass, build your solution up methodically | ||
|
|
||
| function isProperFraction(numerator, denominator) { | ||
| if (Math.abs(numerator) < denominator) return true; // This version of code works correctly for proper and negative fractions. | ||
| if (Math.abs(numerator) >= Math.abs(denominator)) return false; | ||
| if (Math.abs(numerator) === Math.abs(denominator)) return false; | ||
|
|
||
| if (numerator < denominator) { | ||
| return true; | ||
| } | ||
|
Comment on lines
+11
to
17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| } | ||
|
|
||
| // The line below allows us to load the isProperFraction function into tests in other files. | ||
|
|
@@ -46,14 +51,18 @@ assertEquals(improperFraction, false); | |
| // 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. | ||
| const negativeFraction = isProperFraction(-4, 7); | ||
| // ====> complete with your assertion | ||
| assertEquals(negativeFraction, true); // assertion for negative fraction. | ||
|
|
||
| // 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. | ||
| const equalFraction = isProperFraction(3, 3); | ||
| assertEquals(equalFraction, false); // assertion for equal numerator and denominator. | ||
| // ====> complete with your assertion | ||
|
|
||
| // Stretch: | ||
| // What other scenarios could you test for? | ||
| console.log(isProperFraction(5, 2)); | ||
| console.log(isProperFraction(-4, 7)); | ||
| console.log(isProperFraction(-3, 3)); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,15 +8,35 @@ | |
| // write one test at a time, and make it pass, build your solution up methodically | ||
| // just make one change at a time -- don't rush -- programmers are deep and careful thinkers | ||
| function getCardValue(card) { | ||
| if (rank === "A") { | ||
| return 11; | ||
| const rank = card.slice(0, -1); // get the rank (before the suit symbol) | ||
|
|
||
| if (!isNaN(rank)) { | ||
| return Number(rank); // Number card | ||
| } | ||
|
Comment on lines
+13
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In JavaScript, strings that represent valid numeric literals in the language can be safely converted to equivalent numbers. For examples, "0x02", "2.1", "9e1" or "0002" Do you want to recognize these string values as valid ranks? |
||
|
|
||
| if (rank === "J" || rank === "Q" || rank === "K") { | ||
| return 10; // Face cards | ||
| } | ||
|
|
||
| if (rank === "A") return 11; // Ace | ||
|
|
||
| // Anything else is invalid | ||
| throw new Error("Invalid card rank"); | ||
| } | ||
|
|
||
| // if the rank is not a number or a face card, throw an error(invalid card rank). | ||
|
|
||
| // if (rank === "x") { | ||
| // return 11; | ||
| // } | ||
|
|
||
|
|
||
|
|
||
| // The line below allows us to load the getCardValue function into tests in other files. | ||
| // This will be useful in the "rewrite tests with jest" step. | ||
| module.exports = getCardValue; | ||
|
|
||
|
|
||
| // You need to write assertions for your function to check it works in different cases | ||
| // we're going to use this helper function to make our assertions easier to read | ||
| // if the actual output matches the target output, the test will pass | ||
|
|
@@ -39,13 +59,23 @@ assertEquals(aceofSpades, 11); | |
| // 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). | ||
| const fiveofHearts = getCardValue("5♥"); | ||
| // ====> write your test here, and then add a line to pass the test in the function above | ||
| assertEquals(fiveofHearts, 5); | ||
|
|
||
| const tenofDiamonds = getCardValue("10♦"); | ||
| assertEquals(tenofDiamonds, 10); | ||
|
|
||
| // Handle Face Cards (J, Q, K): | ||
| // Given a card with a rank of "10," "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. | ||
| const jackofClubs = getCardValue("J♣"); | ||
| assertEquals(jackofClubs, 10); | ||
|
|
||
| const queenofHearts = getCardValue("Q♥"); | ||
| assertEquals(queenofHearts, 10); | ||
|
|
||
| const kingofSpades = getCardValue("K♠"); | ||
| assertEquals(kingofSpades, 10); | ||
| // Handle Ace (A): | ||
| // Given a card with a rank of "A", | ||
| // When the function is called with an Ace, | ||
|
|
@@ -55,3 +85,8 @@ const fiveofHearts = getCardValue("5♥"); | |
| // 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." | ||
| // try { | ||
| // getCardValue("Z♠"); | ||
| // } catch (error) { | ||
| // console.log("Caught error:", error.message); // Should say "Invalid card rank" | ||
| // } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,7 +7,21 @@ test("should return true for a proper fraction", () => { | |
| }); | ||
|
|
||
| // Case 2: Identify Improper Fractions: | ||
| est("should return false for improper fractions", () => { | ||
| const improperFraction = isProperFraction(5, 2); | ||
| expect(improperFraction).toEqual(false); | ||
| }); | ||
|
|
||
| // Case 3: Identify Negative Fractions: | ||
| test("should return true for negative fractions", () => { | ||
| const negativeFraction = isProperFraction(-4, 7); | ||
| expect(negativeFraction).toEqual(true); | ||
| }); | ||
|
|
||
|
Comment on lines
+16
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not all negative fractions are proper fractions. For example, -5/2. If you allow negative denominator, then you could also test |
||
|
|
||
| // Case 4: Identify Equal Numerator and Denominator: | ||
| test("should return false for equal numerator and denominator", () => { | ||
| const equalFraction = isProperFraction(3, 3); | ||
| expect(equalFraction).toEqual(false); | ||
| }); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,58 @@ | |
| // We will use the same function, but write tests for it using Jest in this file. | ||
| const getCardValue = require("../implement/3-get-card-value"); | ||
|
|
||
|
|
||
| // Case 4: Handle Ace (A): | ||
| test("should return 11 for Ace of Spades", () => { | ||
| const aceofSpades = getCardValue("A♠"); | ||
| expect(aceofSpades).toEqual(11); | ||
| }); | ||
|
|
||
| // Case 2: Handle Number Cards (2-10): | ||
| test("should return correct values for number cards", () => { | ||
| expect(getCardValue("5♥")).toEqual(5); | ||
| expect(getCardValue("10♦")).toEqual(10); | ||
| }); | ||
|
|
||
|
|
||
| // Case 3: Handle Face Cards (J, Q, K): | ||
| // Case 4: Handle Ace (A): | ||
| test("should return for face cards J, Q, K", () => { | ||
| expect(getCardValue("J♣")).toEqual(10); | ||
| expect(getCardValue("Q♦")).toEqual(10); | ||
| expect(getCardValue("K♠")).toEqual(10); | ||
| }); | ||
|
|
||
|
|
||
|
|
||
| // Case 5: Handle Invalid Cards: | ||
| test("throws an error or invalid card", () => { | ||
| expect(() => getCardValue("Z♠")).toThrow("Invalid card"); | ||
| }); | ||
|
|
||
| test("should throw error for malformed numeric input", () => { | ||
| expect(() => getCardValue("2.9999♠")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for repeated characters in rank", () => { | ||
| expect(() => getCardValue("3AAAA♠")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for number beyond valid range", () => { | ||
| expect(() => getCardValue("11♠")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for missing suit", () => { | ||
| expect(() => getCardValue("Q")).toThrow("Invalid card rank"); | ||
| }); | ||
|
|
||
| test("should throw error for non-string input", () => { | ||
| expect(() => getCardValue(5)).toThrow("Card must be a string"); | ||
| }); | ||
|
|
||
|
Comment on lines
+29
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| // try { | ||
| // getCardValue("Z♠"); | ||
| //} catch (error) { | ||
| // console.log("Caught error:", error.message); | ||
| //} | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,16 @@ | ||
| function countChar(stringOfCharacters, findCharacter) { | ||
| return 5 | ||
| let count = 0; // start a count with 0 | ||
| for (let char of stringOfCharacters) { | ||
| // repeat for each character in the string | ||
| if (char === findCharacter) { | ||
| // check if the character matches the one we are looking for. | ||
| count++; // if it does increase the count by 1. | ||
| } | ||
| } | ||
|
|
||
| return count; | ||
|
|
||
| } | ||
| console.log(countChar("aAaAaAaAa", "a")); // example usage should return 5. | ||
|
|
||
| module.exports = countChar; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,20 @@ | ||
| function getOrdinalNumber(num) { | ||
| return "1st"; | ||
| let ord = "th"; | ||
|
|
||
| if (n % 10 == 1 && n % 100 != 11) { | ||
| ord = "st"; | ||
| } else if (n % 10 == 2 && n % 100 != 12) { | ||
| ord = "nd"; | ||
| } else if (n % 10 == 3 && n % 100 != 13) { | ||
| ord = "rd"; | ||
| } | ||
|
|
||
| return n + ord; | ||
| } | ||
| console.log(getOrdinalNumber(1)); // The output should be "1st" | ||
|
|
||
| // in this function, we got the ordinal number for 1. | ||
| // we used a simple if statement to check if the number is 1 and return "1st". | ||
|
|
||
|
|
||
| module.exports = getOrdinalNumber; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| function repeat() { | ||
| return "hellohellohello"; | ||
| if (times < 0) { | ||
| throw new Error("Times must be a non-negative integer"); | ||
| } | ||
| return word.repeat(times); | ||
| } | ||
| console.log(repeat("hello", 3)); // The output should be "hellohellohello" | ||
|
|
||
|
|
||
|
|
||
| module.exports = repeat; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The subfolder containing these two files were moved from its original folder.