Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
11 changes: 10 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;

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

return count;
}

module.exports = countChar;

7 changes: 7 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,10 @@ 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("should return 0 when character does not occur in the string", () => {
const str = "hello";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});
14 changes: 13 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,17 @@
function getOrdinalNumber(num) {
return "1st";
const lastDigit = num % 10;
const lastTwoDigits = num % 100;

if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
return `${num}th`;
}


if (lastDigit === 1) return `${num}st`;
if (lastDigit === 2) return `${num}nd`;
if (lastDigit === 3) return `${num}rd`;

return `${num}th`;
}

module.exports = getOrdinalNumber;
62 changes: 53 additions & 9 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,57 @@
const getOrdinalNumber = require("./get-ordinal-number");
// In this week's prep, we started implementing getOrdinalNumber
// Case 2: Identify the ordinal number for 2
// When the number is 2,
// Then the function should return "2nd"
test("should return '2nd' for 2", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
});

// Case 3: Identify the ordinal number for 3
// When the number is 3,
// Then the function should return "3rd"
test("should return '3rd' for 3", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
});

// Case 4: Identify ordinal numbers ending in 1 but not 11
// When the number ends with 1 but is not 11,
// Then the function should return the correct 'st' suffix
test("should return '21st' for 21", () => {
expect(getOrdinalNumber(21)).toEqual("21st");
});

// Case 5: Identify ordinal numbers ending in 2 but not 12
// When the number ends with 2 but is not 12,
// Then the function should return the correct 'nd' suffix
test("should return '42nd' for 42", () => {
expect(getOrdinalNumber(42)).toEqual("42nd");
});

// continue testing and implementing getOrdinalNumber for additional cases
// Write your tests using Jest - remember to run your tests often for continual feedback
// Case 6: Identify ordinal numbers ending in 3 but not 13
// When the number ends with 3 but is not 13,
// Then the function should return the correct 'rd' suffix
test("should return '53rd' for 53", () => {
expect(getOrdinalNumber(53)).toEqual("53rd");
});

// Case 1: Identify the ordinal number for 1
// When the number is 1,
// Then the function should return "1st"
// Case 7: Identify teen exceptions 11, 12, 13
// When the number is 11, 12, or 13,
// Then the function should return the correct 'th' suffix
test("should return '11th' for 11", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
});
test("should return '12th' for 12", () => {
expect(getOrdinalNumber(12)).toEqual("12th");
});
test("should return '13th' for 13", () => {
expect(getOrdinalNumber(13)).toEqual("13th");
});

test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
// Case 8: Identify general 'th' endings
// When the number does not end in 1, 2, or 3 (except teens),
// Then the function should return the 'th' suffix
test("should return '20th' for 20", () => {
expect(getOrdinalNumber(20)).toEqual("20th");
});
test("should return '100th' for 100", () => {
expect(getOrdinalNumber(100)).toEqual("100th");
});
7 changes: 5 additions & 2 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("Count cannot be negative");
}
return str.repeat(count);
}

module.exports = repeatStr;
35 changes: 17 additions & 18 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
// Implement a function repeatStr
const repeatStr = require("./repeat-str");
// Given a target string str and a positive integer count,
// When the repeatStr function is called with these inputs,
// Then it should:

// case: repeat String:
// Given a target string str and a positive integer count,
// When the repeatStr function is called with these inputs,
// Then it should repeat the str count times and return a new string containing the repeated str values.

test("should repeat the string count times", () => {
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, ensuring that a count of 1 results in no repetition.
test("should return original string when count is 1", () => {
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, ensuring that a count of 0 results in an empty output.
test("should return empty string when count is 0", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

// case: 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 or return an appropriate error message, as negative counts are not valid.
test("should throw an error for negative count", () => {
const str = "hello";
const count = -3;
expect(() => repeatStr(str, count)).toThrow();
});