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
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;
9 changes: 8 additions & 1 deletion Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ test("should count multiple occurrences of a character", () => {
expect(count).toEqual(5);
});

// Scenario: No Occurrences
//Scenario: No Occurrences
test("should return 0 when the character does not exist in the string", () => {
const str = "hello";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Given the input string `str`,
// And a character `char` that does not exist within `str`.
// When the function is called with these inputs,
Expand Down
20 changes: 18 additions & 2 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
function getOrdinalNumber(num) {
return "1st";
}
let lastDigit = num % 10;
let lastTwoDigits = num % 100;
if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) {
return `${num}th`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Did you remember to run your IDE's formmatter here?

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

module.exports = getOrdinalNumber;
9 changes: 9 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,12 @@ 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("converts 1 to an ordinal number", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(11)).toEqual("11th");
});
test("converts numbers to ordinal number", () => {
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(33)).toEqual("33rd");
expect(getOrdinalNumber(13)).toEqual("13th");
});
10 changes: 8 additions & 2 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
function repeatStr() {
function repeatStr(str, count) {
let result = "";
for (let i =0; i<count;i++){
result +=str;
}

// 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";
return result;
}
console.log(repeatStr("hello",3))

module.exports = repeatStr;
22 changes: 22 additions & 0 deletions Sprint-3/2-practice-tdd/repeat-str.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,35 @@ test("should repeat the string count times", () => {
// 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.
// Case: handle count of 1:
test("should return the original string unchanged when count is 1", () => {
const str = "apple";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("apple");
});


// 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.

test("should return an empty string when count is 0", () => {
const str = "world";
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 a negative integer", () => {
const str = "test";
const count = -5;
expect(() => repeatStr(str, count)).toThrow();
});

Loading