I have this code:
const removeFromArray = function(...xNumber) {
return xNumber.reduce(function(xArray, yNumber) {
for (let i = 0; i < xArray.length; i++) {
if (xArray[i] === yNumber) {
const index = xArray.indexOf(yNumber);
xArray.splice(index, 1);
} else if (xArray[i] !== yNumber) {
continue;
}
return xArray;
}
});
};
console.log(removeFromArray([3, 4, 5, 1], 1, 3, 5, 10));
It's supposed to take in arguments that, if they match with the elements in the array, they get deleted and it returns the new array. If the argument is not related to the elements in the array, it ignores it. This function works well if I write only numbers that are related to the elements in the array. If I write a number, such as 10, that's not inside the array, it returns undefined.
In order to solve this, I need to play with conditionals I assume?
I'm sure my mistake is in the else if
conditional, having continue
in there is not good, I just didn't know what else to do.
Comments
Post a Comment