Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6807173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:47:58+00:00 2026-05-26T19:47:58+00:00

Somebody wrote this (very terrible) function to translate a numeric value from 0-999 to

  • 0

Somebody wrote this (very terrible) function to translate a numeric value from 0-999 to English words.

function getNumberWords(number) {
  var list = new Array(1000);
  list[000] = "zero";
  list[001] = "one";
  list[002] = "two";
  list[003] = "three";
  ///skip a few
  list[099] = "ninety nine";
  list[100] = "one hundred";
  list[101] = "one hundred and one";
  ///skip a few more
  list[997] = "nine hundred and ninety seven";
  list[998] = "nine hundred and ninety eight";
  list[999] = "nine hundred and ninety nine";
  return list[number];
}

There is some rather odd bug in here that I can’t seem to figure out the cause of. Some, but not all of the elements are placed in the wrong cell.

I tried displaying the contentes of the list and it showed a pretty funky result:

> list.toString();
"zero,one,two,three,four,five,six,seven,ten,eleven,twelve,thirteen,fourteen,
fifteen,sixteen,seventeen,twenty,twenty one,twenty two,twenty three,twenty four,
twenty five,twenty six,twenty seven,thirty,thirty one,thirty two,thirty three,
thirty four,thirty five,thirty six,thirty seven,forty,forty one,forty two,"
///(skip a few)
"sixty six,sixty seven,seventy,seventy one,seventy two,seventy three,seventy four,
seventy five,seventy six,seventy seven,,,,,sixty eight,sixty nine,,,,,,,,,
seventy eight,seventy nine,eighty,eighty one,eighty two,eighty three,eighty four,"
///(and so on)

That is, elements 0-7 have the expected value. Elements 68, 69, and 78-999 also have the expected values. Elements 64-67 and 70-77 are empty. Elements 8-63 have incorrect values.

What in the world is going on here? Why are 15 cells empty, 56 cells incorrect, and the rest correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:47:59+00:00Added an answer on May 26, 2026 at 7:47 pm

    Numeric literals starting with 0 are interpreted as octal values (if they can be) — that is, numbers in base-8. This is the case in Javascript, C, C++, PHP, Perl, Bash and many other languages.

    Now, base-8 22 is base-10 18 so you’re not accessing the elements that you think you are. Your first eight array elements were fine because, naturally, base-8 0 is also base-10 0, and so on up to 7. A value like 069 did not cause confusion because it cannot represent anything in base-8, so Javascript falls back to base-10. Yuck!


    I suggest using spacing for alignment instead:

    function getNumberWords(number) {
      var list = new Array(1000);
      list[  0] = "zero";
      list[  1] = "one";
      list[  2] = "two";
      list[  3] = "three";
      ///skip a few
      list[ 99] = "ninety nine";
      list[100] = "one hundred";
      list[101] = "one hundred and one";
      ///skip a few more
      list[997] = "nine hundred and ninety seven";
      list[998] = "nine hundred and ninety eight";
      list[999] = "nine hundred and ninety nine";
      return list[number];
    }
    

    I also suggest making a new function that generates the strings on-the-fly; it shouldn’t be taxing and certainly no more so than creating this array on every call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to copy information from an iframe to my document, I wrote this
Somebody knows an example of this? an application made with JSF 2.0 and EJB
Could somebody please name a few. I could given time, but this is for
Somebody must have come up with a solution for this by now. We are
Would somebody help me please in this question. I'm new in Matlab... And it's
I'm looking at two lines of code that somebody wrote and there is an
I've wrote a select from statement for my gaming system but it keeps returning
I recently stumbled over a problem caused by some very old code I wrote
I've wrote this little method to achieve the goal in the subj., however, is
This is my code: function func(){ for(i=0; i < 5; i++){ alert('B'); } }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.