function passwordGrade(pwd) {
var score = 0;
var regexArr = ['[0-9]', '[a-z]', '[A-Z]', '[\W_]'];
var repeatCount = 0;
var prevChar = '';
//check length
var len = pwd.length;
score += len > 18 ? 18 : len;
//check type
for (var i = 0, num = regexArr.length; i < num; i++) { if (eval('/' + regexArr[i] + '/').test(pwd)) score += 4; }
//bonus point
for (var i = 0, num = regexArr.length; i < num; i++) {
if (pwd.match(eval('/' + regexArr[i] + '/g')) && pwd.match(eval('/' + regexArr[i] + '/g')).length >= 2) score += 2;
if (pwd.match(eval('/' + regexArr[i] + '/g')) && pwd.match(eval('/' + regexArr[i] + '/g')).length >= 5) score += 2;
}
//deduction
for (var i = 0, num = pwd.length; i < num; i++) {
if (pwd.charAt(i) == prevChar) repeatCount++;
else prevChar = pwd.charAt(i);
}
score -= repeatCount * 1;
return score;
}