Skip to content

Commit 7bb71a1

Browse files
Complete Sprint 3 practice TDD tasks
1 parent 3372770 commit 7bb71a1

File tree

13 files changed

+199
-29
lines changed

13 files changed

+199
-29
lines changed

Sprint-3/2-practice-tdd/count.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
let count =0;
3+
for(let i=0; i<stringOfCharacters.length; i++){
4+
if(stringOfCharacters[i] === findCharacter){
5+
count=count+1;}}
6+
return count;
37
}
4-
8+
console.log(countChar("banana","a"))//3
59
module.exports = countChar;

Sprint-3/2-practice-tdd/count.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// implement a function countChar that counts the number of times a character occurs in a string
22
const countChar = require("./count");
3+
34
// Given a string `str` and a single character `char` to search for,
45
// When the countChar function is called with these inputs,
56
// Then it should:
@@ -22,3 +23,32 @@ test("should count multiple occurrences of a character", () => {
2223
// And a character `char` that does not exist within `str`.
2324
// When the function is called with these inputs,
2425
// Then it should return 0, indicating that no occurrences of `char` were found.
26+
27+
test("should return 0 when character does not exist in string", () => {
28+
const str = "welcome";
29+
const char = "z";
30+
const count = countChar(str, char);
31+
expect(count).toEqual(0);
32+
});
33+
34+
// Scenario: Single Occurrence
35+
// Given a string where the character appears exactly once,
36+
// Then it should return 1.
37+
38+
test("should return 1 when character appears once", () => {
39+
const str = "apple";
40+
const char = "a";
41+
const count = countChar(str, char);
42+
expect(count).toEqual(1);
43+
});
44+
45+
// Scenario: Empty String
46+
// Given an empty string,
47+
// Then it should return 0 for any character.
48+
49+
test("should return 0 for empty string", () => {
50+
const str = "";
51+
const char = "a";
52+
const count = countChar(str, char);
53+
expect(count).toEqual(0);
54+
});
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1+
12
function getOrdinalNumber(num) {
2-
return "1st";
3+
const lastTwoDigits = num % 100;
4+
5+
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
6+
return num + "th";
7+
}
8+
9+
const lastDigit = num % 10;
10+
11+
if (lastDigit === 1) return num + "st";
12+
if (lastDigit === 2) return num + "nd";
13+
if (lastDigit === 3) return num + "rd";
14+
15+
return num + "th";
316
}
417

518
module.exports = getOrdinalNumber;

Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,44 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1818
expect(getOrdinalNumber(21)).toEqual("21st");
1919
expect(getOrdinalNumber(131)).toEqual("131st");
2020
});
21+
22+
// Case 2: Numbers ending with 2 (but not 12)
23+
// When the number ends with 2, except those ending with 12,
24+
// Then the function should return a string by appending "nd" to the number.
25+
test("should append 'nd' for numbers ending with 2, except those ending with 12", () => {
26+
expect(getOrdinalNumber(2)).toEqual("2nd");
27+
expect(getOrdinalNumber(22)).toEqual("22nd");
28+
expect(getOrdinalNumber(102)).toEqual("102nd");
29+
});
30+
31+
// Case 3: Numbers ending with 3 (but not 13)
32+
// When the number ends with 3, except those ending with 13,
33+
// Then the function should return a string by appending "rd" to the number.
34+
test("should append 'rd' for numbers ending with 3, except those ending with 13", () => {
35+
expect(getOrdinalNumber(3)).toEqual("3rd");
36+
expect(getOrdinalNumber(23)).toEqual("23rd");
37+
expect(getOrdinalNumber(203)).toEqual("203rd");
38+
});
39+
40+
// Case 4: Numbers ending with 11, 12, 13
41+
// When the number ends with 11, 12, or 13,
42+
// Then the function should return a string by appending "th" to the number.
43+
test("should append 'th' for numbers ending with 11, 12, or 13", () => {
44+
expect(getOrdinalNumber(11)).toEqual("11th");
45+
expect(getOrdinalNumber(12)).toEqual("12th");
46+
expect(getOrdinalNumber(13)).toEqual("13th");
47+
expect(getOrdinalNumber(111)).toEqual("111th");
48+
expect(getOrdinalNumber(112)).toEqual("112th");
49+
expect(getOrdinalNumber(113)).toEqual("113th");
50+
});
51+
52+
// Case 5: All other numbers
53+
// When the number does not end with 1, 2, 3 (or ends with 11, 12, 13),
54+
// Then the function should return a string by appending "th" to the number.
55+
test("should append 'th' for all other numbers", () => {
56+
expect(getOrdinalNumber(4)).toEqual("4th");
57+
expect(getOrdinalNumber(10)).toEqual("10th");
58+
expect(getOrdinalNumber(14)).toEqual("14th");
59+
expect(getOrdinalNumber(20)).toEqual("20th");
60+
expect(getOrdinalNumber(100)).toEqual("100th");
61+
});
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
function repeatStr() {
2-
return "hellohellohello";
3-
}
1+
function repeatStr(str, count) {
2+
if (count < 0) {
3+
throw new Error("Count cannot be negative");
4+
}
45

6+
return str.repeat(count);
7+
}
58
module.exports = repeatStr;

