Skip to content

Commit 7a76aec

Browse files
committed
password validator
1 parent ad57a02 commit 7a76aec

4 files changed

Lines changed: 48 additions & 10 deletions

File tree

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ let testName = "Jerry";
55
const greeting = "hello";
66

77
function sayHello(greeting, name) {
8-
const greetingStr = greeting + ", " + name + "!";
9-
return `${greeting}, ${name}!`;
10-
console.log(greetingStr);
118
}
129

1310
testName = "Aman";

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +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());
65
const petsStartingWithH = pets.filter((pet) => pet[0] === "h");
76

8-
function logPets(petsArr) {
9-
petsArr.forEach((pet) => console.log(pet));
10-
}
11-
127
function countAndCapitalisePets(petsArr) {
138
const petCount = {};
149

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

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
1-
function passwordValidator(password) {
2-
return password.length < 5 ? false : true
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)) {
20+
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+
}
335
}
436

537

0 commit comments

Comments
 (0)