Skip to content
Closed
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 <= 0 || angle >= 360) return "Invalid angle";
if (angle === 90) return "Right angle";
if (angle === 180) return "Straight angle";
if (angle < 90) return "Acute angle";
if (angle < 180) return "Obtuse angle";
return "Reflex angle";
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -31,7 +36,25 @@ function assertEquals(actualOutput, targetOutput) {
);
}

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
// Tests covering all cases
const right = getAngleType(90);
assertEquals(right, "Right angle");

assertEquals(getAngleType(45), "Acute angle");
assertEquals(getAngleType(1), "Acute angle");
assertEquals(getAngleType(89), "Acute angle");

assertEquals(getAngleType(91), "Obtuse angle");
assertEquals(getAngleType(135), "Obtuse angle");
assertEquals(getAngleType(179), "Obtuse angle");

assertEquals(getAngleType(180), "Straight angle");

assertEquals(getAngleType(181), "Reflex angle");
assertEquals(getAngleType(270), "Reflex angle");
assertEquals(getAngleType(359), "Reflex angle");

assertEquals(getAngleType(0), "Invalid angle");
assertEquals(getAngleType(360), "Invalid angle");
assertEquals(getAngleType(-10), "Invalid angle");
assertEquals(getAngleType(400), "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,27 @@

// Assumption: The parameters are valid numbers (not NaN or Infinity).

// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition.

// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.
// Note: A proper fraction is one where the absolute value of the numerator
// is strictly less than the absolute value of the denominator.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator === 0) return false;
return Math.abs(numerator) < Math.abs(denominator);
}

// The line below allows us to load the isProperFraction function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = isProperFraction;

// Here's our helper again
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// TODO: Write tests to cover all cases.
// What combinations of numerators and denominators should you test?

// Example: 1/2 is a proper fraction
// Tests covering all cases
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(2, 1), false); // numerator > denominator
assertEquals(isProperFraction(3, 3), false); // equal -> not proper
assertEquals(isProperFraction(-1, 2), true); // negative numerator
assertEquals(isProperFraction(1, -2), true); // negative denominator
assertEquals(isProperFraction(1, 0), false); // zero denominator
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const suit = card.slice(-1);
const rank = card.slice(0, -1);
const validSuits = ["\u2660", "\u2665", "\u2666", "\u2663"];
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];

