Search⌘ K
AI Features

Challenge: The Nearest Power of 2

Explore how to use for and while loops in JavaScript to create a program that finds the nearest power of 2 to a specified number. Learn to handle edge cases by choosing the smaller power when two candidates are equally close, and practice coding logic that efficiently controls program flow.

Task

Write a program that takes a positive number (num) and finds the power of 22 nearest to that number. The program stores the resulting value to pwr.

If there are two candidate values, display the smaller one. For example, 22 ...