Skip to content

Instantly share code, notes, and snippets.

@dladowitz
Created June 28, 2012 06:16
Show Gist options
  • Save dladowitz/3009465 to your computer and use it in GitHub Desktop.
Save dladowitz/3009465 to your computer and use it in GitHub Desktop.
Numbers to words (up to 99) in Javascript
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
</head>
<body>
<script>
var inWords = function(num) {
var under_20 = { 1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine", 10: "ten", 11: "eleven", 12: "twelve", 13: "thirteen", 14: "fourteen", 15: "fifteen", 16: "sixteen", 17: "seventeen", 18: "eighteen", 19: "nineteen" };
over_20 = { 0: '', 1: 'unused', 2: "twenty", 3: "thirty", 4: "forty", 5: "fifty", 6: "sixty", 7: "seventy", 8: "eighty", 9: "ninety"};
var number_in_words = "";
first_digit = "";
second_digit = "";
third_didgit = "";
second_and_third_digits = ""
if(num<20){
number_in_words = under_20[num];
}
else{
first_digit = num/10;
number_in_words = over_20[Math.floor(first_digit)];
if((num % 10) === 0){
}
else{
console.log("I'm in the second else statement")
second_digit = num % 10;
console.log(under_20[second_digit]);
number_in_words = number_in_words + under_20[second_digit];
}
}
console.log(number_in_words);
return number_in_words;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment