var number = [1,2,3,4];
var assignment= ["first", "second", "third", "fourth"];
I need to combine these two arrays so that it console logs this information like so:
1 first
2 second
3 third
4 fourth
Thank you.
var number = [1,2,3,4];
var assignment= ["first", "second", "third", "fourth"];
I need to combine these two arrays so that it console logs this information like so:
1 first
2 second
3 third
4 fourth
Thank you.
You can combine them like
var number = [1,2,3,4]; var assignment= ["first", "second", "third", "fourth"];
// alert (number[i] + " = " + assignment[number[i]-1]);
// change "i" as desired
alert (number[0] + " = " + assignment[number[0]-1]);
Now you can loop and combine them like this,
var number = [1,2,3,4]; var assignment= ["first", "second", "third", "fourth"];
for(var i=0;i<4;i++)
{
alert (number[i] + " = " + assignment[number[i]-1]);
}
I you want a string, you can do something like this,
var number = [1,2,3,4]; var assignment= ["first", "second", "third", "fourth"];
var txt = "";
for(var i=0;i<4;i++)
{
txt = txt + (number[i] + " = " + assignment[number[i]-1] + " ");
}
alert(txt);
If the arrays has the same leght, it should do the work
var number = [1,2,3,4];
var assignment= ["first", "second", "third", "fourth"];
var result = new Array(number.length)
for(var i=0;i<number.length;i++){
result[i] = (number[i] + " " + assignment[i]);
}
then you can print them
The following will work as long as the arrays are equal in size:
var number = [1,2,3,4];
var assignment = ["one","two","three","four"];
var size = number.count;
for(var i=0; i<size; i++){
var log = number[i] + " " + assignment[i];
console.log(log);
}
Try forEach
:
var arr = [];
var number = [1, 2, 3, 4];
var assignment = ["first", "second", "third", "fourth"];
var i = 0;
number.forEach(function(e) {
arr.push(e + ' ' + assignment[i++])
});
console.info(arr)
WARNING: We assume the arrays size are equal