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

119
Views
Javascript bucles opdracht

Wie kan mij helpen bij de volgende vraag Ik geraak er maar niet uit... Javascript-loops.

Schrijf een script waarbij je één getal ingeeft a través de een prompt bij het inladen van de pagina. In de console toon je het eerstvolgende cijfer dat een priemgetal is. Voorbeeld: als ik ingeef '20' dan krijg ik in de console '23' (quiero 23 es een priemgetal en is hoger dan 20).

alvast bedankt voor de hulp.

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

0

Gebruik een while-lus. Als het getal geen priemgetal is, blijf het getal optellen totdat het een priemgetal is. Dan console.log el número.

 const isPrime = num => { for(let i = 2, s = Math.sqrt(num); i <= s; i++) if(num % i === 0) return false; return num > 1; } let input = prompt("Voer nummer in") while (!isPrime(input)) { input++ } alert(input) console.log(input)

about 4 years ago · Juan Pablo Isaza Report

0

// Function that returns true if n is prime else returns false function isPrime(n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip middle five numbers in below loop if (n%2 == 0 || n%3 == 0) return false; for (let i=5; i*i<=n; i=i+6) { if (n%i == 0 || n%(i+2) == 0){ return false; } } return true; } // Function to return the smallest prime number greater than N function nextPrime(N) { // Base case if (N <= 1) { return 2; } let prime = N; let found = false; // Loop continuously until isPrime returns true for a number greater than n while (!found) { prime++; if (isPrime(prime)) { found = true; } } return prime; } // Driver code const num = prompt() console.log(nextPrime(num))
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!