Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

NW6 | Pedram Amani | Module-JS1 | Week4 #191

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions week-4/implement/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,19 @@
// And a character char that does not exist within the case-sensitive str,
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.

function countChar(str, char) {
const charsArray = str.split("");
const occurrences = charsArray.filter((currentChar) => {
return currentChar === char;
});
return occurrences.length;
}

test("counts the number of times a character occurs in a string", function () {
expect(countChar("aaaaa", "a")).toBe(5);
expect(countChar("apple", "b")).toBe(0);
expect(countChar("book", "o")).toBe(2);
expect(countChar("hello", "e")).toBe(1);
expect(countChar("world", "z")).toBe(0);
});
35 changes: 35 additions & 0 deletions week-4/implement/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,38 @@

// continue testing and implementing getOrdinalNumber for additional cases
// Write your tests using Jest - remember to run your tests often for continual feedback

function getOrdinalNumber(num) {
if (!Number.isInteger(num) || num < 1) {
return "Invalid input";
}

const lastDigit = num % 10;

let suffix;
if (lastDigit === 1 && num !== 11) {
suffix = "st";
} else if (lastDigit === 2 && num !== 12) {
suffix = "nd";
} else if (lastDigit === 3 && num !== 13) {
suffix = "rd";
} else {
suffix = "th";
}

return `${num}${suffix}`;
}

test("converts any number to an ordinal number", function () {
expect(getOrdinalNumber(1)).toBe("1st");
expect(getOrdinalNumber(11)).toBe("11th");
expect(getOrdinalNumber(21)).toBe("21st");
expect(getOrdinalNumber(12)).toBe("12th");
expect(getOrdinalNumber(13)).toBe("13th");
expect(getOrdinalNumber(-36 / -12)).toBe("3rd");
expect(getOrdinalNumber(0)).toBe("Invalid input");
expect(getOrdinalNumber(-26)).toBe("Invalid input");
expect(getOrdinalNumber(3.14)).toBe("Invalid input");
expect(getOrdinalNumber(4 / 5)).toBe("Invalid input");
expect(getOrdinalNumber("Hello")).toBe("Invalid input");
});
44 changes: 44 additions & 0 deletions week-4/implement/is-prime.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
// Given a positive integer num,
// When the isPrime function is called with num as input,
// Then it should return a boolean representing whether the num is prime

function isPrime(num) {
if (isNaN(num) || num <= 0 || Number.isInteger(num) !== true) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just small refactoring is possible here: !Number.isInteger(num) instead of Number.isInteger(num) !== true

return "Invalid input, please enter a positive integer number";
} else if (num === 2) {
return true;
} else if (num % 2 === 0 || num === 1) {
return false;
} else {
for (let i = 3; i <= Math.sqrt(num); i += 2) {
if (num % i === 0) {
return false;
}
}
return true;
}
}

test("returns a boolean representing whether the num is prime", function () {
expect(isPrime(1)).toBe(false);
expect(isPrime(2)).toBe(true);
expect(isPrime(3)).toBe(true);
expect(isPrime(4)).toBe(false);
expect(isPrime(5)).toBe(true);
expect(isPrime(6)).toBe(false);
expect(isPrime(7)).toBe(true);
expect(isPrime(8)).toBe(false);
expect(isPrime(11)).toBe(true);
expect(isPrime(0)).toBe(
"Invalid input, please enter a positive integer number"
);
expect(isPrime(2.2)).toBe(
"Invalid input, please enter a positive integer number"
);
expect(isPrime(0.5)).toBe(
"Invalid input, please enter a positive integer number"
);
expect(isPrime(-7)).toBe(
"Invalid input, please enter a positive integer number"
);
expect(isPrime("abc")).toBe(
"Invalid input, please enter a positive integer number"
);
});
Loading