-2

这里有一个简单的 Javascript 程序来接受和检查密码。它应该:

  1. 要求您输入新密码

  2. 检查密码的强度,根据 <6 或 >6 的长度输出弱或强消息。

  3. 让你重新输入这个密码进入“系统”

  4. 如果密码不正确,给您简单的提示或 2 个随机字母。

除了强/弱检查器外,一切正常。获取 passwordEntry 的长度时有问题,因为它显然不作为实体存在。

任何想法将不胜感激

var pass;
var main = function(){
strengthCheck((prompt("Please Choose a New Password to Begin"));
}

var strengthCheck = new function(passwordEntry){
score = 0;
// adds to the score variable depending on the length of the password
if(passwordEntry.length > 6{
score=(score+1);
}
//reads messages back stating how strong password is based on length
if(score=0){
console.log("Your Password is Weak");
}
else if(score=1){
console.log("Your Password is Strong");
}
var passContinue = prompt("Do you want to continue with this password? Yes or no?")
if(passContinue === "no" ||  passContinue === "No"{
main();
}
else{
pass = passwordEntry;
console.log("Your new password has been changed to " + pass);
passwordChecker(prompt("Thank You. Please Enter Your Password Below"));
}
}

var passwordChecker = function (attempt){
if(attempt == pass){
    console.log("Correct password. The system has logged you on");
}
    else{
    //if the password is wrong, runs the incorrectpassword() function
        console.log("Incorrect Password");
        IncorrectPass();
        }
    }
}

var IncorrectPass = function (){
var clueanswer = prompt("Do You Want A Clue");
if(clueanswer === "Yes" ||clueanswer === "yes"){    
console.log("I will give you two random letters");
// takes two random locations from the string array and reads them back
var randarray1 = Math.floor((Math.random()*7)+1);
var randarray2 = Math.floor((Math.random()*7)+1);
var randletter1 = pass[randarray1];
var randletter2 = pass[randarray2];
console.log(randletter1+" "+randletter2);
passwordChecker("Please try entering your password again");  
}
    else{
        console.log("GoodBye");
    }
}

main()
4

2 回答 2

3

这部分看起来很不对劲:

if(score=0){
  console.log("Your Password is Weak");
}
else if(score=1){
  console.log("Your Password is Strong");
}

您应该使用==or===而不是=which 用于分配而不是比较。

这也没有意义:

var main = function(){
  strengthCheck((prompt("Please Choose a New Password to Begin"));
}

有三个左括号,只有两个右括号。闻起来像解析器错误。

于 2012-06-25T14:03:53.750 回答
1

改变这个...

var strengthCheck = new function(passwordEntry){

对这个……

var strengthCheck = function(passwordEntry){

当您使用 时new,您并没有使用它来创建新功能。您正在使用它作为构造函数调用该函数,该构造函数将返回一个对象。(在您的情况下是一个空对象。)

此外,您的代码中有许多语法错误。使用 http://jshint.com 之类的代码验证器以及http://jsbeautifier.org之类的美化器来清理您的代码。

于 2012-06-25T14:01:19.127 回答