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
14 changes: 12 additions & 2 deletions Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
}
if(stringOfCharacters === "" || findCharacter === ""){
throw new Error("string an char are not given")
}

const str = stringOfCharacters.split("")
let count = 0;
for(const char of str){
if(char === findCharacter){
count++
}
}
return count
}
module.exports = countChar;
8 changes: 8 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ test("should count multiple occurrences of a character", () => {

// Scenario: No Occurrences
// Given the input string `str`,

// And a character `char` that does not exist within `str`.
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of `char` were found.

test("should return 0 when no occurrence of the char", () => {
const str = "aeiou";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual(0);
})
28 changes: 27 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,31 @@
function getOrdinalNumber(num) {
return "1st";
const numberStr1 = String(num).slice(-1)
const numberStr2 = String(num).slice(-2)

let number1 = Number(numberStr1)
let number2 = Number(numberStr2)

if (number2 === 11 || number2 === 12 || number2 === 13) {
return `${num}th`
}
else {

switch (number1) {
case 1:
return`${num}st`
break;
case 2:
return `${num}nd`
break;
case 3:
return `${num}rd`
break;
default:
return`${num}th`;
}
}

}


module.exports = getOrdinalNumber;
34 changes: 31 additions & 3 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,35 @@ const getOrdinalNumber = require("./get-ordinal-number");
// When the number ends with 1, except those ending with 11,
// Then the function should return a string by appending "st" to the number.
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(131)).toEqual("131st");
});

test("should append 'nd' for numbers ending with 2", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(32)).toEqual("32nd");

});

test("should append 'rd' for numbers ending with 3", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(333)).toEqual("333rd");
expect(getOrdinalNumber(303)).toEqual("303rd");

});

test("should append 'th' to numbers ending in 0, 4, 5, 6, 7, 8, 9, and to 11, 12, and 13", () => {
expect(getOrdinalNumber(0)).toEqual("0th");
expect(getOrdinalNumber(44)).toEqual("44th");
expect(getOrdinalNumber(65)).toEqual("65th");
expect(getOrdinalNumber(46)).toEqual("46th");
expect(getOrdinalNumber(77)).toEqual("77th");
expect(getOrdinalNumber(98)).toEqual("98th");
expect(getOrdinalNumber(99)).toEqual("99th");
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
})
;
27 changes: 22 additions & 5 deletions Sprint-3/2-practice-tdd/repeat-str.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
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(word, wordCounter) {
// 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.
let count = 0;
let repeatedString = []

if (wordCounter < 0) { // rejects negative numbers
throw new Error("Enter a positive number")
}
if(wordCounter === 0){
return ""
}
if(wordCounter === 1){
return word
}
if(wordCounter > 1){
while(count < wordCounter){
count++
repeatedString.push(word)
}
}
return repeatedString.join("")
}
module.exports = repeatStr;
19 changes: 19 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,32 @@ 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.
test("should repeat the string 1 time", () =>{
const str = "name";
const count = 1;
const repeatedStr = repeatStr(str, count);
expect(repeatedStr).toEqual("name")
})

// 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", () =>{
const str = "name";
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.
// 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("throws on negative number", () => {
expect(() => repeatStr("dami",-5)).toThrow("Enter a positive number");
});
Loading