-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
70 lines (47 loc) · 2.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const romanNumberValues = require('./Values.js');
const romanNumberTranslator = new romanNumberValues();
//function that returns number from string which contains roman number
function translateRomanNumber(str) {
const strlenght = str.length;
let translation = 0;
let previousNumber = 999999;
let currentNumber;
let negativesInARow = false;
let twoInARow = false;
for (let i = 0; i<strlenght;i++) {
currentNumber = romanNumberTranslator.translate(str[i]);
if (currentNumber === 0 ) {
return "ei validi syöte";
}
//check if current number is smaller or equal than prevnumber
if (currentNumber <= previousNumber) {
negativesInARow = false;
(currentNumber==previousNumber) ? (twoInARow=true) : (twoInARow=false);
translation += currentNumber;
//if its greater than prevnumber we need to minus the prevnumber
//from current number before adding it
} else {
//this makes inputs like IIV and IVX not valid
if(twoInARow || negativesInARow) {
return "ei validi syöte";
}
negativesInARow = true;
twoInARow = false;
translation -= previousNumber*2
translation += currentNumber;
}
previousNumber = currentNumber;
}
return translation;
}
function run () {
console.log("Syötä roomalaisia numeroita ja saat palautuksena numeron kymmenjärjestelmästä")
let stdin = process.openStdin();
stdin.addListener("data", function(romanNumber) {
let translated = translateRomanNumber(romanNumber.toString().trim());
console.log("Syötit roomalaisen numeron " +
romanNumber.toString().trim() + " ja se on käännettynä: " + translated);
console.log("Halutessasi syötä uusi roomalainen numero");
});
}
run();