JavaScript Adding two arrays and returning a new array

Write a function addArrays that takes 2 arrays of numbers as parameters and returns a new array where elements at same index position are added together. For example: addArrays([1,2,3], [4,3,2,1]); // => [5,5,5,1]

I am trying to use nested for loops but its giving an incorrect answers....

  function addArrays(num1, num2){
      var result = [];
      for(var i=0; i< num1.length; i++){
        for(var j=0; j<num2.length; j++){
          result.push(num1[i] + num2[j]);
      }
      }
      return result;
    }
Answer:1

I suggest to use only one loop with a check of the length and some default values if the elements do not exists.

function addArrays(array1, array2) {
    var i,
        l = Math.max(array1.length, array2.length),
        result = [];
    for (i = 0 ; i < l; i++) {
        result.push((array1[i] || 0) + (array2[i] || 0));
    }
    return result;
}

console.log(addArrays([1, 2, 3], [4, 3, 2, 1]));
Answer:2

There is no need to nest with 2 loops. Nested loops for arrays are used if you have a 2 dimensional array.

function addArrays(num1, num2){
    var result = [];
    var size = Math.max(num1.length, num2.length);

    for(var i = 0; i < size; i++){
        result.push(num1[i] + (num2[i]);

    }
    return result;
}

You can also default it to 0 if the arrays are different lengths and you go off the end like this

(num1[i] || 0) + (num2[i] || 0)

This chooses either the number in the array, or 0 if it doesnt exist

Answer:3
function addArrays(num1, num2){
      var result = [];
      for(var i=0; i< num1.length; i++){
          result.push(num1[i] + num2[i]);
      }
      return result;
}
Answer:4

Is it possible to do this with html canvas? With this for (var i = 0; i < data.length; i += 4) { var grayscale = data[i]*0.2126 + data[i +1]*0.7152 + data[i +2]*0.0722; data[i] ...

Is it possible to do this with html canvas? With this for (var i = 0; i < data.length; i += 4) { var grayscale = data[i]*0.2126 + data[i +1]*0.7152 + data[i +2]*0.0722; data[i] ...

I am using tableexport.js to export my html table into different formats like excel, csv, pdf etc. In tableexport.js, there is code for filename and extension which is not working. window.open('...

I am using tableexport.js to export my html table into different formats like excel, csv, pdf etc. In tableexport.js, there is code for filename and extension which is not working. window.open('...

  1. tableexport jquery plugin download
  2. tableexport.jquery.plugin
  3. tableexport.jquery.plugin example
  4. tableexport.jquery.plugin cdn
  5. tableexport.jquery.plugin/html2canvas.js
  6. tableexport.jquery.plugin npm
  7. tableexport.jquery.plugin xlsx
  8. tableexport.jquery.plugin-master
  9. tableexport.jquery.plugin demo
  10. tableexport.jquery.plugin not working
  11. tableexport.jquery.plugin header
  12. tableexport.jquery.plugin/tableexport.min.js
  13. kayalshri/tableexport.jquery.plugin
  14. github table export jquery plugin
  15. html table export jquery plugin
  16. html table export jquery plugin example
  17. tableexport.jquery.plugin/master/tableexport.js
  18. how to use tableexport.jquery.plugin
  19. tableexport.jquery.plugin hhurz

So i have a modal that pops up, it makes an ajax call and gives me 3 variables back to show. In the modal there is a save button and cancel button, if the save button is clicked i want to make another ...

So i have a modal that pops up, it makes an ajax call and gives me 3 variables back to show. In the modal there is a save button and cancel button, if the save button is clicked i want to make another ...

I was reading this JavaScript code. What I don't understand, is why the author does this: (function(namespace) { //more stuff here namespace.Game = Game; })(window); What is the purpose of ...

I was reading this JavaScript code. What I don't understand, is why the author does this: (function(namespace) { //more stuff here namespace.Game = Game; })(window); What is the purpose of ...

  1. using namespace js
  2. namespace with javascript