Skip to content

Homework on Password validation #18

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
// Do work!
// Do work!
function validatePassword(password){
if (password.length >= 8)
return true
}
module.exports = validatePassword
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"lint": "eslint --format codeframe .",
"lint:fix": "eslint --fix --format codeframe .",
"test": "mocha -w"
"test": "mocha "
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expect } = require('chai')
const validatePassword = require('../index')
const validatePassword = require('../validatePassword2')

describe('validatePassword', () => {
it('returns true when the password meets all requirements', () => {
Expand Down
29 changes: 29 additions & 0 deletions validatePassword2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function isLowerCase(pwd, index) {
return pwd.charCodeAt(index) >= 97 && pwd.charCodeAt(index) <= 122
}
function isUpperCase(pwd, index) {
return pwd.charCodeAt(index) >= 65 && pwd.charCodeAt(index) <= 90
}
function isNumeric(pwd, index) {
return !isNaN(pwd[index])
}
function validatePassword(pwd) {
if (pwd.length < 8) return false
let upper = 0
let lower = 0
let numeric = 0
let special = 0
for (let i = 0; i < pwd.length; i++) {
if (isLowerCase(pwd, i)) {
lower++
} else if (isUpperCase(pwd, i)) {
upper++
} else if (isNumeric(pwd, i)) {
numeric++
} else {
special++
}
}
return lower > 0 && upper > 0 && numeric > 0 && special > 0
}
module.exports = validatePassword