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,8 +16,28 @@

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){
return "Reflex angle";
}
else {
return "Invalid angle";
}
}


// The line below allows us to load the getAngleType function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = getAngleType;
Expand All @@ -35,3 +55,18 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");
let reflex = getAngleType(280);
assertEquals(reflex, "Reflex angle");
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");
let invalid = getAngleType(360);
assertEquals(invalid,"Invalid angle");
invalid = getAngleType(-1);
assertEquals(invalid, "Invalid angle");
invalid = getAngleType(0);
assertEquals(invalid, "Invalid angle");
let obtuse = getAngleType(160);
assertEquals(obtuse, "Obtuse angle");
let acute = getAngleType(10);
assertEquals(acute, "Acute angle");

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (numerator < denominator){
return true;
}
else{
return false;
}
}

// console.log(isProperFraction(10,22))
// 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;
Expand All @@ -31,3 +37,7 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(10,20), true);
assertEquals(isProperFraction(80, 10), false);
assertEquals(isProperFraction(8, 16), true);
assertEquals(isProperFraction(10, 2), false);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@

function getCardValue(card) {
// TODO: Implement this function
if (typeof card !== "string" || card.length < 2) {
throw new Error("Invalid card format");
}
const rank = card.slice(0, -1);
const suit = card.slice(-1);

const validSuits = ["♠", "♥", "♦", "♣"];
if (!validSuits.includes(suit)) {
throw new Error("Invalid suit");
}
if (rank === "A") {
return 11;
}
if (["J", "Q", "K"].includes(rank)) {
return 10;
}
const numericValue = parseInt(rank, 10);
if (
!isNaN(numericValue) &&
numericValue >= 2 &&
numericValue <= 10 &&
String(numericValue) === rank
) {
return numericValue;
}
throw new Error("Invalid rank");
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand Down Expand Up @@ -52,3 +78,18 @@ try {
}

// What other invalid card cases can you think of?
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("2♥"), 2);
assertEquals(getCardValue("10♥"), 10);
assertEquals(getCardValue("A♣"), 11);
assertEquals(getCardValue("J♦"), 10);
assertEquals(getCardValue("Q♠"), 10);
assertEquals(getCardValue("K♣"), 10);

assertThrows("invalid");
assertThrows("A");
assertThrows("♠");
assertThrows("1♥");
assertThrows("Z♠");
assertThrows("10");
assertThrows("A♥♠");
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,35 @@ 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 is exactly 90`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});

// Case 3: Obtuse angles
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");
});

// Case 4: Straight angle
test(`should return "Straight angle" when angle is exactly 180`, () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Reflex angles
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");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" for boundaries and out-of-bounds numbers`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(-10)).toEqual("Invalid angle");
expect(getAngleType(361)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,31 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

// Case 1: Standard Positive Numbers
test("should handle standard positive fractions correctly", () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(5, 3)).toEqual(false);
});

// Case 2: Equal Numbers (Boundary)
test("should return false when numerator and denominator are equal", () => {
expect(isProperFraction(5, 5)).toEqual(false); // Equals 1 whole (Improper)
});

// Case 3: Numerator is Zero
test("should return true when numerator is zero and denominator is positive", () => {
expect(isProperFraction(0, 5)).toEqual(true);
});

// Case 4: Negative Numbers
test("should handle negative integers according to mathematical comparison rules", () => {
expect(isProperFraction(-5, -2)).toEqual(true);
expect(isProperFraction(-1, -4)).toEqual(false);
});

// Case 5: Mixed Positive and Negative Numbers
test("should return true when only the numerator is negative", () => {
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(1, -2)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});

// Case 2: Number Cards (2-10)
test("Should return the matching numeric value for number cards (2-10)", () => {
expect(getCardValue("2♥")).toEqual(2);
expect(getCardValue("5♦")).toEqual(5);
expect(getCardValue("10♣")).toEqual(10);
});

// Case 3: Face Cards (J, Q, K)
test("Should return 10 when given face cards (J, Q, K)", () => {
expect(getCardValue("J♣")).toEqual(10);
expect(getCardValue("Q♦")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
});

// Case 4: Invalid Cards

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

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
Expand Down
9 changes: 8 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;

for (let char of stringOfCharacters) {
if (char === findCharacter) {
count++;
}
}
return count;
}

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

// Scenario: No Occurrences
//Scenario: No Occurrences
test("should return 0 when the character does not exist in the string", () => {
const str = "hello";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Given the input string `str`,
// And a character `char` that does not exist within `str`.
// When the function is called with these inputs,
Expand Down
20 changes: 18 additions & 2 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
function getOrdinalNumber(num) {
return "1st";
}
let lastDigit = num % 10;
let lastTwoDigits = num % 100;
if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) {
return `${num}th`;
}
if (lastDigit === 1) {
return `${num}st`;
}
else if (lastDigit === 2) {
return `${num}nd`;
}
else if (lastDigit === 3) {
return `${num}rd`;
}
else {
return `${num}th`;
}
}

module.exports = getOrdinalNumber;
9 changes: 9 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,12 @@ 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("converts 1 to an ordinal number", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(11)).toEqual("11th");
});
test("converts numbers to ordinal number", () => {
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(33)).toEqual("33rd");
expect(getOrdinalNumber(13)).toEqual("13th");
});
10 changes: 8 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
function repeatStr() {
function repeatStr(str, count) {
let result = "";
for (let i =0; i<count;i++){
result +=str;
}

// 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";
return result;
}
console.log(repeatStr("hello",3))

module.exports = repeatStr;
22 changes: 22 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,35 @@ test("should repeat the string count times", () => {
// 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.
// Case: handle count of 1:
test("should return the original string unchanged when count is 1", () => {
const str = "apple";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("apple");
});


// 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 an empty string when count is 0", () => {
const str = "world";
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 throw an error when count is a negative integer", () => {
const str = "test";
const count = -5;
expect(() => repeatStr(str, count)).toThrow();
});

3 changes: 0 additions & 3 deletions Sprint-3/3-dead-code/exercise-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ 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
14 changes: 14 additions & 0 deletions Sprint-3/4-stretch/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ console.log(find("code your future", "z"));
// Pay particular attention to the following:

// a) How the index variable updates during the call to find
// It always starts at 0, the first letter of the string. On each loop
// iteration that doesn't find a match, index++ runs, increasing its value by 1

// b) What is the if statement used to check
// If it evaluates to true, it stops the
// entire function immediately and hands back that position number.

// c) Why is index++ being used?
// It forces the loop to move to the next letter in the string for the next round.
// Without it, the loop would look at index 0 over and over again and increasing
// the index in every single turn makes sure that the loop with eventually
// be equal to the length of the string and come to an end.

// d) What is the condition index < str.length used for?
// This condition is a safety guardrail that defines the lifetime of the loop.
// It basically tells the loop to keep looping as long as
// we haven't run out of letters to look at
Loading
Loading