1

I want the following function, when invoked to return an array of elements without duplicates

const removeDuplicates = nums => {
 var result = Array.from(new Set(nums));
  console.log(result)   
}
removeDuplicates([1,1,2,2,3])

Basically, I want this function to work without console.log but with invocation of it, like so removeDuplicates([1,1,2,2,3]) Please note that return is not working in this case as it stops the function from being invoked.

P.S. I have read a lot of answers related to my question, however they are not specifically answering my question; in particular, I want to invoke the removeDuplicates function with provided array of elements, like so: removeDuplicates([1,1,2,2,3]) and I expect it to return the elements without duplicates.

4
  • 3
    Why is return Array.from(new Set(nums)) not an option? Commented Apr 7, 2019 at 23:25
  • just return Array.from... Commented Apr 7, 2019 at 23:26
  • Yes, thanks, the repl on which I was trying code was not working, I guess because I have chosen to create JS/HTML/CSS repl instead of just Javascript Commented Apr 7, 2019 at 23:34
  • In that repl can log to console or do something in the dom with the results. The script is running, you just aren't doing anything with the results Commented Apr 7, 2019 at 23:35

1 Answer 1

2

I expect it to return the elements

So add a return

const removeDuplicates = nums => {
  return Array.from(new Set(nums));
}

const res = removeDuplicates([1, 1, 2, 2, 3])

console.log(res)

Or use an implicit return

const removeDuplicates = nums => Array.from(new Set(nums));
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, that is very logical/correct; however it is not working, I am trying it on repl, here is the link: repl.it/@umbur/FaintSlushyModel
I don't see you doing anything with res in the repl
The code that you've provided is in script.js of that repl; however it works fine on this repl, which was created by choosing only JS: repl.it/@umbur/HotSerpentineProcedurallanguage
Understand ... add this and run again document.body.innerHTML = JSON.stringify(res) and if you log to console the repl console will work
Does not work, but I guess it is not important, as the issue is solved now; thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.