here are two arrays with individual values, write a JavaScript program to compute the sum of each individual index value from the given arrays.

Sample array :

array1 = [1,0,2,3,4];

array2 = [3,5,6,7,8,13];

Expected Output :

[4, 5, 8, 10, 12, 13]

let array1 = [1,0,2,3,4];

let array2 = [3,5,6,7,8,13];

let sum = [];

console.log(array1[5]);

let max = array1.length > array2.length ? array1.length :array2.length;

for(let i = 0 ; i < max ; i++){

if(array1[i] === undefined ){

array1[i] = 0;

}else if(array2[i] === undefined){

array2[i] = 0;

}

let temp = array1[i] + array2[i];

sum.push(temp);

}

1 Like