由于字符串长度问题,代码将无法运行

时间:2012-06-25 13:57:04

标签: javascript

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

  1. 要求您输入新密码

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

  3. 让您重新输入此密码以进入“系统”

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

  5. 除强/弱检查员外,一切正常。获取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()
    

2 个答案:

答案 0 :(得分:3)

这部分看起来非常错误:

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

您应该使用=====代替=,而不是用于分配。

这也没有意义:

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

有三个开括号,只有两个结束括号。闻起来像解析器错误。

答案 1 :(得分:1)

改变这个......

var strengthCheck = new function(passwordEntry){

到此......

var strengthCheck = function(passwordEntry){

使用new时,您不会使用它来创建新功能。您正在使用它将该函数作为构造函数调用,它将返回一个对象。 (您的案例中的空对象。)

此外,您的代码中存在许多语法错误。使用代码验证程序(如http://jshint.com)以及美化程序http://jsbeautifier.org来清理代码。