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 @@ -16,6 +16,22 @@

function getAngleType(angle) {
// TODO: Implement this function
if( angle > 0 && angle < 90){
return "Acute angle";
}
else if (angle === 90){
return "Right angle";
}
else if ( angle > 90 && angle < 180){
return "Obtuse angle";
}
else if(angle === 180){
return "Straight angle";
}
else if( angle > 180 && angle < 360){
Comment on lines +19 to +31

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see some white spaces in the conditions that are not needed. How can you ensure consistent formatting in your code?

return "Reflex angle";
}
else return "Invalid angle";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: You don't need the else here as the code will only run if no other condition was true

}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -33,5 +49,32 @@ function assertEquals(actualOutput, targetOutput) {

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles

const acute = getAngleType(45);
assertEquals(acute, "Acute angle");


const right = getAngleType(90);
assertEquals(right, "Right angle");


const obtuse = getAngleType(120);
assertEquals(obtuse, "Obtuse angle");


const straight = getAngleType(180);
assertEquals(straight, "Straight angle");


const reflex = getAngleType(200);
assertEquals(reflex, "Reflex angle");


const invalid0 = getAngleType(0);
assertEquals(invalid0, "Invalid angle");

const invalid360 = getAngleType(360);
assertEquals(invalid360, "Invalid angle");



Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if(denominator === 0){
return false;
}
let fraction = numerator / denominator;
return fraction > 0 && fraction < 1;
}

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

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

// Example: numerator is 0 is a proper fraction
assertEquals(isProperFraction(0, 2), false);

// Example: numerator is equal to denominators is a proper fraction
assertEquals(isProperFraction(2, 2), false);

// Example: denominator is 0 is a proper fraction
assertEquals(isProperFraction(5, 0), false);

// Example: numerator is negative is a proper fraction
assertEquals(isProperFraction(-1, 2), false);

// Example: denominator is negative is a proper fraction
assertEquals(isProperFraction(4, -3), false);

// Example: numerator and denominator are both negative values is a proper fraction
assertEquals(isProperFraction(-5, -6), true);

// Example: negative numerator with smaller value then negative dominator is a proper fraction
assertEquals(isProperFraction(-6, -5), false);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@

function getCardValue(card) {
// TODO: Implement this function

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

const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
const validSuits = ["♠", "♥", "♦", "♣"];

let isValidRank = validRanks.includes(rank);
let isValidSuit = validSuits.includes(suit);



// Invalid Card
if( !isValidRank || !isValidSuit ){
throw new Error("Invalid Card")
Comment on lines +30 to +40

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats a nice way of checking if the input is valid. By expliciting stating all allowed values that code is easy to understand

}

if(rank == "A"){
return 11;
}
else if(rank == "J" || rank == "Q" || rank == "K"){
return 10;
}
else return Number(rank);

}

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

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:

assertEquals(getCardValue("8♠"), 8);

assertEquals(getCardValue("7♠"), 7);

assertEquals(getCardValue("6♠"), 6);

assertEquals(getCardValue("5♠"), 5);

assertEquals(getCardValue("4♠"), 4);

assertEquals(getCardValue("3♠"), 3);

assertEquals(getCardValue("2♠"), 2);

assertEquals(getCardValue("9♠"), 9);

assertEquals(getCardValue("J♠"), 10);

assertEquals(getCardValue("Q♠"), 10);

assertEquals(getCardValue("K♠"), 10);

assertEquals(getCardValue("A♠"), 11);

// Handling invalid cards
try {
getCardValue("invalid");
Expand All @@ -51,4 +99,23 @@ try {
console.log("Error thrown for invalid card 🎉");
}


// What other invalid card cases can you think of?
try {
getCardValue("24♠");

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}


try {
getCardValue("44");

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,43 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
expect(getAngleType(89)).toEqual("Acute angle");
});


// Case 2: Right angle
test(`should return "Right angle" when ( angle = 90)`, () => {
// Test various right angles, including boundary cases
expect(getAngleType(90)).toEqual("Right angle");

});


// Case 3: Obtuse angles
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
// Test various obtuse angles, including boundary cases
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(120)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});


// Case 4: Straight angle
test(`should return "Straight angle" when ( angle = 180)`, () => {
// Test various straight angles, including boundary cases
expect(getAngleType(180)).toEqual("Straight angle");
});


// Case 5: Reflex angles
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
// Test various reflex angles, including boundary cases
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(220)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});


// Case 6: Invalid angles
test(`should return "Invalid angle" when (180 < angle < 360)`, () => {
// Test various invalid angles, including boundary cases
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could add 2 test cases that are not boundary cases

});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,43 @@ 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);
});


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


// Special case: numerator is equal to denominators is a proper fraction
test(`should return false when numerator is equal to denominator`, () => {
expect(isProperFraction(2, 2)).toEqual(false);
});


// Special case: numerator is negative is a proper fraction
test(`should return false when numerator is negative value`, () => {
expect(isProperFraction(-1, 2)).toEqual(false);
});


// Special case: denominator is negative is a proper fraction
Comment on lines +25 to +31

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comments say is a prober fracteion but the function should return false. This is confusing

test(`should return false denominator is negative value `, () => {
expect(isProperFraction(1, -2)).toEqual(false);
});


// Special case: numerator and denominator are both negative values is a proper fraction//
test(`should return true when numerator ans denominator are both negative values `, () => {
expect(isProperFraction(-5, -6)).toEqual(true);
});


// Special case: negative numerator with smaller value then negative dominator is a proper fraction

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comments say is a prober fracteion but the function should return false. This is confusing

test(`should return false when negative numerator is smaller than negative denominator `, () => {
expect(isProperFraction(-6, -5)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,58 @@ const getCardValue = require("../implement/3-get-card-value");

// TODO: Write tests in Jest syntax to cover all possible outcomes.

// Case 1: Ace (A)
// Case 1: Ace (A) ♣ ♦ ♥ C
test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
expect(getCardValue("A♥")).toEqual(11);
expect(getCardValue("A♦")).toEqual(11);
expect(getCardValue("A♣")).toEqual(11);
});



// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
// Invalid Cards


// 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


// Number cards
test(`Should return the number when given a number card`, () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("7♥")).toEqual(7);
expect(getCardValue("9♦")).toEqual(9);
expect(getCardValue("6♣")).toEqual(6);
});


// Face Cards (J, Q, K)
test(`Should return 10 when given a face card`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♦")).toEqual(10);
});


// test(`Should throw an error when given an invalid card`, () => {
// expect(() => getCardValue("1♠")).toThrow();
// expect(() => getCardValue("Z♦")).toThrow();
// expect(() => getCardValue("11♥")).toThrow();
// expect(() => getCardValue("")).toThrow();
// });

Comment on lines +45 to +51

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this code commented out?

// Invalid Cards
test(`Should throw an error when given an invalid card`, () => {
expect(() => getCardValue("1♣")).toThrow("Invalid Card");
expect(() => getCardValue("Z♦")).toThrow("Invalid Card");
expect(() => getCardValue("12♦")).toThrow("Invalid Card");
expect(() => getCardValue("♦3")).toThrow("Invalid Card");
expect(() => getCardValue("")).toThrow("Invalid Card");
expect(() => getCardValue("invalid")).toThrow("Invalid Card");
expect(() => getCardValue("----")).toThrow("Invalid Card");
});
Loading