JavaScript bootcamp · Lab 27

Is prime?

mediumFor12 minLesson: For

Read the question, write JavaScript on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read n ≥ 2. Print yes if n is prime, else no.

Input. One integer ≥ 2.

Output. yes or no.

Examples

Example 1
Input
7
Output
yes
Example 2
Input
9
Output
no
Hint
  1. Check divisors from 2 to Math.sqrt(n).
Show correct code

Peek only after you have tried. You can still Check your own version.

const n = Number(readLine());
let ok = n >= 2;
for (let d = 2; d * d <= n; d++) if (n % d === 0) { ok = false; break; }
console.log(ok ? 'yes' : 'no');
main.jsconsole.log · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.