Skip to content

Commit 128da69

Browse files
author
Jaskaran Singh
authored
Create reduce_to_zero.js
1 parent b73f4fa commit 128da69

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

reduce_to_zero.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number} num
3+
* @return {number}
4+
*/
5+
var numberOfSteps = function(num) {
6+
let steps = 0;
7+
while(num !== 0) {
8+
if(num % 2 === 0) {
9+
num /= 2;
10+
steps++;
11+
} else {
12+
num--;
13+
steps++;
14+
}
15+
}
16+
17+
return steps;
18+
};

0 commit comments

Comments
 (0)