Start learning free

LESSON 215 OF 671 · WHILE LOOPS - CHALLENGES

Halving Steps in JavaScript: Dividing by 2 in a while Loop

Practice Read the full explanation

Given a positive integer n, return how many times you have to divide it by 2 until it becomes 1.

Every division is an integer division: the remainder is dropped, so 9 divided by 2 gives 4. Keep dividing the new value by 2 until it is 1, and count the divisions. If n is already 1, no division is needed.

 

Example 1:

Input: n = 8
Output: 3
Explanation: 8 → 4 → 2 → 1: three divisions by 2.

Example 2:

Input: n = 9
Output: 3
Explanation: 9 → 4 → 2 → 1. 9 divided by 2 gives 4, because the remainder 1 is dropped.

Example 3:

Input: n = 1
Output: 0
Explanation: n is already 1, so there is nothing to divide.

 

Constraints:

  • 1 <= n <= 109
STEP 1 OF 5
    Ctrl + Enter to check

    JAVASCRIPT · WHILE LOOPS - CHALLENGES

    Halving Steps in JavaScript, explained

    Problem overview

    The method halvingSteps receives a positive integer n and returns how many times it must be divided by 2 before it becomes 1. Each division is an integer division, so the remainder is dropped: 9 divided by 2 gives 4. For n = 8 the chain is 8, 4, 2, 1, which is three divisions. For n = 9 it is 9, 4, 2, 1, also three. For n = 1, no division is needed and the answer is 0.

    Approach: loop until the value reaches 1

    You don't know in advance how many divisions you'll need, so a while loop fits: keep halving while n is bigger than 1, and add 1 to a counter each time. The loop stops by itself the moment n is 1, and the counter holds the answer.

    JavaScript's / is not an integer division. It keeps the fraction, so 9 / 2 is 4.5. Wrapping the result in Math.floor rounds it down to the whole number the lesson wants: Math.floor(9 / 2) is 4.

    Step by step

    1. Declare steps with let, starting at 0. It counts the divisions, and it changes inside the loop.
    2. Loop while n > 1. When n is 1 from the start, the loop doesn't run and the answer stays 0.
    3. Halve n: n = Math.floor(n / 2). Every pass makes n smaller, so it must reach 1 and the loop must end.
    4. Add 1 to steps with ++. One pass is one division.
    5. Return steps after the loop.

    The solution in JavaScript

    class Solution {
        halvingSteps(n) {
            let steps = 0;
            while (n > 1) {
                n = Math.floor(n / 2);
                steps++;
            }
            return steps;
        }
    }
    

    For halvingSteps(9), the values after each pass are:

    Passnsteps
    141
    222
    313

    After the third pass n is 1, so the loop ends and the method returns 3.

    • The loop condition is n > 1, not n > 0. The task stops at 1. Looping while n > 0 would do one extra division, from 1 to 0, and return one too many.
    • Floor and the odd numbers. For an odd number, halving rounds down: 9 gives 4, 5 gives 2 and 3 gives 1. Every number from 2 up gets strictly smaller, so the loop can't get stuck.

    Edge cases and common mistakes

    • One. halvingSteps(1) returns 0.
    • Two and three. halvingSteps(2) and halvingSteps(3) both return 1: 2 becomes 1, and 3 becomes 1 after rounding down.
    • Powers of two against numbers just above them. 8 and 9 both need 3 divisions, and 16 needs 4. The answer grows by one each time n reaches the next power of 2.
    • The largest input. halvingSteps(1000000000) returns 29, so even the biggest value needs only a few dozen passes. Each pass halves what is left, so the number of passes grows very slowly as n grows.
    • Skipping Math.floor. Without it, n becomes 4.5, then 2.25, then 1.125, then 0.5625. The loop passes 1 without stopping on it and ends only when n is no longer above 1, so the count is too high.
    • Forgetting steps++. The loop would still end, but the method would return 0 every time.

    Testing

    • n = 8 gives 3.
    • n = 9 gives 3.
    • n = 1 gives 0.
    • n = 2 gives 1.
    • n = 1000000000 gives 29.
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free