Skip to content

Commit c84ff52

Browse files
committed
sprint-3/ revise/ implement Done
1 parent 74a346c commit c84ff52

File tree

6 files changed

+136
-4
lines changed

6 files changed

+136
-4
lines changed

Sprint-3/revise/implement/count.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// implement a function countChar that counts the number of times a character occurs in a string
2-
2+
function countChar(str, char) {
3+
let count = 0
4+
for (let c of str) {
5+
if (c === char) {
6+
count++;
7+
}
8+
}
9+
return count;
10+
}
311
// Given a string str and a single character char to search for,
412
// When the countChar function is called with these inputs,
513
// Then it should:
@@ -15,3 +23,7 @@
1523
// And a character char that does not exist within the case-sensitive str,
1624
// When the function is called with these inputs,
1725
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
26+
test("Count the number of times a character occurs in a string", function () {
27+
expect(countChar('fndscn','n')).toEqual(2);
28+
expect(countChar("",'g')).toEqual(0);
29+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* Here are the rules for a valid number:
2+
3+
- Number must be 16 digits, all of them must be numbers.
4+
- You must have at least two different digits represented (all of the digits cannot be the same).
5+
- The final digit must be even.
6+
- The sum of all the digits must be greater than 16.*/
7+
8+
function creditcardValidator(str) {
9+
if (typeof str !== "string" || str.length !== 16) {
10+
return false;
11+
}
12+
let sum = 0;
13+
let obj = {};
14+
for (let i = 0; i < 16; i++) {
15+
if (typeof +str[i] !== "number" || isNaN(+str[i])) {
16+
return false;
17+
} else {
18+
sum = sum + +str[i];
19+
obj[+str[i]] = 1;
20+
}
21+
}
22+
return (
23+
sum > 16 && str[str.length - 1] % 2 == 0 && Object.keys(obj).length > 2
24+
);
25+
}
26+
27+
test("Check if card number valid", function () {
28+
expect(creditcardValidator("1234567812345678")).toEqual(true);
29+
expect(creditcardValidator("123456781234567g")).toEqual(false);
30+
expect(creditcardValidator("1000000000000000")).toEqual(false);
31+
});

Sprint-3/revise/implement/get-ordinal-number.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,24 @@
22

33
// continue testing and implementing getOrdinalNumber for additional cases
44
// Write your tests using Jest - remember to run your tests often for continual feedback
5+
function getOrdinalNumber(num) {
6+
if (typeof num !== 'number' && isNaN(num)) {
7+
return 'Enter valid input'
8+
}
9+
if (num === 1) {
10+
return '1st'
11+
} else if (num === 2) {
12+
return '2nd'
13+
} else if (num === 3) {
14+
return '3rd'
15+
} else {
16+
return `${num}th`
17+
}
18+
}
19+
20+
test("The ordinal number", function () {
21+
expect(getOrdinalNumber(1)).toEqual("1st");
22+
expect(getOrdinalNumber(2)).toEqual('2nd');
23+
expect(getOrdinalNumber(3)).toEqual("3rd");
24+
expect(getOrdinalNumber(13)).toEqual("13th");
25+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
// Given a positive integer num,
22
// When the isPrime function is called with num as input,
33
// Then it should check if the num is prime
4+
function isPrime(num) {
5+
for (let i = 2; i < Math.sqrt(num); i++){
6+
if (num % i === 0) {
7+
return false
8+
}
9+
}
10+
return true
11+
}
12+
13+
14+
test("The ordinal number", function () {
15+
expect(isPrime(1)).toEqual(true);
16+
expect(isPrime(22)).toEqual(false);
17+
expect(isPrime(33)).toEqual(false);
18+
expect(isPrime(13)).toEqual(true);
19+
});
Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/*
22
Password Validation
33
4-
Write a program that should check if a password is valid
5-
and returns a boolean
6-
4+
Write a program that should check if a password is valid and returns a boolean
75
To be valid, a password must:
86
- Have at least 5 characters.
97
- Have at least one English uppercase letter (A-Z)
@@ -14,3 +12,38 @@ To be valid, a password must:
1412
1513
You must breakdown this problem in order to solve it. Find one test case first and get that working
1614
*/
15+
function passwordValidator(str, passwords) {
16+
if (
17+
typeof str != "string" ||
18+
str.length < 5 ||
19+
passwords.indexOf(str) != -1
20+
) {
21+
return false;
22+
}
23+
let upperCaseLetter = false;
24+
let lowerCaseLetter = false;
25+
let oneNumber = false;
26+
let nonAlphanumericSymbol = false;
27+
const symbols = ['!', '#', '$', '%', '.', '*', '&'];
28+
29+
for (let i = 0; i < str.length; i++) {
30+
const char = str[i];
31+
32+
if (char >= "A" && char <= "Z") {
33+
upperCaseLetter = true;
34+
} else if (char >= "a" && char <= "z") {
35+
lowerCaseLetter = true;
36+
} else if (char >= "0" && char <= "9") {
37+
oneNumber = true;
38+
} else if (symbols.includes(char)) {
39+
nonAlphanumericSymbol = true;
40+
}
41+
}
42+
return upperCaseLetter && lowerCaseLetter && oneNumber && nonAlphanumericSymbol
43+
44+
}
45+
46+
test('Check for valid password', function () {
47+
expect(passwordValidator('Asd123#',[])),toEqual(true);
48+
expect(passwordValidator("Asd123#", ["Asd123#"])), toEqual(false);
49+
})

Sprint-3/revise/implement/repeat.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
// Implement a function repeat
22

3+
function repeat(str, count) {
4+
if (count < 0 || typeof count !== 'number') {
5+
return 'Error'
6+
} else if (count === 0 ) {
7+
return ''
8+
} else {
9+
let res = ''
10+
for (let i = 0; i < count; i++){
11+
res += str
12+
}
13+
return res
14+
}
15+
}
16+
17+
test("Repeat the str count times", function () {
18+
expect(repeat('hello', 0)).toEqual('');
19+
expect(repeat('hello', 2)).toEqual('hellohello');
20+
expect(repeat('hello', -5)).toEqual('Error');
21+
});
322
// Given a target string str and a positive integer count,
423
// When the repeat function is called with these inputs,
524
// Then it should:

0 commit comments

Comments
 (0)