var solution = (s) => { let total = 0; for(let index = 0; index < s.length; index++){ if((s.length >= index + 1) && (romanMap[s[index]] < romanMap[s[index + 1]])){ total += romanMap[s[index + 1]] - romanMap[s[index]]; index ++; } else { total += romanMap[s[index]]; } } return total; }; const romanMap = { 'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000 };
package kata- var solution = (s) => {
- let total = 0;
- for(let index = 0; index < s.length; index++){
- if((s.length >= index + 1) && (romanMap[s[index]] < romanMap[s[index + 1]])){
- total += romanMap[s[index + 1]] - romanMap[s[index]];
- index ++;
- } else {
- total += romanMap[s[index]];
- }
- }
- return total;
- };
func Decode(roman string) int {return 0}- const romanMap = {
- 'I': 1,
- 'V': 5,
- 'X': 10,
- 'L': 50,
- 'C': 100,
- 'D': 500,
- 'M': 1000
- };
const strictEqual = require('chai').assert.strictEqual; function doTest (romanString, expected) { const actual = solution(romanString); strictEqual(actual, expected, `for roman number ${romanString}`); } describe("Tests", () => { it("sample tests", () => { doTest('XXI', 21); doTest('I', 1); doTest('IV', 4); doTest('MMVIII', 2008); doTest('MDCLXVI', 1666); }); });
package kata_testimport (. "github.com/onsi/ginkgo". "github.com/onsi/gomega". "codewarrior/kata")- const strictEqual = require('chai').assert.strictEqual;
var _ = Describe("test roman to decimal converter", func() {It("should give decimal number from roman", func() {Expect(Decode("XXI")).To(Equal(21))})It("should give decimal number from roman", func() {Expect(Decode("I")).To(Equal(1))})It("should give decimal number from roman", func() {Expect(Decode("IV")).To(Equal(4))})It("should give decimal number from roman", func() {Expect(Decode("MMVIII")).To(Equal(2008))})It("should give decimal number from roman", func() {Expect(Decode("MDCLXVI")).To(Equal(1666))})})- function doTest (romanString, expected) {
- const actual = solution(romanString);
- strictEqual(actual, expected, `for roman number ${romanString}`);
- }
- describe("Tests", () => {
- it("sample tests", () => {
- doTest('XXI', 21);
- doTest('I', 1);
- doTest('IV', 4);
- doTest('MMVIII', 2008);
- doTest('MDCLXVI', 1666);
- });
- });