File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * @param {number } n
3+ * @return {number }
4+ */
5+ var numberOfMatches = function ( n ) {
6+ let matches = 0 ; // initialize matches to zero
7+ let num = n ; // initialize num equal to n
8+ for ( let i = 0 ; i < n ; i ++ ) { // loop through the number n
9+ if ( num == 1 ) { // if num is equal to 1 then break the for loop
10+ break ;
11+ } else { // else
12+ if ( num % 2 == 0 ) { // if num is even
13+ let divide = num / 2 ; // divide num by 2
14+ matches += divide ; // add divide to matches
15+ num -= divide ; // subtract divide to num
16+ } else { // else
17+ let divide = ( num - 1 ) / 2 ; // subtract num by 1 and then divide it by 2
18+ matches += divide ; // add divide to matches
19+ num -= divide ; // subtract divide to num
20+ }
21+ }
22+ }
23+ return matches ; // return number matches
24+ } ;
You can’t perform that action at this time.
0 commit comments