Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
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 i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters[i] ===findCharacter) {
count++;
}
}
return count;
}

module.exports = countChar;
21 changes: 21 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,24 @@ test("should count multiple occurrences of a character", () => {
// 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.

test("returns 0 if the character is not in the string", () => {
const str = "bread";
const char = "z";
const count = countChar(str, char);
expect(count).toBe(0);
});

test("counts a character that appears once", () => {
const str = "bread";
const char = "d";
const count = countChar(str, char);
expect(count).toBe(1);
});

test("counts how many times a character appears", () => {
const str = "breadboard";
const char = "b";
const count = countChar(str, char);
expect(count).toBe(2);
});
16 changes: 15 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,19 @@
function getOrdinalNumber(num) {
return "1st";
const lastTwoDigits = num % 100;
if (lastTwoDigits >= 11 && lastTwoDigits <= 13){
return num+ "th";
}
const lastDigit = num %10;
switch (lastDigit){
case 1:
return num + "st";
case 2:
return num + "nd";
case 3:
return num + "rd";
default:
return num + "th";
}
}

module.exports = getOrdinalNumber;
39 changes: 38 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 @@ -8,6 +8,43 @@ const getOrdinalNumber = require("./get-ordinal-number");
// When the number is 1,
// Then the function should return "1st"

test("should return '1st' for 1", () => {
test("1 becomes '1st'", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});

test("2 becomes '2nd'", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
});

test("3 becomes '3rd'", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
});

test("4 becomes '4th'", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
});

test("11, 12, 13 and 111, 112, 113 all become 'th' endings", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(111)).toEqual("111th");
expect(getOrdinalNumber(112)).toEqual("112th");
expect(getOrdinalNumber(113)).toEqual("113th");
});

test("21, 22, 23 and 101, 102, 103 get correct 'st', 'nd', 'rd' endings", () => {
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(101)).toEqual("101st");
expect(getOrdinalNumber(102)).toEqual("102nd");
expect(getOrdinalNumber(103)).toEqual("103rd");
});

test("5, 10, 24, 100 all end with 'th'", () => {
expect(getOrdinalNumber(5)).toEqual("5th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(24)).toEqual("24th");
expect(getOrdinalNumber(100)).toEqual("100th");
});
9 changes: 6 additions & 3 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function repeat() {
return "hellohellohello";
function repeatStr(str, count) {
if (count < 0) {
throw new Error("count cannot be negative");
}
return str.repeat(count);
}

module.exports = repeat;
module.exports = repeatStr;
24 changes: 22 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Implement a function repeat
const repeat = require("./repeat");
const repeatStr = require("./repeat");
// Given a target string str and a positive integer count,
// When the repeat function is called with these inputs,
// Then it should:
Expand All @@ -12,7 +12,7 @@ const repeat = require("./repeat");
test("should repeat the string count times", () => {
const str = "hello";
const count = 3;
const repeatedStr = repeat(str, count);
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("hellohellohello");
});

Expand All @@ -21,12 +21,32 @@ test("should repeat the string count times", () => {
// When the repeat function is called with these inputs,
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.

test("returns the original string when count is 1", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toBe("hello");
});

// case: Handle Count of 0:
// Given a target string str and a count equal to 0,
// When the repeat function is called with these inputs,
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.

test("returns an empty string when count is 0", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toBe("");
});

// case: Negative Count:
// Given a target string str and a negative integer count,
// When the repeat function is called with these inputs,
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.

test("throws an error when count is negative", () => {
const str = "hello";
const count = -3;
expect(() => repeatStr(str, count)).toThrow("count cannot be negative");
});
Loading