带有 while 循环的 JavaScript 数组

在这段代码中,它应该找到“var dob”的年龄,然后遍历数组以找到学生的年级。


有人告诉我,我有正确的代码来查找此人的年龄。我也有合适的数组。我的问题似乎在我的 while 循环中。


我的 while 循环中的语法有错误。


    <script language="javascript" type="text/javascript">


        var dob = '20120810';

        var year = Number(dob.substr(0, 4));

        var month = Number(dob.substr(4, 2)) - 1;

        var day = Number(dob.substr(6, 2));

        var today = new Date();

        var age = today.getFullYear() - year;

        if (today.getMonth() < month || (today.getMonth() == month && today.getDate() < day)) {

      age--;

    }

    //document.write("You are " + (age + 1) + " Years old"+"<br>");


    var grade = [

        [6,'Grade 1'],

        [7,'Grade 2'],

        [8,'Grade 3'],

        [9,'Grade 4'],

        [10,'Grade 5'],

        [11,'Grade 6'],

        ];


    while (var age = 0; age < grade; age++){

          document.write(grade[age]);

    }



    </script>


有只小跳蛙
浏览 110回答 2
2回答

侃侃无极

您的一些错误是1。您正在将 (int)age 与 (array)grade 进行比较 最好的方法是age < grade.length2。grade数组的索引是从 0 到 5,即grade[0] == [6, 'Grade 1'],这意味着如果年龄大于 5,你不会有结果。另一种方法,如果你真的想打印出与年龄对应的等级,&nbsp; &nbsp; // age = 7;&nbsp; &nbsp; let yourGrade = grade.find(e => {&nbsp; &nbsp; &nbsp; &nbsp; return e[0] == age;&nbsp; &nbsp; });&nbsp; &nbsp; // console.log(yourGrade); // [7, 'Grade 2']&nbsp; &nbsp; // console.log(yourGrade ? yourGrade[1] : "You’re either too young or too old");&nbsp; &nbsp; // Grade 2上面的代码尝试查看您的(数组)等级并返回第一个项目等于计算年龄的值的第一次出现。有很多方法可以实现这一目标。编辑或者,如果您对while循环特别感兴趣,您可以执行以下操作:&nbsp; &nbsp; //age = 7;&nbsp; &nbsp; let yourGrade;&nbsp; &nbsp; let i = grade.length;&nbsp; &nbsp; while(i––) {&nbsp; &nbsp; &nbsp; &nbsp; if (grade[i][0] == age) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yourGrade = grade[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; console.log(yourGrade);&nbsp; &nbsp; //[7,'Grade 2']&nbsp; &nbsp; //try with age = 5 //undefined

紫衣仙女

如果您正在寻找年龄和年级,这可能是您的最佳解决方案。请记住,这里的等级是二维数组,您将等级的第一个索引元素与年龄进行比较。所以你需要首先从等级数组的第一个元素中找到最大值。var dob = '20120810';var year = Number(dob.substr(0, 4));var month = Number(dob.substr(4, 2)) - 1;var day = Number(dob.substr(6, 2));var today = new Date();var age = today.getFullYear() - year;if (today.getMonth() < month || (today.getMonth() == month && today.getDate() < day)) {&nbsp; &nbsp; age--;}console.log("You are " + (age + 1) + " Years old");var grade = [[6,'Grade 1'],[7,'Grade 2'],[8,'Grade 3'],[9,'Grade 4'],[10,'Grade 5'],[11,'Grade 6'],];maxAge=grade.reduce(function(max, grade) {&nbsp;&nbsp; &nbsp; return Math.max(max, grade[0]);&nbsp;}, -Infinity);for (i = 0; i < maxAge; i++){&nbsp; &nbsp; if(typeof grade[i] !== 'undefined' && grade[i][0]==age){&nbsp; &nbsp; &nbsp; &nbsp; console.log(grade[i][1]);&nbsp; &nbsp; }}另一种选择可能是:var dob = '20120810';var year = Number(dob.substr(0, 4));var month = Number(dob.substr(4, 2)) - 1;var day = Number(dob.substr(6, 2));var today = new Date();var age = today.getFullYear() - year;if (today.getMonth() < month || (today.getMonth() == month && today.getDate() < day)) {&nbsp; &nbsp; age--;}//console.log("You are " + (age + 1) + " Years old");var grade = [[6,'Grade 1'],[7,'Grade 2'],[8,'Grade 3'],[9,'Grade 4'],[10,'Grade 5'],[11,'Grade 6'],];let yourGrade = grade.find(e => {&nbsp; &nbsp; return e[0] == age;});console.log('Age: ' + yourGrade[0] + ' and Grade: ' + yourGrade[1]);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript