1

Is there a way to get prime numbers in one line I was given a task to get the largest prime number in an array in one line is that even possible?!

Given an array of numbers. Create a function which returns the largest prime number. (​NOTE*​, 
it should be written only 1 line of code). ​(2)

let arr1 = [1,5,7,6,9,10,13,11,12]
    function largestPrime(arr) {
    // write your code here... }
    }
  • Can you use also jQuery on pure Javascript only? – Jakub Adamec Jun 28 '20 at 06:40
  • @JakubAdamec this is tagged with Node.js, so I assume no. Also, I'm not really sure how jQuery would help even if it was allowed - the main focus of it is to help with DOM interactions, not with maths. – VLAZ Jun 28 '20 at 06:44
  • 4
    Since line breaks (or the absence thereof) are mostly irrelevant in JavaScript, you can put any amount of code on a single line. – Felix Kling Jun 28 '20 at 06:49
  • No I can't use jquery pure JS and I need real one line not to put all lines in one lol – Nairi Areg Hatspanyan Jun 28 '20 at 07:19

1 Answers1

1

Referring the great answer of @Sergio to the question: Number prime test in JavaScript. You can filter the prime numbers in the array then get the largest one by using the Math.max function.

let arr1 = [1,5,7,6,9,10,13,11,12]
    function largestPrime(arr) {
      return Math.max(...arr.filter((n) => { return ![...Array(n).keys()].slice(2).map(i => !(n%i)).includes(true) && ![0,1].includes(n) }));
    }
  console.log(largestPrime(arr1));
Rami Assi
  • 910
  • 2
  • 10
  • 19