1

I am looking to make a program that will concotate(sp?) a real number no matter the length into a string. for these purposes 1234 is fine turning into "one""two""three""four" or "onetwothreefour" or w/e. I was attempting to do this by iterating through the real number and then go through the array. my output in node is undefined through my console.log()'s shown below. Not really too sure what to do since Im new to this any help is greatly appreciated.

var datArray = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

var toEnglish = function (number) {
    for ( var i = 0; i <= number.length; i++ ) {
        var newNumber = datArray[number[i]];
        newNumber = newNumber + newNumber;
    }
    console.log(newNumber);
    return newNumber;
}
toEnglish(1234); 
2
  • 1
    Numbers don't have a length, you have to convert it to a string first. Commented Jan 27, 2015 at 6:24
  • Or you could loop through it by dividing it by 10 over and over. Commented Jan 27, 2015 at 6:25

4 Answers 4

3

You were almost there. Everything except:

  • you have to change the number into a string
  • you have to not reassign newNumber every time
  • you have to use a strict less-than comparison in the loop

So

var datArray = ["zero", "one", "two", "three", "four", 
                "five", "six", "seven", "eight", "nine"];

var toEnglish = function (number) {
  number = number.toString();
  var newNumber = "";
  for ( var i = 0; i < number.length; i++ ) {
    newNumber += datArray[number[i]];
  }
  return newNumber;
}
toEnglish(1234); 

Edit

A far more functional and terse version would be

var toEnglish = function (number) {
  return number.toString().split('').map(function(i) {
    return datArray[i]
  }).join(' ');
};

This assumes a version of Javascript that has map built in, but almost every library (jQuery, underscore, &c.) will have some equivalent.

In fact, if you are sure of a really advanced version of Javascript (e.g. in Firefox), you can write it as

var toEnglish = function (number) {
  return number.toString().split('').map(i => datArray[i]).join(' ');
}

Shweet.

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

2 Comments

thanks, I ended up getting the string part on my own when I realized what iterating or I should say attempting to iterate was solving. After I did that I saw the printing and conversion happening but I didnt consider the newNumber += datArray[number[i]]; thanks for the help.
@imnothardcore, dodging fencepost and accumulator errors is exactly why functional programing is so popular. See my edit.
0

A number does not have a length:

> console.log(1234.length);
< undefined

You must first convert it to a string:

> console.log(String(1234).length);
< 4

You also keep resetting newNumber so instead I've used an array and appended items to that (so we can join using a seperator if we wish). Lastly, I've removed <= number.length in your loop and replaced it by < number.length as otherwise we use a character that doesn't exist. Hence your code can be converted into the following:

var datArray = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

var toEnglish = function (number) {
    number = String(number); // Cast it

    var result = [];

    for ( var i = 0; i < number.length; i++ ) {
        result.push(datArray[number[i]]); // Add the text to result
    }

    return result.join(' '); // Return the results seperated by space
}

Usage:

> toEnglish(1234); 
< "one two three four"

3 Comments

You missed the fence-post error the first time, didn't you?
@Malvolio I did till I noticed the extra space at the end of four. :-)
I'm just peanut-butter-and-jealous. You got your answer in literally 10 seconds ahead of mine, and I think your push-and-join strategy is actually preferable.
0

Three things:

  • number is a number and does not have a .length property. Pass a string like "1234" instead, or cast your argument to a string.
  • You're producing newNumber by only doubling up the word for the current digit, overwriting previous results and only returning the last. Instead, initialise with the empty string and repeatedly concat to it.
  • The loop must not run up to the length of the input but one below, as sequences are indexed from 0 to n-1.

function toEnglish(number) {
    number = String(number);
    var newNumber = "";
    for (var i = 0; i < number.length; i++) {
        newNumber += datArray[number[i]];
    }
    return newNumber;
}
console.log(toEnglish(1234)); 

Comments

0

Something like this might work, but I see there are a number of answers already so whatever. Thanks for the coding exercise.

var datArray = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]

var toEnglish = function (number) {
    var exploded = number.toString().split("");

    var newNumber = "";
    exploded.forEach(function(number) {
      newNumber += datArray[ number ];
    });

    // for ( var i = 0; i <= number.length; i++ ) {
    //     var newNumber = datArray[number[i]];
    //     newNumber = newNumber + newNumber;
    // }
    console.log('newNumber', newNumber);
    return newNumber;
}
toEnglish(1234); 

3 Comments

Don't use forEach when you have map at hand.
Why? This answer comes with the hint of other programming language influences. I will Google your constructive criticism, thanks!
My comment comes with functional programming influences :-)

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.