Reputation: 11830
I was doing this problem on hacker earth, (can ignore the question)
Basically in the below code snippet
function minimumNumber(n, password) {
const mapStrength = {
numberProp: false,
lowerCase: false,
upperCase: false,
specialChar: false
}
// Number of characters less than 6, add those many characters to reach 6 -> The buffer characters
let charRequeiredToMakePasswordStrong = 0
let bufferLength = (6-n) > 0 ? 6-n : 0
console.log(`This is buffer:`, bufferLength)
// Split password and itt
password.split('').forEach(character => {
// Checking if character is number
if (Number.isInteger(character) && !mapStrength.numberProp) mapStrength.numberProp = true
// Checking if character is
if (character.match(/[^a-zA-Z]/) && (character === character.toLowerCase()) && !mapStrength.lowerCase) mapStrength.lowerCase = true
if (character.match(/[^a-zA-Z]/) && (character === character.toUpperCase()) && !mapStrength.upperCase) mapStrength.upperCase = true
if (character.toLowerCase() === character.toUpperCase()) mapStrength.specialChar = true
})
console.log(mapStrength)
// Calculating Number of false
Object.values(mapStrength).forEach(charge => {
if (!charge) charRequeiredToMakePasswordStrong += 1
})
console.log(charRequeiredToMakePasswordStrong, bufferLength)
if (bufferLength > charRequeiredToMakePasswordStrong) return bufferLength
if (charRequeiredToMakePasswordStrong > bufferLength) return charRequeiredToMakePasswordStrong
return bufferLength
}
minimumNumber(3, 'zss');
why is my mapStrength.lowerCase coming out to be false? In this line I though I am setting to true?
if (character.match(/[^a-zA-Z]/) && (character === character.toLowerCase()) && !mapStrength.lowerCase) mapStrength.lowerCase = true
Upvotes: 0
Views: 53
Reputation: 370789
With your test:
if (character.match(/[^a-zA-Z]/) && (character === character.toLowerCase()) && !mapStrength.lowerCase) {
mapStrength.lowerCase = true
}
The first condition only applies if character.match(/[^a-zA-Z]/)
- in other words, if character
is not alphabetical. The [^
matches any character that is not one of the following characters in the character set. Use /[a-z]/i
instead (which is the same as [a-zA-Z]
, only less repetitive):
function minimumNumber(n, password) {
const mapStrength = {
numberProp: false,
lowerCase: false,
upperCase: false,
specialChar: false
}
// Number of characters less than 6, add those many characters to reach 6 -> The buffer characters
let charRequeiredToMakePasswordStrong = 0
let bufferLength = (6-n) > 0 ? 6-n : 0
console.log(`This is buffer:`, bufferLength)
// Split password and itt
password.split('').forEach(character => {
// Checking if character is number
if (Number.isInteger(character) && !mapStrength.numberProp) mapStrength.numberProp = true
// Checking if character is
if (character.match(/[a-z]/i) && (character === character.toLowerCase()) && !mapStrength.lowerCase) mapStrength.lowerCase = true
if (character.match(/[a-z]/i) && (character === character.toUpperCase()) && !mapStrength.upperCase) mapStrength.upperCase = true
if (character.toLowerCase() === character.toUpperCase()) mapStrength.specialChar = true
})
console.log(mapStrength)
// Calculating Number of false
Object.values(mapStrength).forEach(charge => {
if (!charge) charRequeiredToMakePasswordStrong += 1
})
console.log(charRequeiredToMakePasswordStrong, bufferLength)
if (bufferLength > charRequeiredToMakePasswordStrong) return bufferLength
if (charRequeiredToMakePasswordStrong > bufferLength) return charRequeiredToMakePasswordStrong
return bufferLength
}
minimumNumber(3, 'zss');
minimumNumber(3, 'zsS');
You could make the code more readable by just using a single condition that assigns to mapStrength.lowerCase
regardless:
if (character.match(/[a-z]/)) {
mapStrength.lowerCase = true;
}
if (character.match(/[A-Z]/)) {
mapStrength.upperCase = true;
}
function minimumNumber(n, password) {
const mapStrength = {
numberProp: false,
lowerCase: false,
upperCase: false,
specialChar: false
}
// Number of characters less than 6, add those many characters to reach 6 -> The buffer characters
let charRequeiredToMakePasswordStrong = 0
let bufferLength = (6-n) > 0 ? 6-n : 0
console.log(`This is buffer:`, bufferLength)
// Split password and itt
password.split('').forEach(character => {
// Checking if character is number
if (Number.isInteger(character) && !mapStrength.numberProp) mapStrength.numberProp = true
// Checking if character is
if (character.match(/[a-z]/)) {
mapStrength.lowerCase = true;
}
if (character.match(/[A-Z]/)) {
mapStrength.upperCase = true;
}
if (character.toLowerCase() === character.toUpperCase()) mapStrength.specialChar = true
})
console.log(mapStrength)
// Calculating Number of false
Object.values(mapStrength).forEach(charge => {
if (!charge) charRequeiredToMakePasswordStrong += 1
})
console.log(charRequeiredToMakePasswordStrong, bufferLength)
if (bufferLength > charRequeiredToMakePasswordStrong) return bufferLength
if (charRequeiredToMakePasswordStrong > bufferLength) return charRequeiredToMakePasswordStrong
return bufferLength
}
minimumNumber(3, 'zss');
minimumNumber(3, 'zsS');
Upvotes: 1