JavaScript code problem with toString()

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”
Screenshot 2020-10-12 at 21.42.36

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):

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.

1 Like

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

Ah, messed up again. Facepalms

But with a little digging around in the PXT repo, found this:

Looks like it doesn’t take any arguments, unless I’m looking in the wrong spot. (Probably the latter :sweat_smile:)