Created
October 7, 2022 07:14
-
-
Save Nasah-Kuma/d3030a926af66f43c426272786ad7cbd to your computer and use it in GitHub Desktop.
Solution to HackerRank's Strong Password Challenge: https://www.hackerrank.com/challenges/strong-password/problem?isFullScreen=true
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Complete the 'minimumNumber' function below. | |
* | |
* The function is expected to return an INTEGER. | |
* The function accepts following parameters: | |
* 1. INTEGER n | |
* 2. STRING password | |
*/ | |
function minimumNumber(n, password) { | |
// Return the minimum number of characters to make the password strong | |
const numbers = "0123456789"; | |
const lower_case = "abcdefghijklmnopqrstuvwxyz"; | |
const upper_case = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
const special_characters = "!@#$%^&*()-+"; | |
let numIndicator= false; | |
let lowerCaseIndicator = false; | |
let upperCaseIndicator = false; | |
let regexIndicator = false; | |
let count = 0; | |
for (let i=0; i < n; i++) { | |
if(!numIndicator && findChar(numbers, password[i])) { | |
count ++; | |
numIndicator = true; | |
} | |
if(!lowerCaseIndicator && findChar(lower_case, password[i])) { | |
count++; | |
lowerCaseIndicator = true; | |
} | |
if(!upperCaseIndicator && findChar(upper_case, password[i])) { | |
count++; | |
upperCaseIndicator = true; | |
} | |
if(!regexIndicator && findChar(special_characters, password[i])) { | |
count++; | |
regexIndicator = true; | |
} | |
} | |
count = 4 - count; | |
if(n + count < 6) { | |
count = 6 - n; | |
} | |
function findChar(arr, c) { | |
const l = arr.length; | |
for(let i = 0; i < l; i++) { | |
if(c == arr[i]) return true; | |
} | |
return false; | |
} | |
return count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment