Reputation: 312
var answer1=arguments[0];
var value1=arguments[1];
var value2=arguments[2];
var finalAnswer=[];
console.log(answer1);
console.log(value1);
console.log(value2);
for (var i=0;i<=answer1.length-1;i++ ){
if (answer1[i]===value2 || answer1[i]===value1){
finalAnswer= answer1.splice(i,1);
finalAnswer.pop();
}
}
console.log(answer1);
console.log(finalAnswer);
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
Hi I tried this Seek and Destroy Challenge ,which is Search for the argument objects given in the destroyer's argument and remove them. But my if statement is not working properly. Its not checking for the or option. Its not checking for the comparison of "answer1[i]===value2" option at all.
The answer should be like:[1,1] But I am getting answer:[1,3,1,3] .It is not removing 3. Any suggestions why?
Upvotes: 1
Views: 68
Reputation: 3247
Try this with Array.filter
function destroyer(){
var answer1=arguments[0];
var value1=arguments[1];
var value2=arguments[2];
answer1 = answer1.filter(function(val){
return val!==value1 && val!==value2;
});
console.log('answr1',answer1);
}
destroyer([1, 2, 3, 1, 2, 3], 2,3);
Upvotes: 2
Reputation: 756
try this cycle:
answer1.forEach(function(el){
if (el !== value1 && el !== value2) finalAnswer.push(el);
});
total will be like that:
function destroyer() {
var answer1=arguments[0];
var value1=arguments[1];
var value2=arguments[2];
var finalAnswer=[];
answer1.forEach(function(el){
if (el !== value1 && el !== value2) finalAnswer.push(el);
});
console.log(finalAnswer);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
Upvotes: 0
Reputation: 6511
You if condition works fine as it should. What's causing your results to go wrong is that your for loop does not take into account the change made to the array you are iterating.
Upvotes: 0