Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
Recreate Number.toString(nn) in pure javascript

I'm trying convert a number into string using nn base. Basically, trying re-create Number.toString(nn)

Here is the code I have so far which produces incorrect result:

const baseData = "0123456789abcdef";
for(let i = 0; i < 257; i += 8)
{
  console.log(i, "expected:", i.toString(16), "| actual:", toBase(i, 16));
}

function toBase(n, radix)
{
  let result = "";
  const count = ~~(n / radix);
  for(let i = 0; i < count; i++)
  {
    result += baseData[~~(i % radix)];
  }
  result += baseData[~~(n % radix)];
  return result;
}
.as-console-wrapper{top:0;max-height:unset!important;overflow:auto!important;}

There must be some kind of formula for this...

Any suggestions?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const baseData = "0123456789abcdef";
for(let i = 0; i < 257; i += 8)
{
  console.log(i, "expected:", i.toString(16), "| actual:", toBase(i, 16));
}

function toBase(n, radix)
{
  let result = "";
  while(n>0){
    result = baseData[n % radix]+result;
    n=(n - n % radix)/radix;
  }
  return result;
}

about 4 years ago · Juan Pablo Isaza Report

0

const baseData = "0123456789abcdef";
for (let i = 0; i < 257; i += 8) {
  console.log(i, "expected:", i.toString(16), "| actual:", toBase(i, 16));
}

function toBase(n, radix) {
  if (n === 0) return 0
  const chars = '0123456789abcdef'
  const result = []
  while (n > 0) {
    const mod = n % radix
    result.unshift(chars[mod])
    n = Math.floor(n / radix)
  }
  return result.join('')
}
.as-console-wrapper {
  top: 0;
  max-height: unset!important;
  overflow: auto!important;
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!