2022-09-22

How to run loop with two arrays?

This is the code I wrote but it runs 16 loops. I want to print an output into the console.log just like this:

John got F
Tyler got D
Rose got C
Lawrence got B

Comment the right code please.

function gradeNumbers(){
    var students = ["John", "Tyler", "Rose", "Lawrence"];
    var marks = ["35" , "44" , "55" , "66"];
    for (var s = 0; s < students.length; s++) {
        for (var x = 0; x < marks.length; x++) {
            if ((marks[x]) < 40) {
                console.log(students[s] + " got F");
            }
            else if (marks[x] < 50) {
                console.log(students[s] + " got D");
            }
            else if (marks[x] < 60 ) {
                console.log(students[s] + " got C");
            }
            else if (marks[x] < 70) {
                console.log(students[s] + " got B");
            }
        }
    }
    return gradeNumbers;
};


No comments:

Post a Comment