ICTMitchell
ICTMitchell

Reputation: 91

JS Numbers to text with For-loop

i am kinda new to javascript and i am stuck at a javascript for-loop exercise, the problem is i need to return the filled in telephone number to text it have to look like this:

(this is just an example)

filled in number: 0332141 how i have to return it: zero - three - three - two - one - four - one

This is the code i already have and i have to use this:

 <!DOCTYPE html>
 <html lang = "nl">
 <head>
 <meta http-equiv="Content-Type"
   content="text/html"
   charset=UTF-8/>
     <title>Lab11/title>
  </head>
  <body>
   <script>
   var number = new Array();
   number[0] = 'zero';
   number[1] = 'one';
   number[2] = 'two';
   number[3] = 'three';
   number[4] = 'four';
   number[5] = 'five';
   number[6] = 'six';
   number[7] = 'seven';
   number[8] = 'eight';
   number[9] = 'nine';

    var phonenumber = prompt('What is youre phonenumber?');
   document.write('<br> your phonenumber is: ' + phonenumber);
   </script>
   </body>
   </html>

but this ofcourse only shows the digits, so as the example above i need to return the digits in words, any ideas on how to do this?

Upvotes: 2

Views: 1948

Answers (4)

Deep Duggal
Deep Duggal

Reputation: 15

Here's the fastest way:

var h1 = document.body.getElementsByTagName('h1')[0];

// Convert number to words
function numToText(num) {
  num = num.toString(); //Convert number to string
  var text = '<br>'; 
  var numbers = {'0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine'};

  var len = num.length;
  for(var i = 0; i < len; i++) {
    text += numbers[num.charAt(i)];
    if(i < len-1)  text += ' - ';
  }
  return text;
}

// Form Handler
var form = document.forms[0];
form.onsubmit = function(){
    var input = form.elements[0].value;
    document.body.innerHTML += numToText(input);
    return false; //prevent page refresh after submit
};
<h1>What is you're phone number?</h1>
<form id="form">
  <input type="text" name="text" value="">
  <input type="submit" name="submit" value="submit">
</form>

Upvotes: 0

kevin ternet
kevin ternet

Reputation: 4612

This code should be usefull. It simply splits the input string into a array of figure, transforms each figures into corresponding string and finally joins the array into a "-" separeted string.

var table = ['zero','one','two','three','four','five','six','seven','eight','nine'];
var phone = "012658"
var number2text = phone.split("").map(x => table[x]).join("-");
console.log(number2text);

Upvotes: 2

computnik
computnik

Reputation: 327

To get words for the number entered, use the following snippet (after you have prompted for number

var phoneString = "";
for (i=0; i<phonenumber.length; i++){
  phoneString +=(number[phonenumber[i]] + " ");
}
document.write('<br> your phonenumber is: ' + phoneString);

Upvotes: 0

Hydrothermal
Hydrothermal

Reputation: 5081

   var number = new Array();
   number[0] = 'zero';
   number[1] = 'one';
   number[2] = 'two';
   number[3] = 'three';
   number[4] = 'four';
   number[5] = 'five';
   number[6] = 'six';
   number[7] = 'seven';
   number[8] = 'eight';
   number[9] = 'nine';

    var phonenumber = prompt('What is youre phonenumber?');
    phonenumber = phonenumber.split("");
    
    for(var i = 0; i < phonenumber.length; i++) {
        phonenumber[i] = number[phonenumber[i]];
    }
    
    phonenumber = phonenumber.join(" - ");
   document.write('<br> your phonenumber is: ' + phonenumber);
 <!DOCTYPE html>
 <html lang = "nl">
 <head>
 <meta http-equiv="Content-Type"
   content="text/html"
   charset=UTF-8/>
     <title>Lab11</title>
  </head>
  <body>
   </body>
   </html>

This should work for you. It splits the response from the prompt into an array of one digit per element, then loops through it and replaces each digit with its name.

Upvotes: 1

Related Questions