Home > Net >  convert the given array to an array of Boolean where Each element is true if the number is a prime n
convert the given array to an array of Boolean where Each element is true if the number is a prime n

Time:02-21

I need to find out either a number is Prime or not in an array. in the beginning I thought it's easy, but .. :)

First I tried this version :

function primeValues(arr) {
  let newArr = []
  for( let el of arr) {
    if (el <= 2 ) {
      newArr.push(true)
    }
    for (let j=2; j<el; j  ) {
      if (el%j === 0) {
        newArr.push(false)
      } 
      if (el%j !==0 ) {
        newArr.push(true)
      } 
    }    
  }
  return newArr;
}  

console.log(primeValues([17, 3, 21]));

But every time it goes through for loop, It pushes True or False in my new array :/`

(35) [true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false, true, true, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true]

what should I do? :/

CodePudding user response:

function primeValues(arr) {
  return arr.map(element => {
    const x = Math.abs(element);
    if(x <= 2) return true;
    if(x % 2 === 0) return false;
    for(let i = 3; i <= Math.sqrt(x); i =2) {
      if(x % i === 0) {
        return false;
      }
    }
    return true;
  });
}

console.log(primeValues([7,9,11,13,-21,2,54]));

I added some optimizations to the code

  1. We check only odd numbers because an element can't be divisible by an even number if it's not already divisible by 2.
  2. We don't need to check ALL the numbers less than "element" - we need to check only numbers from 3 to square root of the element. If you need any additional explanation why - I can give it.
  3. I suggest using "map" instead of creating an array and pushing there - it's more efficient. Also, it protects you from missing "else" and "break" statements - every check immediately finishes when true or false is added to the answers list.

UPD: Added Math.abs function call, so negative numbers will be processed correctly.

CodePudding user response:

this way...

console.log( JSON.stringify( primeValues([17, 3, 21] )))

function primeValues(arr)
  {
  let result = [], modulo;
  for( let el of arr) 
    {
    if (el <= 2 ) 
      result.push(true)
    else                  // this one is missing in your code
      {
      modulo = 0         // modulo assignement must be there
      for (let j = 2; j < el; j  ) 
        {
        modulo = el % j
        if (modulo === 0) 
          {
          result.push(false)
          break          // this other one is missing in your code
          } 
        } 
      if (modulo !== 0)  result.push(true) // outside the loop
      }
    }
  return result;
  }

But you might prefer to code it like this:

console.log( JSON.stringify( primeValues([ 17, 3, 21 ] )))

function primeValues(arr)
  {
  let result = []
    , isNotPrim
    ;
  for (let el of arr) 
    {
    isNotPrim = false    
    if (el > 2)
    for (let j = 2; j < el; j  ) 
    if (isNotPrim = !(el %j))   
      break
      ;
    result.push( !isNotPrim )
    }
  return result;
  } 

  • Related