this is my first question as I'm learning programming for few days but now and I'm stuck
Task:
Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result.
It should remove all values from list
a
, which are present in listb
keeping their order.
arrayDiff([1,2],[1]) == [2]
If a value is present inb
, all of its occurrences must be removed from the other:
arrayDiff([1,2,2,2,3],[2]) == [1,3]
My solution:
function arrayDiff(a, b) {
for (var j = 0; j < a.length; j++) {
for (var i = 0; i < b.length; i++) {
if(a[j] == b[i]) {
a.splice(j);
j--;
}
}
}
return a;
}
Looks like I'm doing something wrong and peculiar thing happens: sometimes tests turn all red, sometimes all BUT 1 test turn red, while rest is green. The one that always fails me is:
Should pass Basic tests
a
was [1,2]
, b
was [1]
: expected []
to deeply equal [ 2 ]