-1

I'm slowly learning JavaScript, and to this point I could figure everything out on my own. But I'm just spending waaay to much time at this problem.

Basically, I have two arrays, and if a number in the second array matches a number in the first array, it has to delete that number.

So the simplest solution I can think of is a for loop that loops through each property of the array, and if it doesn't match a number out of the second array, push it into a new array.

I have written this bit of code:

var arr = [1, 2, 3, 5, 1, 2, 3], newArr = [2, 3, 3];
var finalArr = [];

for (var i = 0; i < newArr.length; i++) { 
 if (arr[i] != newArr[0] && arr[i] != newArr[1] && arr[i] != newArr[2]) 
     finalArr.push(arr[i]);
}

// ----> finalArr = [1]

The purpose is, it takes every value out of the array named "arr", compares it to a value of "newArr", and if the value doesn't match, push it into a new array.

Can anyone see the problem?

Thanks in advance!

kucb
  • 17
  • 1
  • 1
    You haven't asked a good question or even said what the issue is. Please say what you expect it to do, and what's it's actually doing. – Carcigenicate Jul 16 '17 at 15:18
  • 3
    You are looping through `newArr`, not `arr`, is that the problem? – LarsW Jul 16 '17 at 15:18
  • Which array should the number be deleted from, the first or second? – Brett DeWoody Jul 16 '17 at 15:19
  • 1
    Possible duplicate https://stackoverflow.com/questions/1187518/javascript-array-difference – Nirav Joshi Jul 16 '17 at 15:20
  • 1
    Possible duplicate of [JavaScript array difference](https://stackoverflow.com/questions/1187518/javascript-array-difference) – lkdhruw Jul 16 '17 at 15:21
  • The purpose is, it takes every value out of the array named "arr", compares it to a value of "newArr", and if the value doesn't match, push it into a new array. – kucb Jul 16 '17 at 15:24

2 Answers2

1
var arr = [1, 2, 3, 5, 1, 2, 3], newArr = [2, 3, 3];
var finalArr = [];

for (var i = 0; i < arr.length; i++) { 
    if (arr[i] != newArr[0] && arr[i] != newArr[1] && arr[i] != newArr[2]) 
        finalArr.push(arr[i]);
}

// result: [1,5,1]

This is your corrected code, just note that second array does not always need to contain three elements, you could loop through it as well with a for loop

Killer Death
  • 459
  • 2
  • 10
1

The purpose is, it takes every value out of the array named "arr", compares it to a value of "newArr"...

You're not iterating over every value of arr, since you've newArr.length in your loop. You need to iterate till arr.length instead

...if the value doesn't match, push it into a new array.

Your code assumes newArr is always of length 3. Maybe it is. But, a more generic way is to use Array#indexOf which returns the first index at which a given element can be found in the array, or -1 if it is not present.

var arr = [1, 2, 3, 5, 1, 2, 3],
  newArr = [2, 3, 3];
var finalArr = [];

for (var i = 0; i < arr.length; i++) {
  if (newArr.indexOf(arr[i]) === -1) {
    finalArr.push(arr[i]);
  }
}

console.log(finalArr);
maazadeeb
  • 5,922
  • 2
  • 27
  • 40