Skip to content
Browse files

Coffescript and JS versions of the kata

  • Loading branch information...
1 parent d425c9b commit fab99a0b15dc71864623867967bcd0764d53a998 @cavalle cavalle committed with 12meses12katas Mar 2, 2011
Showing with 96 additions and 0 deletions.
  1. +32 −0 cavalle/roman_numerals.coffee
  2. +64 −0 cavalle/roman_numerals.js
View
32 cavalle/roman_numerals.coffee
@@ -0,0 +1,32 @@
+assert = require "assert"
+vows = require "vows"
+
+String.ROMAN_MAPPINGS = { M: 1000, CM: 900, D: 500, CD: 400, C:100, XC:90, L:50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1 }
+
+String.prototype.toArabic = ->
+ return 0 if this.length == 0
+ for roman, arabic of String.ROMAN_MAPPINGS
+ return arabic + this.substr(roman.length).toArabic() if this.substr(0,roman.length) == roman
+
+vows.describe("Roman Numerals").addBatch(
+ 'Transforms 1 to I': -> assert.equal "I".toArabic(), 1
+ 'Transforms 2 to II': -> assert.equal "II".toArabic(), 2
+ 'Transforms 3 to III': -> assert.equal "III".toArabic(), 3
+ 'Transfroms 4 to IV': -> assert.equal "IV".toArabic(), 4
+ 'Transforms 5 to V': -> assert.equal "V".toArabic(), 5
+ 'Transforms 6 to VI': -> assert.equal "VI".toArabic(), 6
+ 'Transforms 7 to VII': -> assert.equal "VII".toArabic(), 7
+ 'Transforms 8 to VIII': -> assert.equal "VIII".toArabic(), 8
+ 'Transforms 9 to IX': -> assert.equal "IX".toArabic(), 9
+ 'Transforms 10 to X': -> assert.equal "X".toArabic(), 10
+ 'Transforms 11 to XI': -> assert.equal "XI".toArabic(), 11
+ 'Transforms 14 to XIV': -> assert.equal "XIV".toArabic(), 14
+ 'Transforms 39 to XXXIX': -> assert.equal "XXXIX".toArabic(), 39
+ 'Transforms 49 to XLIX': -> assert.equal "XLIX".toArabic(), 49
+ 'Transforms 89 to LXXXIX': -> assert.equal "LXXXIX".toArabic(), 89
+ 'Transforms 99 to XCIX': -> assert.equal "XCIX".toArabic(), 99
+ 'Transforms 499 to CDXCIX': -> assert.equal "CDXCIX".toArabic(), 499
+ 'Transforms 899 to DCCCXCIX': -> assert.equal "DCCCXCIX".toArabic(), 899
+ 'Transforms 999 to CMXCIX': -> assert.equal "CMXCIX".toArabic(), 999
+ 'Transforms 1999 to MCMXCIX': -> assert.equal "MCMXCIX".toArabic(), 1999
+).run()
View
64 cavalle/roman_numerals.js
@@ -0,0 +1,64 @@
+var vows = require("vows"),
+ assert = require("assert");
+
+const ROMAN_MAPPINGS = { M:1000, CM:900, D:500, CD:400, C:100, XC:90, L:50, XL:40, X:10, IX:9, V:5, IV:4, I:1 }
+
+Number.prototype.toRoman = function() {
+ var str = "",
+ num = this.valueOf();
+
+ for (var roman in ROMAN_MAPPINGS) {
+ var arabic = ROMAN_MAPPINGS[roman];
+ while (num >= arabic) {
+ str += roman;
+ num -= arabic;
+ }
+ }
+
+ return str;
+}
+
+String.prototype.toArabic = function() {
+ var num = 0,
+ str = this.valueOf();
+
+ for (var roman in ROMAN_MAPPINGS) {
+ var arabic = ROMAN_MAPPINGS[roman];
+ while (str.substr(0, roman.length) == roman) {
+ num += arabic;
+ str = str.substr(roman.length);
+ }
+ }
+
+ return num;
+}
+
+vows.describe("Roman numerals").addBatch({
+ "Number.toRoman": {
+ "transforms 1 to I": function() { assert.equal(Number(1).toRoman(), "I"); },
+ "transforms 2 to II": function() { assert.equal(Number(2).toRoman(), "II"); },
+ "transforms 3 to III": function() { assert.equal(Number(3).toRoman(), "III"); },
+ "transforms 4 to IV": function() { assert.equal(Number(4).toRoman(), "IV"); },
+ "transforms 5 to V": function() { assert.equal(Number(5).toRoman(), "V"); },
+ "transforms 6 to VI": function() { assert.equal(Number(6).toRoman(), "VI"); },
+ "transforms 7 to VII": function() { assert.equal(Number(7).toRoman(), "VII"); },
+ "transforms 8 to VIII": function() { assert.equal(Number(8).toRoman(), "VIII"); },
+ "transforms 9 to IX": function() { assert.equal(Number(9).toRoman(), "IX"); },
+ "transforms 10 to X": function() { assert.equal(Number(10).toRoman(), "X"); },
+ "transforms 11 to XI": function() { assert.equal(Number(11).toRoman(), "XI"); },
+ "transforms 39 to XXXIX": function() { assert.equal(Number(39).toRoman(), "XXXIX"); },
+ "transforms 49 to XLIX": function() { assert.equal(Number(49).toRoman(), "XLIX"); },
+ "transforms 89 to LXXXIX": function() { assert.equal(Number(89).toRoman(), "LXXXIX"); },
+ "transforms 399 to CCCXCIX": function() { assert.equal(Number(399).toRoman(), "CCCXCIX"); },
+ "transforms 499 to CDXCIX": function() { assert.equal(Number(499).toRoman(), "CDXCIX"); },
+ "transforms 899 to DCCCXCIX": function() { assert.equal(Number(899).toRoman(), "DCCCXCIX"); },
+ "transforms 999 to CMXCIX": function() { assert.equal(Number(999).toRoman(), "CMXCIX"); },
+ "transforms 1999 to MCMXCIX": function() { assert.equal(Number(1999).toRoman(), "MCMXCIX"); }
+ },
+ "String.toArabic": {
+ "transforms I to 1": function() { assert.equal(String("I").toArabic(), 1) },
+ "transforms II to 2": function() { assert.equal(String("II").toArabic(), 2) },
+ "transforms III to 3": function() { assert.equal(String("III").toArabic(), 3) },
+ "transforms MCMXCIX to 1999": function() { assert.equal(String("MCMXCIX").toArabic(), 1999) },
+ }
+}).run();

0 comments on commit fab99a0

Please sign in to comment.
Something went wrong with that request. Please try again.