How to run loop with two arrays?

Viewed 50

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;
};

4 Answers

By taking real world scenario both the array will have same number of elements in them, therefore, you can do this instead

function gradeNumbers(){
var students = ["John", "Tyler", "Rose", "Lawrence"];
var marks = ["35" , "44" , "55" , "66"];

for (var s = 0; s <= students.length; s++) 
    { 
            if ((marks[s])<40) {
                console.log(students[s] + " got F ");
            }
                else if ((marks[s])<50){
                    console.log(students[s] + " got D" );
                }
                    else if ((marks[s])<60){
                        console.log(students[s] + " got c" );
                    }
                    else if ((marks[s])<70) {
                        console.log(students[s] +"'s got : B");
                        }

        
    }
return gradeNumbers;
};

you don't have to run 2 for a loop since the student array and mark array has the same length you can run this with on for loop. after checking the condition if it is correct you need to skip the for loop so it won't iterate with every condition here you can run this code

function gradeNumbers() {
  var students = ["John", "Tyler", "Rose", "Lawrence"];
  var marks = ["35", "44", "55", "66"];

  for (var s = 0; s < students.length; s++) {
    if (marks[s] < 40) {
      console.log(students[s] + "'s got : F");
      continue;
    } else if (marks[s] < 50) {
      console.log(students[s] + "'s got D");
      continue;
    } else if (marks[s] < 60) {
      console.log(students[s] + "'s got C ");
      continue;
    } else if (marks[s] < 70) {
      console.log(students[s] + "'s got : B");
      continue;
    }
  }

  return gradeNumbers;
}

TLDR - Solution: Consider changing it to a single loop with a switch statement


Explanation:

What's happening to your code is in the second for loop. I'll try to talk through your code here:

Your first for-loop iterates through all of the students Your second for-loop iterates through all of the grades - this is where your issue is.

So, the first loop is hit. var s = 0, which means we're on John.

Now we go to your second loop. var x = 0, which means we're on 35.

We reach the if statement. 35 is less than 40. The condition is met, so the console logs John got an F.

Now we return to the start of the second loop because x is not larger that the marks array length. x is incremented, so now we're on marks[1], which is 44.

We reach your if statement. 44 is larger than 40, so we skip it. 44 is less than 50, so the console logs John got a D

This is where your issue is - the second for loop keeps going through the marks array 4 times using the student because that's what you have set up.

Dear @brendon: it is your outer for loop that makes it run 16X, this is because the inner array runs 4X, the outer array also runs 4X , now 4X4 = 16.

Try remove the outer array and recode the stuff and things will go well as imagined

Related