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
10 changes: 9 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;
str = stringOfCharacters
char = findCharacter
for (let i = 0; i < str.length; i++) {
if (str[i] === char) {
count++;
}
}
return count;
}

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

test("should count multiple occurrences of a character", () => {
const str = "Code your Future";
const char = "u";
const count = countChar(str, char);
expect(count).toEqual(3);
});

test("should count multiple occurrences of a character", () => {
const str = "Introduction to Programming";
const char = "o";
const count = countChar(str, char);
expect(count).toEqual(4);
});

test("should count multiple occurrences of a character", () => {
const str = "Introduction to Javascript";
const char = "i";
const count = countChar(str, char);
expect(count).toEqual(2);
});
// Scenario: No Occurrences
// Given the input string `str`,
// And a character `char` that does not exist within `str`.
Expand Down
5 changes: 4 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,8 @@
function getOrdinalNumber(num) {
return "1st";
const s = ["th", "st", "nd", "rd"];
const lastDigits = num % 100;
// Uses the index 1, 2, or 3 for st/nd/rd, defaults to 0 (th)
return num + (s[(lastDigits - 20) % 10] || s[lastDigits] || s[0]);
}

module.exports = getOrdinalNumber;
24 changes: 24 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,27 @@ 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("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(132)).toEqual("132nd");
});

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

test("should append 'th' for numbers ending with 4-9, 0, and teens (11-13)", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(5)).toEqual("5th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(70)).toEqual("70th");
expect(getOrdinalNumber(40)).toEqual("40th");
});

11 changes: 6 additions & 5 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
function repeatStr() {
// 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";
function repeatStr(str, count) {
let result = "";
for (let i = 0; i < count; i++) {
result += str;
}
return result;
}

module.exports = repeatStr;
28 changes: 28 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,34 @@ test("should repeat the string count times", () => {
expect(repeatedStr).toEqual("hellohellohello");
});

test("should repeat the string count times", () => {
const str = "CYF";
const count = 3;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("CYFCYFCYF");
});

test("should repeat the string count times", () => {
const str = "Code Your Future";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("Code Your Future");
});

test("should repeat the string count times", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("");
});

test("should repeat the string count times", () => {
const str = "ITP";
const count = 5;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("ITPITPITPITPITP");
});

// 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,
Expand Down
Loading