JavaScript bootcamp · Lab 44

Power of two?

mediumOperators10 minLesson: Operators

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

QuestionHint and solution stay closed until you open them

Read n; log yes if it is a power of two, else no.

Input. Integer ≥ 1.

Output. yes or no.

Examples

Example 1
Input
8
Output
yes
Example 2
Input
6
Output
no
Hint
  1. n > 0 && (n & (n - 1)) === 0
Show correct code

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

const n = Number(readLine());
console.log(n > 0 && (n & (n - 1)) === 0 ? 'yes' : 'no');
main.jsconsole.log · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.