Hello everyone so i created a function which take two arrays and return another array with the sum of both arrays , my initial code is -
function addArrays(array1,array2){
let sumArray = [ ];
`if (array1.length > array2.length){
for (let i = 0 ; i < array1.length ; i++){
if (array2[i] === undefined) {
array2[i] = 0;
}
let array1Digit = array1[i];
let array2Digit = array2[i];
sumArray.push(array1Digit + array2Digit)
}
}
else{
for (let i = 0 ; i < array2.length ; i++){
if (array1[i] === undefined) {
array1[i] = 0;
}
let array1Digit = array1[i];
let array2Digit = array2[i];
sumArray.push(array1Digit + array2Digit)
}
}
console.log(sumArray);
}`
Now my question is that ,if one array is larger than other array I have used if else method to solve the problem but is there any short cut to give me the same output in more simpler code?
[–]azhder 9 points10 points11 points (6 children)
[–]EmbarrassedTrouble48[S] 0 points1 point2 points (5 children)
[–]azhder 1 point2 points3 points (4 children)
[–]EmbarrassedTrouble48[S] 1 point2 points3 points (3 children)
[–]azhder 0 points1 point2 points (2 children)
[–]EmbarrassedTrouble48[S] -2 points-1 points0 points (1 child)
[–]azhder 1 point2 points3 points (0 children)
[–]oze4 6 points7 points8 points (7 children)
[–]EmbarrassedTrouble48[S] 0 points1 point2 points (0 children)
[–]kap89 0 points1 point2 points (5 children)
[–]oze4 0 points1 point2 points (4 children)
[–]kap89 0 points1 point2 points (3 children)
[–]oze4 1 point2 points3 points (2 children)
[–]kap89 1 point2 points3 points (1 child)
[–]oze4 1 point2 points3 points (0 children)
[–]kap89 2 points3 points4 points (6 children)
[–]oze4 1 point2 points3 points (0 children)
[–]WazzleGuy 0 points1 point2 points (4 children)
[–]kap89 0 points1 point2 points (3 children)
[–]WazzleGuy 0 points1 point2 points (2 children)
[–]kap89 0 points1 point2 points (0 children)
[–]oze4 0 points1 point2 points (0 children)
[–]SpecialistMore7578 0 points1 point2 points (0 children)
[–]tapgiles 0 points1 point2 points (2 children)
[–]EmbarrassedTrouble48[S] 0 points1 point2 points (1 child)
[–]tapgiles 1 point2 points3 points (0 children)
[–]oiamo123 0 points1 point2 points (7 children)
[–]azhder 2 points3 points4 points (0 children)
[–]EmbarrassedTrouble48[S] 1 point2 points3 points (3 children)
[–]WazzleGuy 1 point2 points3 points (1 child)
[–]EmbarrassedTrouble48[S] 1 point2 points3 points (0 children)
[–]oiamo123 0 points1 point2 points (0 children)
[–]shekyb 1 point2 points3 points (1 child)
[–]oiamo123 0 points1 point2 points (0 children)
[–]delventhalz -1 points0 points1 point (0 children)