romannumbers update
This commit is contained in:
parent
e23257005a
commit
ed0a398a63
|
@ -1,16 +1,45 @@
|
|||
// Реализовать создание следующую запись ;]
|
||||
// ...
|
||||
|
||||
Number.prototype.I = Array.from(Array(1).keys())
|
||||
Number.prototype.II = Array.from(Array(2).keys())
|
||||
Number.prototype.III = Array.from(Array(3).keys())
|
||||
Number.prototype.IV = Array.from(Array(4).keys())
|
||||
Number.prototype.V = Array.from(Array(5).keys())
|
||||
Number.prototype.VI = Array.from(Array(6).keys())
|
||||
Number.prototype.VII = Array.from(Array(7).keys())
|
||||
Number.prototype.VIII = Array.from(Array(8).keys())
|
||||
Number.prototype.IX = Array.from(Array(9).keys())
|
||||
Number.prototype.X = Array.from(Array(10).keys())
|
||||
const romanToDecMap = {
|
||||
'I': 1,
|
||||
'V': 5,
|
||||
'X': 10,
|
||||
'L': 50,
|
||||
'C': 100,
|
||||
'D': 500,
|
||||
'M': 1000
|
||||
}
|
||||
|
||||
function romanToDec(roman) {
|
||||
let res = 0;
|
||||
for (let i = 0; i < roman.length; i++) {
|
||||
cur = romanToDecMap[roman[i]];
|
||||
next = romanToDecMap[roman[i + 1]];
|
||||
if (cur >= next || next === undefined) {
|
||||
res += cur;
|
||||
} else {
|
||||
res += -cur + next;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
function nArr(n) {
|
||||
return Array.from(Array(n).keys());
|
||||
}
|
||||
|
||||
const proxyHandler = {
|
||||
get: function (target, prop, receiver) {
|
||||
const n = romanToDec(prop);
|
||||
return nArr(n);
|
||||
}
|
||||
};
|
||||
|
||||
const proxy = new Proxy(Number.prototype, proxyHandler);
|
||||
Object.setPrototypeOf(Number.prototype, proxy);
|
||||
|
||||
|
||||
console.log(0..V); // [0, 1, 2, 3, 4];
|
||||
console.log(0..VII); // [0, 1, 2, 3, 4, 5, 6];
|
Loading…
Reference in New Issue
Block a user