Advertisement
Guest User

Untitled

a guest
Sep 26th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const array1 = [1, 0, 2, 3, 4];
  2. const array2 = [3, 5, 6, 7, 8, 13];
  3.  
  4. const getMaxLengthFromArrays = (firstArray, secondArray) => Math.max(firstArray.length, secondArray.length);
  5. const isNumber = value => isNaN(value) ? 0 : value;
  6.  
  7. const getSumFromTwoArrays = (firstArray, secondArray) => {
  8.   const arrayFromTotalLength = Array.from({ length: getMaxLengthFromArrays(firstArray, secondArray) });
  9.   return arrayFromTotalLength.reduce((acc, _, idx) => {
  10.     return [...acc, isNumber(firstArray[idx]) + isNumber(secondArray[idx])]
  11.   }, []);
  12. }
  13.  
  14. console.log(computeSumFromTwoArrays(array1, array2));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement