Skip to content

Commit 74ecc4a

Browse files
committed
Complete practice-tdd assignments cleanly from main
1 parent b31a586 commit 74ecc4a

8 files changed

Lines changed: 122 additions & 9 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
let count = 0;
3+
4+
for (let char of stringOfCharacters) {
5+
if (char === findCharacter) {
6+
count++;
7+
}
8+
}
9+
return count;
310
}
411

512
module.exports = countChar;

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ test("should count multiple occurrences of a character", () => {
1717
expect(count).toEqual(5);
1818
});
1919

20-
// Scenario: No Occurrences
20+
//Scenario: No Occurrences
21+
test("should return 0 when the character does not exist in the string", () => {
22+
const str = "hello";
23+
const char = "z";
24+
const count = countChar(str, char);
25+
expect(count).toEqual(0);
26+
});
27+
2128
// Given the input string `str`,
2229
// And a character `char` that does not exist within `str`.
2330
// When the function is called with these inputs,
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
3-
}
2+
let lastDigit = num % 10;
3+
let lastTwoDigits = num % 100;
4+
if (lastTwoDigits === 11 || lastTwoDigits === 12 || lastTwoDigits === 13) {
5+
return `${num}th`;
6+
}
7+
if (lastDigit === 1) {
8+
return `${num}st`;
9+
}
10+
else if (lastDigit === 2) {
11+
return `${num}nd`;
12+
}
13+
else if (lastDigit === 3) {
14+
return `${num}rd`;
15+
}
16+
else {
17+
return `${num}th`;
18+
}
19+
}
420

521
module.exports = getOrdinalNumber;

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,12 @@ 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+
test("converts 1 to an ordinal number", () => {
22+
expect(getOrdinalNumber(1)).toEqual("1st");
23+
expect(getOrdinalNumber(11)).toEqual("11th");
24+
});
25+
test("converts numbers to ordinal number", () => {
26+
expect(getOrdinalNumber(21)).toEqual("21st");
27+
expect(getOrdinalNumber(33)).toEqual("33rd");
28+
expect(getOrdinalNumber(13)).toEqual("13th");
29+
});
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
function repeatStr() {
1+
function repeatStr(str, count) {
2+
let result = "";
3+
for (let i =0; i<count;i++){
4+
result +=str;
5+
}
6+
27
// 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).
38
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
9+
return result;
510
}
11+
console.log(repeatStr("hello",3))
612

713
module.exports = repeatStr;

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,35 @@ 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+
// Case: handle count of 1:
24+
test("should return the original string unchanged when count is 1", () => {
25+
const str = "apple";
26+
const count = 1;
27+
const repeatedStr = repeatStr(str, count);
28+
expect(repeatedStr).toEqual("apple");
29+
});
30+
2331

2432
// Case: Handle count of 0:
2533
// Given a target string `str` and a `count` equal to 0,
2634
// When the repeatStr function is called with these inputs,
2735
// Then it should return an empty string.
2836

37+
test("should return an empty string when count is 0", () => {
38+
const str = "world";
39+
const count = 0;
40+
const repeatedStr = repeatStr(str, count);
41+
expect(repeatedStr).toEqual("");
42+
});
43+
2944
// Case: Handle negative count:
3045
// Given a target string `str` and a negative integer `count`,
3146
// When the repeatStr function is called with these inputs,
3247
// Then it should throw an error, as negative counts are not valid.
48+
49+
test("should throw an error when count is a negative integer", () => {
50+
const str = "test";
51+
const count = -5;
52+
expect(() => repeatStr(str, count)).toThrow();
53+
});
54+

Sprint-3/4-stretch/find.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ 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+
// It always starts at 0, the first letter of the string. On each loop
24+
// iteration that doesn't find a match, index++ runs, increasing its value by 1
25+
2326
// b) What is the if statement used to check
27+
// If it evaluates to true, it stops the
28+
// entire function immediately and hands back that position number.
29+
2430
// c) Why is index++ being used?
31+
// It forces the loop to move to the next letter in the string for the next round.
32+
// Without it, the loop would look at index 0 over and over again and increasing
33+
// the index in every single turn makes sure that the loop with eventually
34+
// be equal to the length of the string and come to an end.
35+
2536
// d) What is the condition index < str.length used for?
37+
// This condition is a safety guardrail that defines the lifetime of the loop.
38+
// It basically tells the loop to keep looping as long as
39+
// we haven't run out of letters to look at
Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
1-
function passwordValidator(password) {
2-
return password.length < 5 ? false : true
3-
}
1+
function passwordValidator(password, passwordsHistory) {
2+
if (password.length < 5) {
3+
return false;
4+
}
5+
if (passwordsHistory && passwordsHistory.includes(password)) {
6+
return false;
7+
}
8+
let hasUppercase = false;
9+
let hasLowercase = false;
10+
let hasNumber = false;
11+
let hasSymbol = false;
12+
13+
const specialSymbols = "!#$%*&.";
14+
for (let i = 0; i < password.length; i++) {
15+
const char = password[i];
16+
17+
if (char >= "0" && char <= "9") {
18+
hasNumber = true;
19+
} else if (specialSymbols.includes(char)) {
420

21+
hasSymbol = true;
22+
} else if (char === char.toUpperCase() && char !== char.toLowerCase()) {
23+
hasUppercase = true;
24+
25+
} else if (char === char.toLowerCase() && char !== char.toUpperCase()) {
26+
hasLowercase = true;
27+
}
28+
}
29+
30+
if (hasUppercase && hasLowercase && hasNumber && hasSymbol) {
31+
return true;
32+
} else {
33+
return false;
34+
}
35+
}
36+
console.log(passwordValidator("Aodbe.f2", "Aodbe.f2"));
537

638
module.exports = passwordValidator;

0 commit comments

Comments
 (0)