0

In this code it is supposed to find the age of the "var dob" then loop through the array to find the grade the student would be in.

I have been told that I have the proper code to find the person's age. I also have the proper array. My problem seems to be in my while loop.

I have an error in my syntax in my while loop.

    <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>
5
  • 1
    Change from while to for, that looks like that. Commented Mar 27, 2020 at 21:03
  • You have lists within lists here. If you intend to access 6,7,8,9 etc.. then you would need to do something like grade[i][0] - lists are zero based... And use i in your loop. w3schools.com/js/js_loop_for.asp Try this alert(grade[0][0]); then alert(grade[1][0]); Commented Mar 27, 2020 at 21:20
  • 1
    You also can't compare an integer to an array/list (age < grade) grade.length may have been what you were after instead of grade on its own. Commented Mar 27, 2020 at 21:24
  • You also have age in the loop and and in the calculation. If grade[i] == age would be what you probably need to then lookup grade[i][1] - to give Grade 1 etc Commented Mar 27, 2020 at 21:31
  • I suggest taking a look at the while-loop and for-loop documentation. You'll notice that a while-loop can't have the syntax you've used in you question. Commented Mar 27, 2020 at 21:49

2 Answers 2

2

Some of your errors are
1. You are comparing (int)age with an (array)grade The best way would be age < grade.length
2. The index of grade array is from 0 to 5, that is grade[0] == [6, 'Grade 1'], which means if the age is greater than 5, you won’t have a result.

An alternative way, if you really want to print out the grade that corresponds to the age is,

    // age = 7;
    let yourGrade = grade.find(e => {
        return e[0] == age;
    });
    // console.log(yourGrade); // [7, 'Grade 2']
    // console.log(yourGrade ? yourGrade[1] : "You’re either too young or too old");
    // Grade 2

The code above tries to look through your (array)grade and returns the first occurrence of the value whose first item equals the calculated age.

There are many ways to achieve this.
EDIT
Alternatively, if you’re particular about the while loop, you can do the following:

    //age = 7;
    let yourGrade;
    let i = grade.length;
    while(i––) {
        if (grade[i][0] == age) {
            yourGrade = grade[i];
            break;
        }
    }
    console.log(yourGrade);
    //[7,'Grade 2']
    //try with age = 5 //undefined
Sign up to request clarification or add additional context in comments.

6 Comments

Only thing I'd change is this... console.log((yourGrade == null) ? "No grade for age" : yourGrade[1]);
That’s true. To actually check if there’s a suitable grade for the calculated age and to log the grade itself, and not the array that contains the grade. I’ll update the answer.
You while-loop contains invalid syntax. The while-clause should not contain ; and should contain a single expression.
@JGFMK (yourGrade == null) seems like an ant-pattern. Why not use console.log(yourGrade ? yourGrade[1] : "No grade for age");?
@JGFMK I agree with that. It should be stated the other way round.
|
0

This is probably the best solution for you if you are looking for age and grade.

Remember the grade here is two dimentional array and you are comparing the first index element of grade with age. So you need to first find the max from first elements of grade array.

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--;
}
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) { 
    return Math.max(max, grade[0]); 
}, -Infinity);

for (i = 0; i < maxAge; i++){
    if(typeof grade[i] !== 'undefined' && grade[i][0]==age){
        console.log(grade[i][1]);
    }
}

Another alternative could be :

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--;
}
//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 => {
    return e[0] == age;
});
console.log('Age: ' + yourGrade[0] + ' and Grade: ' + yourGrade[1]);

3 Comments

In your first code example, you set maxGrade to the maximum instance of grade[0] which is actually the age but not the grade. You might want to correct that.
that is absolutely correct, grade[0] is the max age, thats how the for loop is going to end, during this loop I am just searching mached age which is previously determined and retuning the grade (grade[i][0]) if matched.
grade[0] is the max age but you attached it to a variable named maxGrade, ain’t that confusing for the readers?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.