Sprint-3/2-practice-tdd/repeat-str.test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Implement a function repeatStr
22
const repeatStr = require("./repeat-str");
3+
34
// Given a target string `str` and a positive integer `count`,
45
// When the repeatStr function is called with these inputs,
56
// Then it should:
@@ -8,7 +9,6 @@ const repeatStr = require("./repeat-str");
89
// Given a target string `str` and a positive integer `count` greater than 1,
910
// When the repeatStr function is called with these inputs,
1011
// Then it should return a string that contains the original `str` repeated `count` times.
11-
1212
test("should repeat the string count times", () => {
1313
const str = "hello";
1414
const count = 3;
@@ -20,13 +20,31 @@ test("should repeat the string count times", () => {
2020
// Given a target string `str` and a `count` equal to 1,
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
23+
test("should return original string when count is 1", () => {
24+
const str = "hello";
25+
const count = 1;
26+
const repeatedStr = repeatStr(str, count);
27+
expect(repeatedStr).toEqual("hello");
28+
});
2329

2430
// Case: Handle count of 0:
2531
// Given a target string `str` and a `count` equal to 0,
2632
// When the repeatStr function is called with these inputs,
2733
// Then it should return an empty string.
34+
test("should return empty string when count is 0", () => {
35+
const str = "hello";
36+
const count = 0;
37+
const repeatedStr = repeatStr(str, count);
38+
expect(repeatedStr).toEqual("");
39+
});
2840

2941
// Case: Handle negative count:
3042
// Given a target string `str` and a negative integer `count`,
3143
// When the repeatStr function is called with these inputs,
3244
// Then it should throw an error, as negative counts are not valid.
45+
test("should throw error when count is negative", () => {
46+
const str = "hello";
47+
const count = -2;
48+
49+
expect(() => repeatStr(str, count)).toThrow();
50+
});

Sprint-3/3-dead-code/exercise-1.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
// Find the instances of unreachable and redundant code - remove them!
2-
// The sayHello function should continue to work for any reasonable input it's given.
2+
// The sayHello function should continue to work for any reasonable input it's given
33

4-
let testName = "Jerry";
4+
let testName = "Jerry"; // Redundant assignment: This value is overwritten before being used.
55
const greeting = "hello";
66

77
function sayHello(greeting, name) {
8-
const greetingStr = greeting + ", " + name + "!";
9-
return `${greeting}, ${name}!`;
10-
console.log(greetingStr);
8+
const greetingStr = greeting + ", " + name + "!";
9+
// ⚠ Not redundant by itself, but becomes redundant because it is only used in unreachable code below.
10+
11+
return `${greeting}, ${name}!`;
12+
13+
14+
console.log(greetingStr);
15+
// Unreachable code: This will never execute because it is after the return statement.
1116
}
1217

13-
testName = "Aman";
18+
testName = "Aman";
19+
// This overwrites "Jerry", so the original assignment is redundant.
1420

1521
const greetingMessage = sayHello(greeting, testName);
1622

17-
console.log(greetingMessage); // 'hello, Aman!'
23+
console.log(greetingMessage); // 'hello, Aman!'

Sprint-3/3-dead-code/exercise-2.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@
22
// The countAndCapitalisePets function should continue to work for any reasonable input it's given, and you shouldn't modify the pets variable.
33

44
const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"];
5-
const capitalisedPets = pets.map((pet) => pet.toUpperCase());
6-
const petsStartingWithH = pets.filter((pet) => pet[0] === "h");
75

8-
function logPets(petsArr) {
9-
petsArr.forEach((pet) => console.log(pet));
10-
}
6+
const petsStartingWithH = pets.filter((pet) => pet[0] === "h");
117

128
function countAndCapitalisePets(petsArr) {
139
const petCount = {};
@@ -20,9 +16,10 @@ function countAndCapitalisePets(petsArr) {
2016
petCount[capitalisedPet] = 1;
2117
}
2218
});
19+
2320
return petCount;
2421
}
2522

2623
const countedPetsStartingWithH = countAndCapitalisePets(petsStartingWithH);
2724

28-
console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log
25+
console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 }

Sprint-3/4-stretch/find.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@ console.log(find("code your future", "z"));
2020
// Pay particular attention to the following:
2121

2222
// a) How the index variable updates during the call to find
23+
// The variable `index` starts at 0.
24+
// Each time the loop runs, `index++` increases its value by 1.
25+
// So index becomes 0, then 1, then 2, then 3, and so on,
26+
// until it reaches the length of the string.
27+
// This allows the function to check each character one by one.
28+
29+
2330
// b) What is the if statement used to check
31+
// The if statement checks whether the current character in the string
32+
// (`str[index]`) is equal to the character we are searching for (`char`).
33+
// If they are equal, the function returns the index immediately.
34+
// This means the function finds the first occurrence of the character.
35+
36+
2437
// c) Why is index++ being used?
38+
// `index++` moves the search to the next position in the string.
39+
// Without `index++`, the value of index would never change,
40+
// and the loop would run forever (infinite loop).
41+
42+
2543
// d) What is the condition index < str.length used for?
44+
// The condition `index < str.length` makes sure the loop stops
45+
// when we reach the end of the string.
46+
// It prevents accessing characters outside the string.
47+
// If no match is found, the function returns -1.

Sprint-3/4-stretch/password-validator.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
function passwordValidator(password) {
22
return password.length < 5 ? false : true
3+
4+
35
}
46

57

0 commit comments

Comments
 (0)