猿问

运动与周期

大家好,我是初学者,我正在尝试学习Javascript,我应该使用for循环进行此简单练习,实际上是在给定一个包含电子邮件地址的数组的情况下,对用户通过提示输入的变量进行确认,插入的电子邮件包含在数组中,然后在视频上打印相对肯定的消息,我做了类似的事情,但是它不起作用,而且我不明白我在哪里错了...。


// 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.lenght; i ++) {


    if (mail_user == email) {

        document.writeln ('Your email address is correct')               

    }

 }


芜湖不芜
浏览 132回答 2
2回答

饮歌长啸

您使用for循环,但实际上并没有遍历数组。如果要比较输入的电子邮件地址,则必须从数组中检索特定的电子邮件地址,方法是调用mail[i]var mail = ['mail1', 'mail2', 'mail3'];var mail_user = prompt ('Enter your mail');for (var i = 0; i <mail.length; i ++) {&nbsp; &nbsp; if (mail_user == mail[i]) {&nbsp; &nbsp; &nbsp; &nbsp; console.log('Your email address is correct')&nbsp; &nbsp; }&nbsp;}

温温酱

// initialize an array with a mailing listvar mail = ['mail1', 'mail2', 'mail3'];var mail_user = prompt ('Enter your mail');for (var i = 0; i <mail.length; i ++) {&nbsp; &nbsp; if (mail_user == mail[i]) {&nbsp; &nbsp; &nbsp; &nbsp; document.writeln ('Your email address is correct')&nbsp; &nbsp; }&nbsp;}我更新了你的if声明或者你可以使用 foreachmail.forEach(function(email) {&nbsp; &nbsp; if (mail_user == email) {&nbsp; &nbsp; &nbsp; &nbsp; document.whiteln ('Your email address is correct')&nbsp; &nbsp; }&nbsp;}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答