Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// TODO: Implement this function
if (angle === 90) return "Right angle";
if (angle < 90) return "Acute angle";
if (90 <angle && angle<180 ) return "Obtuse angle";
if (angle === 180) return "Straight angle";
if (180<angle && angle<360) return "Reflex angle";
return "Invalid angle"
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -35,3 +40,25 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

// Case 2: Identify Acute Angles:
const acute = getAngleType(45);
assertEquals(acute, "Acute angle");

// Case 3: Identify Obtuse Angles:
const obtuse = getAngleType(120);
assertEquals(obtuse, "Obtuse angle");

// Case 4: Identify Straight Angles:
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

// Case 5: Identify Reflex Angles:
const reflex = getAngleType(300);
assertEquals(reflex, "Reflex angle");

//case 6: Identify Invalid angle:
assertEquals(getAngleType(367),"Invalid angle");

//case 7: Identify boundary angle:
assertEquals(getAngleType(1), "Acute angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
return Math.abs(numerator) < Math.abs(denominator)
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -31,3 +31,27 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);

//Case 2: 5/5 is not a proper fraction
assertEquals(isProperFraction(5,5), false);

//Case 3: (-3)/(-5) is a proper fraction
assertEquals(isFinite(-3,-5), true);

//Case 4: (-4)/6 is a proper fraction
assertEquals(isProperFraction(-4,6),true);

//case 5: 5/(-8) is a proper fraction
assertEquals(isProperFraction(5,-8), true);

//case 6: 9/7 is not a proper fraction
assertEquals(isProperFraction(9,7), false);

//case 7: 7/0 is not a proper fraction
assertEquals(isProperFraction(7,0), false);

//case 8: 0/6 is a proper fraction
assertEquals(isProperFraction(0,6), true);

//case 9: 0/-8 is a proper fraction
assertEquals(isProperFraction(0,-8), true);
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,23 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
}
const validSuits = ["♠", "♥", "♦", "♣"];
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];

const suit = card.slice(-1);
const rank = card.slice(0, -1);


if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
throw new Error("Invalid card");
}

if (rank === "A") return 11;
if (["J", "Q", "K"].includes(rank)) return 10;

return Number(rank);
}


// 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.
Expand All @@ -38,15 +53,44 @@ function assertEquals(actualOutput, targetOutput) {
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
// Examples 1 numbers:
// Examples 1 numbers:
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("5♣"), 5);

//Examples A:
assertEquals(getCardValue("A♦"), 11)

// Handling invalid cards
try {
getCardValue("invalid");
//Examples J Q K:,
assertEquals(getCardValue("Q♥"), 10)
assertEquals(getCardValue("K♥"), 10)

//Examples invalid numbers:
assertThrow(()=>getCardValue("1♥"))
assertThrow(()=>getCardValue("11♥"))

//Examples invalid suit:
assertThrow(()=>getCardValue("A*"))

//Examples missing rank or suit:
assertThrow(()=>getCardValue("♥"))
assertThrow(()=>getCardValue("5"))

//Example extra suit:
assertThrow(()=>getCardValue("Q♠♠"))


function assertThrow(fn){
try { fn()
// we try to run this function, if it throws, stop running this bit and run the catch below

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card");
} catch (e) {}
} catch (error) {
// if the above code throws, we catch the error here, that stops the whole program crashing
console.log('there was an error getting card value!',error)
}}

console.log(' we finished running getCardValue')

// What other invalid card cases can you think of?
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,24 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test ('should return "Right angle" when angle ===90',() => {
expect(getAngleType(90)).toEqual("Right angle");
})
// Case 3: Obtuse angles
test ('should return "Obtuse angle" when (90<angle && angle <180)',() => {
expect(getAngleType(135)).toEqual("Obtuse angle");
})
// Case 4: Straight angle
test ('should return "Straight angle" when angle ===180',() => {
expect(getAngleType(180)).toEqual("Straight angle");
})

// Case 5: Reflex angles
test ('should return "Obtuse angle" when (180<angle && angle <360)',() => {
expect(getAngleType(275)).toEqual("Reflex angle");
})

// Case 6: Invalid angles
test('should return "Invalid angle"when (angle>=360)', ()=> {
expect(getAngleType(360)).toEqual("Invalid angle");
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,31 @@ const isProperFraction = require("../implement/2-is-proper-fraction");

// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.

// Special case: numerator is zero
/// Special case: denominator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

//case:absolute numerator is bigger than absolute denominator
test ('should return false when numerator absolute is bigger than denominator absolute',() => {
expect(isProperFraction(5, -4)).toEqual(false);
})

//case:numerator is the same as denominator
test('should return false if numerator is the same as denominator', () =>{
expect(isProperFraction(-9,-9)).toEqual(false)
})

//case: absolute numerator is less than absolute denominator
test ('should return true when numerator absolute is less than denominator absolute',() => {
expect(isProperFraction(3,-8)).toEqual(true);
})

//case: numerator is zero
test(`should return true when nominator is zero`, () => {
expect(isProperFraction(0,-3)).toEqual(true);
});




Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,23 @@ test(`Should return 11 when given an ace card`, () => {
// Face Cards (J, Q, K)
// Invalid Cards

// Case 2 : Number(2-10)
test('should return the exact number when given an number card', () =>{
expect(getCardValue("5♠")).toEqual(5);
})

//case 3 : face card(J,Q,K)
test('should return number when given face card', () =>{
expect(getCardValue("K♠")).toEqual(10);
})

// case 4: Invalid cards
test('throws new Error', () => {
expect(() => getCardValue("9**")).toThrow("Invalid card");
});

// To learn how to test whether a function throws an error as expected in Jest,
// please refer to the Jest documentation:
// https://jestjs.io/docs/expect#tothrowerror


Loading