We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b0f3f52 commit f15665bCopy full SHA for f15665b
javascript/1342-number-of-steps-to-reduce-a-number-to-zero.js
@@ -0,0 +1,21 @@
1
+/**
2
+ * @param {number} num
3
+ * @return {number}
4
+ */
5
+var numberOfSteps = function (num) {
6
+ let count = 0; // initialize count to zero
7
+ let ans = num; // initialize ans to num
8
+ while (ans >= 0) { // loop the ans if anwer is greater than or equal to zero
9
+ if (ans === 0) { // if ans is zero then break while loop
10
+ break;
11
+ }
12
+ if (ans % 2 === 0) { // if ans is even then divide it by 2 and increment count
13
+ ans /= 2;
14
+ count++;
15
+ } else { // if ans is odd then decrement ans by -1 and increment count
16
+ ans -= 1;
17
18
19
20
+ return count; // return the count
21
+};
0 commit comments