Javascript 代码赢得';由于字符串长度问题,无法运行
这里有一个简单的Javascript程序来接受和检查密码。它应当:Javascript 代码赢得';由于字符串长度问题,无法运行,javascript,Javascript,这里有一个简单的Javascript程序来接受和检查密码。它应当: 要求您输入新密码 检查密码的强度,该密码根据长度6输出弱消息或强消息 让您重新输入此密码以进入“系统” 如果密码不正确,则给出简单提示或两个随机字母 除强/弱检查器外,所有功能都正常工作。它在获取passwordEntry的长度时遇到问题,因为它显然不作为实体存在 任何想法都将不胜感激 var pass; var main = function(){ strengthCheck((prompt("Please Choose a
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()
改变这个
var strengthCheck = new function(passwordEntry){
对此
var strengthCheck = function(passwordEntry){
当您使用new
时,您并没有使用它来创建新函数。您使用它作为构造函数调用函数,构造函数将返回一个对象。(在您的案例中为空对象。)
此外,代码中还有许多语法错误。使用代码验证程序和美化程序清理代码。这部分看起来非常错误:
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"));
}
有三个左括号,只有两个右括号。闻起来像是解析器错误。缺失)在if(passwordEntry.length>6{?你确定这是你实际使用的代码吗?第3行还有一个额外的
(
)。请投票所有答案和注释!谢谢,修复了。还有一些带有额外括号的错误,这些错误现在都已修复。