if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
throw new Error(`Invalid card: ${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.
Expand All @@ -40,6 +50,13 @@ function assertEquals(actualOutput, targetOutput) {
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("A♠"), 11);
assertEquals(getCardValue("A♥"), 11);
assertEquals(getCardValue("2♦"), 2);
assertEquals(getCardValue("10♥"), 10);
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("K♦"), 10);

// Handling invalid cards
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
// This statement loads the getAngleType function you wrote in the implement directory.
// We will use the same function, but write tests for it using Jest in this file.
const getAngleType = require("../implement/1-get-angle-type");

// TODO: Write tests in Jest syntax to cover all cases/outcomes,
// including boundary and invalid cases.

// Case 1: Acute angles
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(1)).toEqual("Acute angle");
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(89)).toEqual("Acute angle");
});

// Case 2: Right angle
// Case 3: Obtuse angles
// Case 4: Straight angle
// Case 5: Reflex angles
// Case 6: Invalid angles
test(`should return "Right angle" for exactly 90`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});

test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(135)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

test(`should return "Straight angle" for exactly 180`, () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

test(`should return "Invalid angle" for angles outside 0-360`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(-10)).toEqual("Invalid angle");
expect(getAngleType(400)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
// This statement loads the isProperFraction function you wrote in the implement directory.
// We will use the same function, but write tests for it using Jest in this file.
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
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

test(`should return true when numerator < denominator (positive)`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(3, 7)).toEqual(true);
});

test(`should return false when numerator >= denominator (positive)`, () => {
expect(isProperFraction(2, 1)).toEqual(false);
expect(isProperFraction(3, 3)).toEqual(false);
});

test(`should handle negative numerator`, () => {
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(-3, 2)).toEqual(false);
});

test(`should handle negative denominator`, () => {
expect(isProperFraction(1, -2)).toEqual(true);
expect(isProperFraction(3, -2)).toEqual(false);
});
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
// This statement loads the getCardValue function you wrote in the implement directory.
// We will use the same function, but write tests for it using Jest in this file.
const getCardValue = require("../implement/3-get-card-value");

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

// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
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
test(`Should return numeric value for number cards 2-10`, () => {
expect(getCardValue("2♥")).toEqual(2);
expect(getCardValue("5♦")).toEqual(5);
expect(getCardValue("10♥")).toEqual(10);
});

// 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
test(`Should return 10 for face cards J, Q, K`, () => {
expect(getCardValue("J♣")).toEqual(10);
expect(getCardValue("Q♦")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
});

test(`Should throw an error for invalid cards`, () => {
expect(() => getCardValue("invalid")).toThrow();
expect(() => getCardValue("1♠")).toThrow();
expect(() => getCardValue("")).toThrow();
});
6 changes: 5 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;
for (const char of stringOfCharacters) {
if (char === findCharacter) count++;
}
return count;
}

module.exports = countChar;
8 changes: 3 additions & 5 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ test("should count multiple occurrences of a character", () => {
expect(count).toEqual(5);
});

// Scenario: No Occurrences
// Given the input string `str`,
// And a character `char` that does not exist within `str`.
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of `char` were found.
test("should return 0 when character does not occur", () => {
expect(countChar("hello", "z")).toEqual(0);
});
8 changes: 7 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function getOrdinalNumber(num) {
return "1st";
const lastTwo = num % 100;
const lastOne = num % 10;
if (lastTwo >= 11 && lastTwo <= 13) return `${num}th`;
if (lastOne === 1) return `${num}st`;
if (lastOne === 2) return `${num}nd`;
if (lastOne === 3) return `${num}rd`;
return `${num}th`;
}

module.exports = getOrdinalNumber;
19 changes: 19 additions & 0 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,22 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(12)).toEqual("12th");
});

test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(13)).toEqual("13th");
});

test("should append 'th' for all other numbers including 11, 12, 13", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(20)).toEqual("20th");
});
9 changes: 5 additions & 4 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
function repeatStr() {
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
// The goal is to re-implement that function, not to use it.
return "hellohellohello";
function repeatStr(str, count) {
if (count < 0) throw new Error("count must be non-negative");
let result = "";
for (let i = 0; i < count; i++) result += str;
return result;
}

module.exports = repeatStr;
21 changes: 9 additions & 12 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ test("should repeat the string count times", () => {
expect(repeatedStr).toEqual("hellohellohello");
});

// Case: handle count of 1:
// Given a target string `str` and a `count` equal to 1,
// When the repeatStr function is called with these inputs,
// Then it should return the original `str` without repetition.
test("should return the original string when count is 1", () => {
expect(repeatStr("hello", 1)).toEqual("hello");
});

// Case: Handle count of 0:
// Given a target string `str` and a `count` equal to 0,
// When the repeatStr function is called with these inputs,
// Then it should return an empty string.
test("should return empty string when count is 0", () => {
expect(repeatStr("hello", 0)).toEqual("");
});

// Case: Handle negative count:
// Given a target string `str` and a negative integer `count`,
// When the repeatStr function is called with these inputs,
// Then it should throw an error, as negative counts are not valid.
test("should throw an error when count is negative", () => {
expect(() => repeatStr("hello", -1)).toThrow();
});
5 changes: 0 additions & 5 deletions Sprint-3/3-dead-code/exercise-1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
// Find the instances of unreachable and redundant code - remove them!
// The sayHello function should continue to work for any reasonable input it's given.

let testName = "Jerry";
const greeting = "hello";

function sayHello(greeting, name) {
const greetingStr = greeting + ", " + name + "!";
return `${greeting}, ${name}!`;
console.log(greetingStr);
}

testName = "Aman";
Expand Down
7 changes: 1 addition & 6 deletions Sprint-3/3-dead-code/exercise-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
// The countAndCapitalisePets function should continue to work for any reasonable input it's given, and you shouldn't modify the pets variable.

const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"];
const capitalisedPets = pets.map((pet) => pet.toUpperCase());
const petsStartingWithH = pets.filter((pet) => pet[0] === "h");

function logPets(petsArr) {
petsArr.forEach((pet) => console.log(pet));
}

function countAndCapitalisePets(petsArr) {
const petCount = {};
Expand All @@ -23,6 +17,7 @@ function countAndCapitalisePets(petsArr) {
return petCount;
}

const petsStartingWithH = pets.filter((pet) => pet[0] === "h");
const countedPetsStartingWithH = countAndCapitalisePets(petsStartingWithH);

console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log
26 changes: 26 additions & 0 deletions Sprint-3/4-stretch/card-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Validates a credit card number string against the following rules:
// - Must be exactly 16 digits (no letters or other characters)
// - Must contain at least two different digits
// - The final digit must be even
// - The sum of all digits must be greater than 16

function isValidCardNumber(cardNumber) {
// Rule 1: must be exactly 16 digit characters
if (!/^\d{16}$/.test(cardNumber)) return false;

const digits = cardNumber.split("").map(Number);

// Rule 2: at least two different digits must be present
if (new Set(digits).size < 2) return false;

// Rule 3: final digit must be even
if (digits[15] % 2 !== 0) return false;

// Rule 4: sum of all digits must be greater than 16
const sum = digits.reduce((acc, d) => acc + d, 0);
if (sum <= 16) return false;

return true;
}

module.exports = isValidCardNumber;
Loading
Loading