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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
node_modules
.DS_Store
.vscode
**/.DS_Store
.vscode/
**/.DS_Store
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;
12 changes: 12 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,15 @@ test("should count multiple occurrences of a character", () => {
// 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 count multiple occurrences of a character", () => {
expect(countChar("aaaaa", "a")).toEqual(5);
expect(countChar("blind", "a")).toEqual(0);
expect(countChar("blood", "o")).toEqual(2);
expect(countChar("bbbrf", "b")).toEqual(3);
expect(countChar("ooooa", "o")).toEqual(4);
});

// handling invalid input
// the tests work assuming that only letters are in the string
// numbers and special characters are not tested for
17 changes: 16 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,20 @@
function getOrdinalNumber(num) {
return "1st";
// Check if input is a number
if (!Number.isInteger(num) || !Number.isFinite(num) || num <= 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Number.isFinite() is also redundant

  • Good thinking about rejecting negative numbers. In some context such as in math, the ordinal number of 0 is 0th.

throw new Error("Input must be a valid number");
}

const lastNum = num % 10; // checks what last number is
const last2Num = num % 100; // checks what last two numbers are, needed to check for 11

if (last2Num >= 11 && last2Num <= 13) {
return num + "th";
}
if (lastNum === 1) return num + "st";
if (lastNum === 2) return num + "nd";
if (lastNum === 3) return num + "rd";

return num + "th";
}

module.exports = getOrdinalNumber;
33 changes: 32 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,39 @@ const getOrdinalNumber = require("./get-ordinal-number");
// Case 1: Numbers ending with 1 (but not 11)
// When the number ends with 1, except those ending with 11,
// Then the function should return a string by appending "st" to the number.
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {

test("should append 'st' for numbers ending with 1, except 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

// Case 2: Numbers ending with 2
// When the number ends with 2,
// Then the function should return a string by appending "nd" to the number.

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

// Case 3: Numbers ending with 3
// When the number ends with 3,
// Then the function should return a string by appending "rd" to the number.

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

// Case 4: The remaining numbers
// For numbers ending in 0, 4, 5, 6, 7, 8, 9, 11, 12, 13
// the function should return a string by appending "th" to the number.

test("should append 'th' if number is ending in 0, 4, 5,6, 7, 8, 9, 10, 11, 12 or 13", () => {
expect(getOrdinalNumber(20)).toEqual("20th");
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(99)).toEqual("99th");
});
12 changes: 10 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
function repeatStr() {
return "hellohellohello";
function repeatStr(str, count) {
if (count < 0) {
throw new Error("Count must be positive");
}

if (count === 0) {
return " ";
}

return str.repeat(count);
}

module.exports = repeatStr;
39 changes: 29 additions & 10 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,47 @@ const repeatStr = require("./repeat-str");
// Then it should:

// Case: handle multiple repetitions:
// Given a target string `str` and a positive integer `count` greater than 1,
// When the repeatStr function is called with these inputs,
// Then it should return a string that contains the original `str` repeated `count` times.
// A string is repeated count number of times
// If a string is "hello" and count is 3 then
// the hello will be output 3 times with no spaces between

test("should repeat the string count times", () => {
test("when count equals 3 it should repeat the string 3 times without spaces", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeatStr(str, count);
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.
// If count is 1 then the string is not repeated and
// will be output for example if the string is "Hello"
// then the output will be "Hello"

test("when count equals 1 it produce the string", () => {
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.
// If the the string is empty then count will be 0
// and an empty string will be returned

test("should return an empty string when count = 0 ", () => {
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 throw an error when count is negative", () => {
const str = "hello";
const count = -3;
expect(() => repeatStr(str, count)).toThrow("Count must be positive");
});
Loading