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

function getAngleType(angle) {
// TODO: Implement this function
// if angle is between 0 and 90 then return acute angle
if (angle > 0 && angle < 90) {
return "Acute angle";
}

// if it is exactly 90 return right angle
else if (angle === 90) {
return "Right angle";
}
// greater than 90 less than 180 return obtuse angle
else if (angle > 90 && angle < 180) {
return "Obtuse angle";
}
// if exactly 180 return straight angle
else if (angle === 180) {
return "Straight angle";
}
// greater than 180 less than 360 return reflex angle
else if (angle > 180 && angle < 360) {
return "Reflex angle";
}
// everything greater than 360 return invalid angle
else {
return "Invalid angle";
}
// return a string tells user which angle
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@

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

// if card is an ace return 11
if (card === "A") {
return 11;
}

// if card is face "J", "Q", "K"
else if (card === "J" || card === "Q" || card === "K") {
return 10;
}

// if card is a number card and card is bigger than 2 and less than 10 ("2" to "10"), should return its numerical value
else if (Number(card) >= 2 && Number(card) <= 10) {
return Number(card);
} else {
throw new Error("Error invalid card");
}
}

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

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

// Handling invalid cards
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,31 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// 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 angles" when (angle > 90 && < 180)`, () => {
// Test various obtuse angles, including boundary cases
expect(getAngleType(96)).toEqual("Obtuse angle");
expect(getAngleType(142)).toEqual("Obtuse angle");
expect(getAngleType(178)).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 angles" when (angle > 180 && < 360)`, () => {
// Test various reflex angles, including boundary cases
expect(getAngleType(199)).toEqual("Reflex angle");
expect(getAngleType(245)).toEqual("Reflex angle");
expect(getAngleType(306)).toEqual("Reflex angle");
});
// Case 6: Invalid angles
test(`should return "Invalid angles" when (angle > 180 && < 360)`, () => {
// Test various invalid angles, including boundary cases
expect(getAngleType(360)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,18 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

test(`for positive`, () => {
expect(isProperFraction(6, 7)).toEqual(true);
expect(isProperFraction(2, 4)).toEqual(true);
});

test(`should return false for improper fractions`, () => {
expect(isProperFraction(8, 6)).toEqual(false);
expect(isProperFraction(4, 2)).toEqual(false);
});

test(`negative combinations`, () => {
expect(isProperFraction(-5, 9)).toEqual(true);
expect(isProperFraction(-2, 0)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,35 @@ const getCardValue = require("../implement/3-get-card-value");

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

// Case 2: Face (J, Q, K)
test(`Should return 10 when given an face card`, () => {
expect(getCardValue("J")).toEqual(10);
});

test(`Should return 10 when given an face card`, () => {
expect(getCardValue("Q")).toEqual(10);
});

test(`Should return 10 when given an face card`, () => {
expect(getCardValue("K")).toEqual(10);
});
// Case 3: Number Cards (2-10)
test(`Should return numerical value when given an number card`, () => {
expect(getCardValue("5")).toEqual(5);
});

test(`Should return error if the card string is invalid`, () => {
expect(() => getCardValue("17")).toThrow("Error 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

19 changes: 18 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
// trying to find the amount of times the findCharacter is found in the stringOfCharacters
const lengthOfString = stringOfCharacters.length;
console.log("lengthOfString", lengthOfString);

// replace all instances of findCharacter in string of characters with an empty string, and then get the length. (e.g. in house replace the e with an empty string " ").
const otherLettersLength = stringOfCharacters.replaceAll(
findCharacter,
""
).length;

// declaring the variable called result and subtract otherLettersLength from the lengthOfString to find the number of occurences of findCharacter.
const result = lengthOfString - otherLettersLength;
console.log("result", result);
return result;
}

module.exports = countChar;

// function countOccurrences(str, char) {
// return str.length - str.replaceAll(char, "").length;
// }
22 changes: 22 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,30 @@ test("should count multiple occurrences of a character", () => {
expect(count).toEqual(5);
});

test("should count multiple occurrences of a character", () => {
const str = "house";
const char = "e";
const count = countChar(str, char);
expect(count).toEqual(1);
});

test("should count multiple occurrences of a character", () => {
const str = "playful";
const char = "l";
const count = countChar(str, char);
expect(count).toEqual(2);
});

// 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(``);
test("should return when there are no occurrences of a character", () => {
const str = "spaceship";
const char = "x";
const count = countChar(str, char);
expect(count).toEqual(0);
});
15 changes: 14 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,18 @@
function getOrdinalNumber(num) {
return "1st";
const lastdigit = num % 10;
if (lastdigit === 1) {
return `${num}st`;
}

if (lastdigit === 2) {
return `${num}nd`;
}

if (lastdigit === 3) {
return `${num}rd`;
} else {
return `${num}th`;
}
}

module.exports = getOrdinalNumber;
12 changes: 12 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,15 @@ 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", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(132)).toEqual("132nd");
});

test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
expect(getOrdinalNumber(133)).toEqual("133rd");
});
9 changes: 6 additions & 3 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str, count) {
if (count < 0) {
throw new Error("invalid count");
}
return str.repeat(count);
}

// call the repeat function on the string and pass in count.
module.exports = repeatStr;
21 changes: 21 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,33 @@ test("should repeat the string count times", () => {
// When the repeatStr function is called with these inputs,
// Then it should return the original `str` without repetition.

test("should repeat the string count times", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).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 repeat the string count times", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).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 repeat the string count times", () => {
const str = "hello";
const count = -2;
// const repeatedStr = repeatStr(str, count);
expect(() => repeatStr(str, count)).toThrow("invalid count");
});
2 changes: 0 additions & 2 deletions Sprint-3/3-dead-code/exercise-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ let testName = "Jerry";
const greeting = "hello";

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

testName = "Aman";
Expand Down
5 changes: 0 additions & 5 deletions Sprint-3/3-dead-code/exercise-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@
// 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 Down
Loading