Question

I made this javascript program to convert base 10 number into another base number (smaller than 10). I would like to know why isn't the program responding.

Number to transform:<input type="text" id="number" /><br />
Base to transform to:<input type="text" id="basen" /><br />
<input type="submit" onclick="transform()" value="Transform" />
<script>
function transform()
{
    var i=0;
    var a;
    var b;
    var c;
    var e;
    var t=0;
    var pui;
    while(Math.pow(basen.value,i) <= number.value)
    {
        i++;
    }
    var pui=i-1;
    while(pui>=0)
    {
        a=1;
        while(Math.pow(basen.value,pui)*a <= number.value-t)
        {
            a++;
        }
        b=a-1;
        e=10^pui;
        t+=b*e;
        pui--;
    }
    document.write(pui);
}

</script>
Was it helpful?

Solution 2

OK, it's pretty simple.

pui is an integer value.

Last time into the second loop is when pui = 0.

Then you decrement pui, making it -1.

Then you output pui, and that is where your -1 comes from ;)

OTHER TIPS

As Eric Jablow commented, ^ in JavaScript is a bitwise XOR operator. You want to use Math.pow(number, power) instead.

However, your whole algorithm could be replaced by the built-in Number.toString. for example:

(5).toString(4); // "11"

Also:

  • You have a syntax error here:

    b=a-1:
         ^ -- should be a semicolon, not a colon
    

    This kind of error can be easily spotted if you keep your browser console open when testing.

  • Your code relies on some magic performed by browsers, that automatically create variables based on HTML elements ID attributes. Don't do that, use document.getElementById to get element references.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top