I need to write code that will add non-repeating elements to a new array from an old one.
I made 2 functions similar to .includes and .push., .includes checks if the new array has the same element as the old one, if not, it returns true.
Then I turn to the .push function and add this element to the new array. The problem is that in the arrIncludesTest function loop, i is reset every time it is called and the function returns true all the time. Ready-made methods do not suit me, I have to write them myself.
function unique(arr) {
let result = [];
for (let elem of arr) {
if (arrIncludesTest(result, elem)) {
arrPush1(result, elem);
}
}
return result;
}
function arrIncludesTest(result, elem) {
for (let i = 0; i <= result.length; i++) {
if (result[i] !== elem) {
return true;
}
}
return false;
}
function arrPush1(result, elem){
result[result.length] = elem
}
console.log(unique([1,2,2,3,4,4,4,44]))