-2

I can not test the return value of my function correctly at the time of return.

My exercise is this: I must compare two arrays and return true if they have at least one identical element but I need some assistance in figuring out what is wrong with my code:

function duplicateElements(m, n){
  function test (element){
    return n.includes(element);
  }
  return m.filter(test) != [] ? true:false;
}
8
  • it deeply hurts :) Commented Dec 3, 2016 at 9:50
  • s/he needs to compare two arrays and return true if it has at least one identical element. Commented Dec 3, 2016 at 9:53
  • Tu dois testes la propriété length de la valeur de retour -> m.filter(test).length > 0 Commented Dec 3, 2016 at 10:01
  • Ah super! Je te remercie ! Commented Dec 3, 2016 at 10:05
  • try this - jsfiddle.net/79ynncue Commented Dec 3, 2016 at 10:11

1 Answer 1

-1

You have to test the length property of the return value.

function duplicateElements(m, n) {
    function test(element) {
        return n.includes(element);
    }

    return m.filter(test).length > 0 ? true : false;
}
Sign up to request clarification or add additional context in comments.

1 Comment

It is nicer to remove ? true : false. It the > operator already returns a boolean.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.