0

I'm given an array of integers such as:

const vals=[
  00100,
  11110,
  00100101,
  ...
]

and I need to simply make each value iterable maintaining the position of the leading zeros. My first thought was to turn them into strings, but JavaScript wants to use base 10 and nothing that I have tried will change this. And when I try parsing it back to base 2, the leading zeros are all swapped around:

let testNum = 00100; // saves as 64
testNum = parseInt(testNum, 10); // using radix 2 returns NaN
testNum = testNum.toString(2); // 1000000

Any help would be greatly appreciated!

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Josh Carvin
  • 115
  • 1
  • 2
  • 14
  • [Number literals starting with 0 are treated as base 8 (octals)](https://stackoverflow.com/q/6505033/215552) (in non-strict mode, but since you're supposedly not getting errors, I'm assuming you're not in strict mode). – Heretic Monkey Dec 03 '21 at 21:04
  • 1
    Note that [`parseInt`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/parseInt), or preferrably [`Number.parseInt`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number/parseInt), accepts a _string_ and returns a _number_, and [`Number.prototype.toString`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number/toString) works with a _number_ as its `this` and returns a _string_. Numbers in different bases are supposed to be _strings_. – Sebastian Simon Dec 03 '21 at 21:10
  • This still doesn't help me convert `00100` to `"00100"`. I'm aware of the octet "bug" and how to work around it when dealing with base 10 numbers or other numbers that can otherwise be converted to other bases. But `00100` doesn't convert to base 2. And you're right @SebastianSimon sorry about that. I just typed those out as examples. – Josh Carvin Dec 03 '21 at 21:14
  • @JoshCarvin You can’t reliably “convert” the number to the string. You have to supply a string to begin with. – Sebastian Simon Dec 03 '21 at 21:15
  • In strict mode you can't even work with the array as it stands. Is this homework? – pilchard Dec 03 '21 at 21:16
  • So there's is no possible solution, using javascript, to get the string `"00100"` from the initial value of `00100`? – Josh Carvin Dec 03 '21 at 21:16
  • @pilchard no it's not homework. And the array isn't exactly what's provided. I just typed that up as a brief example. It's just pseudo code. The array is more complicated than this question warrants. – Josh Carvin Dec 03 '21 at 21:18
  • but it has a range of numbers some that start with 0, and there's no way of getting it with those values already cast as strings? – pilchard Dec 03 '21 at 21:19
  • 1
    `(00100 / 16).toString(2).padStart(5, '0')` :) Just kidding of course, that wouldn't work with `11110`... – Heretic Monkey Dec 03 '21 at 21:19
  • @HereticMonkey I thought about this. But what if the lengths of the values aren't 5? – Josh Carvin Dec 03 '21 at 21:20
  • Yeah, you'd have to somehow know how many digits they were originally, since JavaScript doesn't store that info: `00100.toString(8) // '100'` – Heretic Monkey Dec 03 '21 at 21:24
  • 1
    @HereticMonkey gotcha. So They just have to be strings to start with, then. Or they have to have a fixed length. Thanks! – Josh Carvin Dec 03 '21 at 21:29

0 Answers0