2

As a result of finding this interesting question, I decided to write an example in JavaScript which implemented the logic and contribute it back to the question. The problem is that I'm having some issues implementing the logic. I can speak Ruby which is what I'm basing my implementation on, but I'm having an issue with an endless while loop which I'm having trouble sorting out.

I have the whole implementation up on js.do.it here: http://jsdo.it/rfkrocktk/k9Jq

function encode(i) {
   if (i == 0) return DICTIONARY[0];

   var result = '';
   var base = DICTIONARY.length;

   while (i > 0) {
       result += DICTIONARY[i % base];
       i = i / base;
   }

   result = result.reverse();
   return result;
}

What am I doing wrong here?

1
  • / probably isn't doing what you expect. 1/2 => 0.5 Commented Sep 25, 2011 at 21:49

1 Answer 1

1

Javascript uses floating point math by default. Use i = Math.floor(i / base);

Sign up to request clarification or add additional context in comments.

1 Comment

That was definitely one of the issues, fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.