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 total = 0;
for (let i=0; i < stringOfCharacters.length; i++){
if (findCharacter == stringOfCharacters[i]){
total++;
}
}
return total;
}

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 @@ -18,7 +18,19 @@ test("should count multiple occurrences of a character", () => {
});

// Scenario: No Occurrences
test("No Occurrences", () => {
const str = "bcdefg";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});
// Given the input string str,
// 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("Case-sensitive str", () => {
const str = "Abcdefg";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});
24 changes: 21 additions & 3 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
function getOrdinalNumber(num) {
return "1st";
}

let result;
let lastTwoDigits = num % 100;
let lastDigit = num % 10;
if (lastTwoDigits == 11 || lastTwoDigits == 12 || lastTwoDigits == 13){
result = num.toString() + "th";
}
else if (lastDigit == 1){
result = num.toString() +"st";
}
else if (lastDigit == 2){
result = num.toString() + "nd";
}
else if (lastDigit == 3){
result = num.toString() + "rd";
}
else {
result = num.toString() + "th";
}

return result;
}
module.exports = getOrdinalNumber;
37 changes: 35 additions & 2 deletions 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,39 @@ const getOrdinalNumber = require("./get-ordinal-number");
// When the number is 1,
// Then the function should return "1st"

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

test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
expect( getOrdinalNumber(2) ).toEqual("2nd");
expect( getOrdinalNumber(22) ).toEqual("22nd");
expect( getOrdinalNumber(132) ).toEqual("132nd");
});

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

test("append 'th' to numbers ending in 4 and more", () => {
expect( getOrdinalNumber(4) ).toEqual("4th");
expect( getOrdinalNumber(25) ).toEqual("25th");
expect( getOrdinalNumber(139) ).toEqual("139th");
});

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");
});

15 changes: 13 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
function repeat() {
return "hellohellohello";
function repeat(str, count) {

if (count < 0){
throw new Error("Count must be positive number!");
}
else if (count == 0){
return "";
}
let result = "";
for ( let i=0; i<count; i++){
result +=str;
}
return result;
}

module.exports = repeat;
17 changes: 15 additions & 2 deletions Sprint-3/2-practice-tdd/repeat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@ test("should repeat the string count times", () => {
// Given a target string str and a count equal to 1,
// 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("handle Count of 1", () => {
const str = "hello";
const count = 1;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("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("handle Count of 0", () => {
const str = "hello";
const count = 0;
const repeatedStr = repeat(str, count);
expect(repeatedStr).toEqual("");
});
// 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("Negative Count", () => {
expect(() => repeat("hello", -1)).toThrow("Count must be positive number!");
});
Loading