Skip to content

Commit 4f488e4

Browse files
author
zongyanqi
committed
add Easy_504_Base_7
1 parent 555086b commit 4f488e4

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Easy_504_Base_7.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
*
3+
* Given an integer, return its base 7 string representation.
4+
5+
Example 1:
6+
Input: 100
7+
Output: "202"
8+
9+
Example 2:
10+
Input: -7
11+
Output: "-10"
12+
13+
Note: The input will be in range of [-1e7, 1e7].
14+
15+
*/
16+
17+
/**
18+
* @param {number} num
19+
* @return {string}
20+
*/
21+
var convertToBase7 = function (num) {
22+
23+
if (num === 0) return '0';
24+
var str = '';
25+
var sign = num > 0;
26+
num = Math.abs(num);
27+
while (num) {
28+
str = (num % 7) + str;
29+
num = Math.floor(num / 7);
30+
}
31+
32+
return sign ? str : '-' + str;
33+
34+
};
35+
36+
console.log(convertToBase7(0));
37+
console.log(convertToBase7(100));
38+
console.log(convertToBase7(-7));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy