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