Explaining Leetcode’s Number of Steps to Reduce a Number to Zero Problem (JavaScript)

  1. Dividing by two if it’s even, or
  2. Subtracting one, then dividing by two if it’s odd
function numberOfSteps(num) {
let steps = 0;
while (num > 0) {
if (num % 2 === 0) {
num /= 2;
steps++;
} else {
num--;
steps++;
}
}
return steps;
}
  • Create a steps variable an initialize it with a value of 0.
  • Is 14 greater than 0? Of course, so we enter the loop
  • If we divide 14 by 2, do we get a remainder of 0? We do, so this condition evaluates to true, and we execute the following block by dividing it by 2 (resulting in 7), and incrementing our steps variable to 1.
  • Now num is 7, and still greater than 0, so we enter the loop again.
  • If we divide 7 by 2, do we get a remainder of 0? Nope, so we ignore this.
  • Next we have to subtract 1 from 7 resulting in 6, and increment our steps counter to 2.
  • Now num is 6, and still greater than 0, so we enter the loop again.
  • If we divide 6 by 2, do we get a remainder of 0? We do, so this condition evaluates to true, and we execute the following block by dividing it by 2 (resulting in 3), and incrementing our steps counter to 3.
  • Now num is 3, and still greater than 0, so we enter the loop again.
  • If we divide 3 by 2, do we get a remainder of 0? Nope, so we ignore this.
  • Next we have to subtract 1 from 3 resulting in 2, and increment our steps counter to 4.
  • Now num is 2, and still greater than 0, so we enter the loop again.
  • If we divide 2 by 2, do we get a remainder of 0? We do, so this condition evaluates to true, and we execute the following block by dividing it by 2 (resulting in 1), and incrementing our steps counter to 5.
  • Now num is 1, and still greater than 0, so we enter the loop again.
  • If we divide 1 by 2, do we get a remainder of 0? Negative Ghost Rider, so we ignore this.
  • Next we have to subtract 1 from 1 resulting in 0, and increment our steps counter to 6.
  • Now num is 0. Is 0 greater than 0? It is not, they’re the same, so the while loop stops execution and the program returns steps, which as this point is 6.

--

--

--

Always learning.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to build modern SPA’s on Salesforce

JavaScript30 challenge — Improving as a developer. Day 1.

How to Build a Command Line Note Taking App

JS Job Story[arr.reverse()]

APEX e2e Testing automation with Jest & Puppeteer #2

Native App for urban farming

Part 2: Parse website HTML with NodeJS and cheerio

Install a Cosmic-powered Nuxt.js App on Digital Ocean in 5 minutes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Saul Feliz

Saul Feliz

Always learning.

More from Medium

AlphaCamp Leetcode 訓練營06. Stack & Queue & Tree — 觀念入門

JavaScript Challenge: 66. Plus One

Array chunk algorithm

JavaScript LeetCode Best Time to Buy and Sell Stock