Main Menu

  • Home
  • jQuery
  • MooTools
  • CSS
  • JavaScript
  • Ruby on Rails
The True Tribe

Search, View and Navigation

JavaScript

JavaScript Challenge: Cardinal Numbers

Last Updated (Sunday, 12 October 2008 01:14) Written by Jonah Dempcy Tuesday, 02 September 2008 00:39

Attention, open in a new window. PDFPrintE-mail

JavaScript - JavaScript Challenges


 

This challenge is to write functions to add and subtract numbers using the names of numbers as strings. More precisely, the challenge is to write add() and subtract() functions that take two strings as input, and return one string. For example:
add("one", "ten"); // should return "eleven"
subtract("twenty", "fifteen"); // should return "five"
For the purposes of this exercise, we will limit the maximum numbers handled to 99. So, you don't have to handle, say, add("three-thousand-five-hundred-and-fifty-two", "ten-million-and-seven"). Though, you certainly get bonus points if you handle this case as well.

Here is my solution below, which is by no means optimal. The following is my first attempt at solving this issue, coded in roughly 30 minutes, so go easy if you find any SNAFUs.

Note: I prefer 4-space indentation but I set it to 2-space for this article so the code example will fit on the page without horizontal scrolling.
var cardinalNumbers = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
"ten": 10,
"eleven": 11,
"twelve": 12,
"thirteen": 13,
"fourteen": 14,
"fifteen": 15,
"sixteen": 16,
"seventeen": 17,
"eighteen": 18,
"nineteen": 19
}

var multiplesOfTen = {
"twenty": 20,
"thirty": 30,
"forty": 40,
"fifty": 50,
"sixty": 60,
"seventy": 70,
"eighty": 80,
"ninety": 90
}

function convertWordToNumber(word) {
var number = new Number(); // this is where we will store the result

if (word.indexOf("-") == -1) { // If it is less than 20
number = cardinalNumbers[word];
} else {
var multipleOfTen = word.split("-")[0]; // e.g. "seventy"
var cardinalNumber = word.split("-")[1]; // e.g. "six"
number = multiplesOfTen[multipleOfTen] + cardinalNumbers[cardinalNumber];
}

return number;
}

function convertNumberToWord(number) {
var word = new String(); // this is where we will store the result

if (number < 20) {
for (var cardinalNumber in cardinalNumbers) {
if (number == cardinalNumbers[cardinalNumber]) {
word = cardinalNumber;
break;
}
}
} else if (number < 100) {
if (number % 10 == 0) { // If the number is a multiple of ten
for (var multipleOfTen in multiplesOfTen) {
if (number == multiplesOfTen[multipleOfTen]) {
word = cardinalNumber;
break;
}
}
} else { // not a multiple of ten
for (var multipleOfTen in multiplesOfTen) {
for (var i = 9; i > 0; i--) {
if (number == multiplesOfTen[multipleOfTen] + i) {
word = multipleOfTen + "-" + convertNumberToWord(i);
break;
}
}
}
}
} else {
alert("We don't handle numbers greater than 99 yet.");
}

return word;
}

// These functions takes words as inputs, e.g. "one" and "two"

function add(x, y) {
return convertNumberToWord(convertWordToNumber(x) + convertWordToNumber(y));
}

function subtract(x, y) {
return convertNumberToWord(convertWordToNumber(x) - convertWordToNumber(y));
}


// Test convertWordToNumber()
alert(convertWordToNumber("three")); // should alert 3
alert(convertWordToNumber("seventy-three")); // should alert 73


// Test convertNumberToWord()
alert(convertNumberToWord(8)); // should alert "eight"
alert(convertNumberToWord(46)); // should alert "forty-six"


// Test add()
alert(add("one", "two")); // should alert "three"

// Test subtract()
alert(subtract("fifteen", "eleven")); // should alert "four"
Feel free to post your solutions in the comments. Particularly good solutions will get an entire article devoted to them.
Next >

Add your comment

Your name:
Your email:
Your website:
Subject:
Comment:
  The word for verification. Lowercase letters only with no spaces.
Word verification:
yvComment v.1.18.4

Additional Information

What is your job title?












Results

 © The True Tribe, 2008. Created by Jonah Dempcy and Alex Grande.    We are now accepting freelance work. Email us for rates and more information.