orginal.indexOf is not a function

Tell us what’s happening:
Trying to keep it short not sure why I am getting this error

  **Your code so far**

function uniteUnique(arr) {
let orginal = arr;
for (let i = 1; i < arguments.length; i++){
  let changearr = arguments[i];
  changearr.forEach(element => orginal.indexOf(element) == -1
  ? orginal = orginal.push(element)
  : console.log("hi"));
}
return arr;
}

uniteUnique([1, 3, 2, 4], [5, 2, 1, 4], [2, 1]);
//
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36

Challenge: Sorted Union

Link to the challenge:

1 Like

turns out i needed to convert element into a string because index of only works on a string object

orginal.indexOf(element) == -1
  ? orginal = orginal.push(element)
  : console.log("hi")

If you don’t have a reason to utilize the false part of the ternary you should use an if statement, and if you are really concerned about line space you can write the if as:

if (orginal.indexOf(element) == -1)  orginal.push(element);

Some people do not like ifs written like that, and find them hard to read so I would recommend using it with caution

1 Like

nope, indexOf is both a string and an array method

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.