Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions javascript/1342-number-of-steps-to-reduce-a-number-to-zero.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @param {number} num
* @return {number}
*/
var numberOfSteps = function (num) {
let count = 0; // initialize count to zero
let ans = num; // initialize ans to num
while (ans >= 0) { // loop the ans if anwer is greater than or equal to zero
if (ans === 0) { // if ans is zero then break while loop
break;
}
if (ans % 2 === 0) { // if ans is even then divide it by 2 and increment count
ans /= 2;
count++;
} else { // if ans is odd then decrement ans by -1 and increment count
ans -= 1;
count++;
}
}
return count; // return the count
};