gpt4 book ai didi

javascript - 包含大量特殊字符数组的 JS 函数不起作用

转载 作者:行者123 更新时间:2023-12-02 23:35:30 24 4
gpt4 key购买 nike

我是编码新手,我开始在Codecademy中学习JS...本类(class)中有一个项目名为“密码验证器”...根据这个项目,我将这个函数命名为hasSpecialCharacter 检查密码是否至少有一个特殊字符,但是当我完成此代码并尝试使用示例密码运行它时,它什么也不做,控制台停止工作...

我尝试从 VS Code 和 Powershell 作为 JS 控制台在我的 PC 中运行该代码,但当我运行此代码时,控制台什么也不显示,但任务仍在运行...我什至尝试从数组中删除一些字符,但它仍然没用...

function hasUpperCase(input) {
for (var i = 0; i < input.length; i++) {
if (input[i] === input[i].toUpperCase()) {
return true;
}
}
}

function hasLowerCase(input) {
for (var j = 0; j < input.length; j++) {
if (input[j] === input[j].toLowerCase()) {
return true;
}
}
}

function isLongEnough(input) {
if (input.length >= 8) {
return true;
}
}

function hasSpecialCharacter(input) {
var specialCharacters = ['`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '{', '}', ';', ':', '\'', '"', '\\', '|', ',', '<', '.', '>', '/', '?'];

for (var k = 0; k < input.length; k++) {
for (var l = 0; specialCharacters.length; l++) {
if (input[k] === specialCharacters[l]) {
return true;
}
}
}
}

function isPasswordValid(input) {
if (hasUpperCase(input) && hasLowerCase(input) && isLongEnough(input)) {// && hasSpecialCharacter(input)) {
console.log("The password is valid");
} if (!hasUpperCase(input)) {
console.log("The password needs atleast 1 capital letter");
} if (!hasLowerCase(input)) {
console.log("The password needs atleast one small letter");
} if (!isLongEnough(input)) {
console.log("The password must be atleast 8 characters long");
} if (!hasSpecialCharacter(input)) {
console.log("The password needs atleast 1 special character");
}
}

isPasswordValid('red');

我认为控制台中的输出会是这样的:

PS C:\Users\Fuad Hasan\Desktop\Codes> node .\passwordValidator2.js
The password needs atleast 1 capital letter
The password must be atleast 8 characters long
The password needs atleast 1 special character

但是控制台光标一直闪烁,就像在此之后正在加载一样:

PS C:\Users\Fuad Hasan\Desktop\Codes> node .\passwordValidator2.js
The password needs atleast 1 capital letter
The password must be atleast 8 characters long
...

最佳答案

您的代码中有一个错误。

编辑此行

for (var l = 0; specialCharacters.length; l++) {

for (var l = 0; l < specialCharacters.length; l++) {

执行/运行下面的代码片段:

function hasUpperCase(input) {
for (var i = 0; i < input.length; i++) {
if (input[i] === input[i].toUpperCase()) {
return true;
}
}
}

function hasLowerCase(input) {
for (var j = 0; j < input.length; j++) {
if (input[j] === input[j].toLowerCase()) {
return true;
}
}
}

function isLongEnough(input) {
if (input.length >= 8) {
return true;
}
}

function hasSpecialCharacter(input) {
var specialCharacters = ['`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '+', '=', '{', '}', ';', ':', '\'', '"', '\\', '|', ',', '<', '.', '>', '/', '?'];

for (var k = 0; k < input.length; k++) {
for (var l = 0; l < specialCharacters.length; l++) {
if (input[k] === specialCharacters[l]) {
return true;
}
}
}
}

function isPasswordValid(input) {
if (hasUpperCase(input) && hasLowerCase(input) && isLongEnough(input)) {// && hasSpecialCharacter(input)) {
console.log("The password is valid");
} if (!hasUpperCase(input)) {
console.log("The password needs atleast 1 capital letter");
} if (!hasLowerCase(input)) {
console.log("The password needs atleast one small letter");
} if (!isLongEnough(input)) {
console.log("The password must be atleast 8 characters long");
} if (!hasSpecialCharacter(input)) {
console.log("The password needs atleast 1 special character");
}
}

console.log('Validating password: "red"');
isPasswordValid('red');
console.log('Validating password: "red@Redcolor"');
isPasswordValid('red@Redcolor');

关于javascript - 包含大量特殊字符数组的 JS 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56306179/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com