Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

329
Views
isPrime function logs 15 as true (15 is not a prime number)

function isPrime(num){
  // loop numbers to see if any num is divisble by that number
  
  if (num < 2){ return false
    
  } else if (num === 2) {
    return true
  }
  
  for (let i = 2; i < num; i++) {
    if (num % i === 0) {
      return false
    } else {
      return true
    }
}
}

Tried changing the conditional of the for loop but still comes out as true.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Wait until the whole loop finishes before returning true, otherwise you only check the first iteration and make a decision right away whether the input is prime before checking all the rest of the numbers. Also surely you don't need to iterate the entire length of the number, Math.ceil(num/2) + 1 should suffice. This is because there's no point to ever check if anything larger than half of a number will factor into that number. (This will make a big impact on larger numbers)

function checkPrime(){
    var num = document.getElementById("num").value;
    var result = document.getElementById("result");
    var arr=[];
    if (num.length===0)
        result.innerHTML = "Please, specify a number.";
    else if (num<0)
        result.innerHTML = "Please, specify a positive number.";
    else
        result.innerHTML = isPrime(num).toString();
        
    return false;
}

function isPrime(num){
  // loop numbers to see if any num is divisble by that number
  
  if (num < 2)
      return false;
  else if (num === 2)
    return true;
  
  const max = Math.ceil(num/2) + 1;
  for (let i = 2; i < max; i++) {
    if (num % i === 0)
      return false;
  }

  return true;
}
<form onsubmit=" return checkPrime()">
      <label>Enter a number check prime</label>
      <input type="number" id="num" name="num"><br>
      <input type="submit" value="Submit" id="submit">
      <div id="result"></div>
</form>

about 4 years ago · Juan Pablo Isaza Report

0

This means you are testing any number entered to return a bool (true/false) if it is a prime number.

I would modify your code a little bit to give this function a correct answer.

function isPrime(num){
  // loop numbers to see if any num is divisble by that number
  
  if (num < 2){ 
      return false
  }else if(num==2){
      return true;
  }else{
      for (let i = 2; i < num; i++) {
           if (num % i == 0) {
             return false;
             //break;
            }
       }
      return true;
  }
  
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!