Posted on

# My Coding Challenge Interview! Sheesh!

## I am very happy I made it!

``````/*
You are working on an authentication system and there is a set of rules the users have to follow when picking a new password:

1. It has to be at least 16 characters long.
2. The password cannot contain the word "password". This rule is not case-sensitive.
3. The same character cannot be used more than 4 times. This rule is case-sensitive, "a" and "A" are different characters.
4. The password has to contain at least one uppercase and one lowercase letter.
5. The password has to contain at least one of the following special characters "*","#","@".

Write a function that takes in a password and returns a collection of any rule numbers that are not met.

All test cases:

Complexity variables:

N = length of the password

time O(n)
space O()

*/

"use strict";

const arrRules = []

// 1. It has to be at least 16 characters long.

// 2. The password cannot contain the word "password". This rule is not case-sensitive.

// 3. The same character cannot be used more than 4 times. This rule is case-sensitive, "a" and "A" are different characters.
const charCounter = {}

for (const char of password) {
if (charCounter[char]) {
charCounter[char] ++
} else {
charCounter[char] = 1
}

if (charCounter[char] > 4) {
arrRules.push(3)
break
}
}

// 4. The password has to contain at least one uppercase and one lowercase letter.
let hasUpperCase = false
let hasLowerCase = false

for (const char of password) {
const decCode = char.charCodeAt(0)

if (decCode > 64 && decCode < 91) {
hasUpperCase = true
} else if (decCode > 96 && decCode < 123) {
hasLowerCase = true
}

if (hasUpperCase && hasLowerCase) break
}

if (!(hasUpperCase && hasLowerCase)) {
arrRules.push(4)
}

// 5. The password has to contain at least one of the following special characters "*","#","@".

let hasSpecialChar = false

for (const char of password) {

if (["*","#","@"].includes(char)) {
hasSpecialChar = true
break
}

}

if (!hasSpecialChar) arrRules.push(5)

return arrRules

}