9
var stu_names = ['Jhon','Alice','Mik'];
var stu_score = [300,200,400];

for (var i = 0; i < stu_names.length; i++) {
   for(var j = 0; j < stu_score.length; j++) {
       console.log(`Score of ${stu_names[i]} is ${stu_scrore[j]}`);
    }
}

I want to get the result like

'Score of Jhon is 300' 'Score of Alice is 200' 'Score of Mike is 400'

But instead of it, I m getting this result

Score of Jhon is 300
Score of Jhon is 200
Score of Jhon is 400
Score of Alice is 300
Score of Alice is 200
Score of Alice is 400
Score of Mik is 300
Score of Mik is 200
Score of Mik is 400

sibabrat swain
  • 1,277
  • 8
  • 20

8 Answers8

6

You need to take the same index for getting the same values of different arrays.

var stu_names = ['Jhon', 'Alice', 'Mik'];
var stu_score = [300, 200, 400];

for (var i = 0; i < stu_names.length; i++) {
    console.log(`Score of ${stu_names[i]} is ${stu_score[i]}`);
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
5

You can use one for loop while making sure that both arrays have the same length:

var stu_names = ['Jhon','Alice','Mik'];
var stu_score = [300,200,400];
if(stu_names.length == stu_score.length){
     for(let i = 0; i < stu_names.length; i++)
          console.log(`Score of ${stu_names[i]} is ${stu_score[i]}`);
}
Majed Badawi
  • 27,616
  • 4
  • 25
  • 48
2

Just another way using map:

var stu_names = ['Jhon', 'Alice', 'Mik'];
var stu_score = [300, 200, 400];

let result = stu_names.map((s, i)=> (`${s} ${stu_score[i]}`));
console.log(result)
StepUp
  • 36,391
  • 15
  • 88
  • 148
1

const stu_names = ['Jhon', 'Alice', 'Mik'];
const stu_scores = [300, 200, 400];

function zip(arr1, arr2) {
  return arr1.map((pr, index) => [pr, arr2[index]]);
}

for (let [stu_name, stu_score] of zip(stu_names, stu_scores)) {
  console.log(`Score of ${stu_name} is ${stu_score}`);
}
Józef Podlecki
  • 10,453
  • 5
  • 24
  • 50
1

You iterate over 2 for loops. But it is basically just 1 for loop which you need.

shy1992
  • 104
  • 5
1

You can use only one loop.

But maybe it's better to check before if two array have the same length.

var stu_names = ['Jhon','Alice','Mik'];
var stu_score = [300,200,400];
if(stu_names.length !== stu_score.length){
 throw new Error("Name array shold have the same length tha score array")
}
for (var i = 0; i< stu_names.length ; i++){
  console.log(`Score of ${stu_names[i]} is ${stu_score[i]}`);
}
ZecKa
  • 2,552
  • 2
  • 21
  • 39
1

You are nesting the list,

Rather use one loop as long as the length of both the arrays is the same.

if(stu_names.length !== stu_scrore.length) return;

for (var i = 0; i< stu_names.length ; i++){
    console.log(`Score of ${stu_names[i]} is ${stu_scrore[i]}`);
}
Raj Yadav
  • 9,677
  • 6
  • 35
  • 30
1

if you use nested loops like that, that means all the objects inside the first array will interact with all the objects in the second array. you can filter the undesired ones, or you can convert it to a single loop

var stu_names = ['Jhon','Alice','Mik'];
var stu_score = [300,200,400];

for (var i = 0; i< stu_names.length ; i++)
 console.log(`Score of ${stu_names[i]} is ${stu_scrore[i]}`);
vlad katz
  • 534
  • 3
  • 9