I wanted to use toString(radix) where radix is, say, 2 to display a number in binary.
But the JS window brings up this error: “Expected 0 arguments but got 1”
Is that a bug?
I wanted to use toString(radix) where radix is, say, 2 to display a number in binary.
But the JS window brings up this error: “Expected 0 arguments but got 1”
Is that a bug?
Well, according to w3schools that’s not a bug.
If it’s like that in JavaScript, I’m guessing it would be the same for Typescript too, since TS is based on JS.
In your example, I’m guessing astring
would be "3"
if you didn’t pass in any parameters to toString
.
This should normally work, toString is supposed to take an optional “radix” argument (2 for binary, 16 for hexadecimal): https://github.com/microsoft/TypeScript/blob/master/src/lib/es5.d.ts#L522
interface Number {
/**
* Returns a string representation of an object.
* @param radix Specifies a radix for converting numeric values to strings. This value is only used for numbers.
*/
toString(radix?: number): string;
However, MakeCode’s Static TypeScript doesn’t appear to understand that and produces the unexpected argument error.
Here’s a quick and inefficient hack, as long as you’re OK with only using it on positive integers:
function bitString (v: number) {
let out = ""
let bit = 1
while (v > 0) {
out = ((v & bit) ? "1" : "0") + out
v &= ~bit
bit *= 2
}
return out
}
let a = 244
console.log(bitString(a))