Studio-9 looping over multiple arrays to form 4 new arrays!

Hi great community,

I would appreciate your help; I have spend many hours trying to solve this problem.

I am trying to loop over 4 different arrays and form 4 new arrays.
Here is my code:


let students = [“Linda”, “Steve”, “John”, “Tony”]
let ages = [20,30,40,50];
let cities = [“NYC”, “VA”,“CA”,“MI”]

let hoppies = [“swimming”, “Tennis”, “Soccer”, “Frisbee”]
let info_1 = [];
let info_2 = [];
let info_3 = [];
let info_4 = [];
for(let i =0; i < students .length; i++){
info_1.push(students[i], ages[i], cities[i], hoppies[i])
info_2.push(students[i], ages[i], cities[i], hoppies[i])
info_3.push(students[i], ages[i], cities[i], hoppies[i])
info_4.push(students[i], ages[i], cities[i], hoppies[i])
};

console.log(info_1);
console.log(info_2);
console.log(info_3);
console.log(info_4);


That is what I would like to accomplish from my code, but it is not giving me this output!!
info_1 = [“Linda”, 20, “NYC”]
info_2 =[“Steve”, 30, “VA”];
info_3 =[“John”, 40, “CA”];
info_4 = [“Tony”, 50, “MI”];

It took me a WHILE but I figured it out!
When it came to this studio I didn’t go about it this way so I had to entirely rework the logic.

The answer is below + in picture!

let students = [“linda”, “steve”, “john”, “tony”];
let ages = [20,30,40,50];
let cities = [“nyc”, “va”,“ca”,“mi”];
let hobbies = [“swimming”, “tennis”,“soccer”,“frisbee”];
let info_1 = [];
let info_2 = [];
let info_3 = [];
let info_4 = [];
let container = [];
container.push(students,ages,cities, hobbies);
let helper = [];

for(let i=0; i<students.length; i++){
for(let j=0; j<students.length; j++){
helper.push(container[j][i]);
};
info_1.push(helper.splice(0, students.length));
};

info_4 = info_1.pop();
info_3 = info_1.pop();
info_2 = info_1.pop();
info_1 = info_1.pop();

console.log(info_1);
console.log(info_2);
console.log(info_3);
console.log(info_4);

Thank you so much, that is a brilliant approach, I spent days trying to find a way with no success.

1 Like