2 回答
TA贡献1951条经验 获得超3个赞
您使用for循环,但实际上并没有遍历数组。如果要比较输入的电子邮件地址,则必须从数组中检索特定的电子邮件地址,方法是调用mail[i]
var mail = ['mail1', 'mail2', 'mail3'];
var mail_user = prompt ('Enter your mail');
for (var i = 0; i <mail.length; i ++) {
if (mail_user == mail[i]) {
console.log('Your email address is correct')
}
}
TA贡献1752条经验 获得超4个赞
// initialize an array with a mailing list
var mail = ['mail1', 'mail2', 'mail3'];
var mail_user = prompt ('Enter your mail');
for (var i = 0; i <mail.length; i ++) {
if (mail_user == mail[i]) {
document.writeln ('Your email address is correct')
}
}
我更新了你的if声明
或者你可以使用 foreach
mail.forEach(function(email) {
if (mail_user == email) {
document.whiteln ('Your email address is correct')
}
}
添加回答
举报