main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
540 bytes
|
Line | |
---|
1 | 'use strict';
|
---|
2 |
|
---|
3 | var GetIntrinsic = require('get-intrinsic');
|
---|
4 |
|
---|
5 | var $floor = GetIntrinsic('%Math.floor%');
|
---|
6 |
|
---|
7 | // https://runestone.academy/ns/books/published/pythonds/BasicDS/ConvertingDecimalNumberstoBinaryNumbers.html#:~:text=The%20Divide%20by%202%20algorithm,have%20a%20remainder%20of%200
|
---|
8 |
|
---|
9 | module.exports = function intToBinaryString(x) {
|
---|
10 | var str = '';
|
---|
11 | var y;
|
---|
12 |
|
---|
13 | while (x > 0) {
|
---|
14 | y = x / 2;
|
---|
15 | x = $floor(y); // eslint-disable-line no-param-reassign
|
---|
16 | if (y === x) {
|
---|
17 | str = '0' + str;
|
---|
18 | } else {
|
---|
19 | str = '1' + str;
|
---|
20 | }
|
---|
21 | }
|
---|
22 | return str;
|
---|
23 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.