mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2025-08-02 11:18:00 +08:00
Add a ton of JS algo snippets (#56)
This commit is contained in:
19
utilities/javascript/intToBin.js
Normal file
19
utilities/javascript/intToBin.js
Normal file
@ -0,0 +1,19 @@
|
||||
// Does not handle negative numbers.
|
||||
function intToBin(number) {
|
||||
if (number === 0) {
|
||||
return '0';
|
||||
}
|
||||
let res = '';
|
||||
while (number > 0) {
|
||||
res = String(number % 2) + res;
|
||||
number = parseInt(number / 2, 10);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
console.log(intToBin(0) === 0..toString(2) && 0..toString(2) === '0');
|
||||
console.log(intToBin(1) === 1..toString(2) && 1..toString(2) === '1');
|
||||
console.log(intToBin(2) === 2..toString(2) && 2..toString(2) === '10');
|
||||
console.log(intToBin(3) === 3..toString(2) && 3..toString(2) === '11');
|
||||
console.log(intToBin(5) === 5..toString(2) && 5..toString(2) === '101');
|
||||
console.log(intToBin(99) === 99..toString(2) && 99..toString(2) === '1100011');
|
Reference in New Issue
Block a user