Subtraction of arrays

Asked

Viewed 383 times

2

I have two arrays: A AND B

A = [1,2,3...]
B = [7,5,1...]

When I do this subtraction has the correct subtraction return:

console.log(A[0] - B[0]);

But when I play in the loop it doesn’t work:

while (i = 0) {
    A[i] - B[i]
}
  • You want to subtract each position from Array A by B or you want to remove all values you have in B from Array A?

  • Subtract array A values with B

  • It would be like creating another Array type C and in it putting the values of A-B? C1= A1-B1; C2=A2-B2...

  • I saw now that you made an edit including a While loop that I try to do, you need to store the result in a third Array in this case, your subtraction must be occurring correctly, but it is not entering the resulting value anywhere

2 answers

7


Are we going to write this code in a more organized, modern way that avoids problems? And then we will fix the two main errors in it. Always use var to declare variables (or let).

It is assigning within the condition, can not (generally), do the assignment before entering the loop, and will even create complicators to enter the loop. Is not increasing i, Then he would not leave the same element, and entering the loop would never leave. Not to mention that only subtracting is doing nothing useful, or you keep it in a variable, or you have it printed for some use.

var a = [1, 2, 3];
var b = [7, 5, 1];
var i = 0;
while (i < 3) console.log(a[i] - b[i++]);

I put in the Github for future reference.

This code works, it’s correct, but it’s not robust, it’s not generic, it just solves this case. I did so not to create confusion with new concepts or present another way to solve the problem. I only used the i++ there to make the increment. Usually when you have this need, you use the for in place of while.

An addendum for beginner. The other answer shows quite curious and even interesting things to use in some scenarios, but it is not something necessary (nor seen if it is right), is inefficient and complicates the solution for those who even master the basic syntax and control of algorithms.

  • Agreed, I agree that I ended up not taking into consideration the level of learning of who asked based on the type of question. Really in the case of beginners your solution becomes more viable, even for understanding.

  • 2

    @Viniciusgabriel may not be the best approach for a beginner, but it is still valid. More response options is always interesting.

  • Thank you very much I will continue with the studies to evolve :D

3

It has two viable options, the first, and what I believe to be better and more "clean" is with the map function():

var resultado = A.map(function(element, index) {
    return element - B[index];
});

The function map() traverse each position of your array and return a value after processing what is inside the callback function. The element parameter refers to the value it is currently accessing and the index to the position.

If you have not yet learned or are not used to using the functions map(), filter() and reduce(), you can also do it this way:

A.forEach(function(element, index, array) {
  array[index] = element - B[index];
})

The function foreach also traverses the entire array A, would be like a For, except that in for you define how many iterations you want to be made, the foreach always traverses the total number of positions of the Array (unless you manipulate this).

  • 1

    Thank you very much helped me by presenting this function . map liked mt!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.