Reputation: 1
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>
Upvotes: 0
Views: 1339
Reputation: 73
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]);
Upvotes: 0
Reputation: 315
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
Upvotes: 2