I have a simple array with a few elems & a new elem , trying to check the elems with a function , push the new elem into the array if not already present & ignore it if it is.
I created a function with an if /else statement but the code always adds the new item to the array.
var arr=['a','b'];
var newElem='c';
function f(){
for(var i=0; i<arr.length; i++){
if(arr[i] == newElem){ console.log('Exists');return }
else {arr.push(newElem);console.log(arr); return }
}
}
f();
The code works fine if the new item not present in the array but if it's ,the new elem still being pushed into the array? Pls anyone could help , don't want to ask the teacher , it looks so simple ?