// function to convert a decimal number to its roman representation
function toRomanNumber(decimalNumber) {
if (decimalNumber == 0) {
return "";
}
const romanMap = [
{ value: 1000, numeral: "M" },
{ value: 900, numeral: "CM" },
{ value: 500, numeral: "D" },
{ value: 400, numeral: "CD" },
{ value: 100, numeral: "C" },
{ value: 90, numeral: "XC" },
{ value: 50, numeral: "L" },
{ value: 40, numeral: "XL" },
{ value: 10, numeral: "X" },
{ value: 9, numeral: "IX" },
{ value: 5, numeral: "V" },
{ value: 4, numeral: "IV" },
{ value: 1, numeral: "I" },
];
let result = "";
let remaining = decimalNumber;
for (const r of romanMap) {
while (remaining >= r.value) {
result += r.numeral;
remaining -= r.value;
}
}
return result;
}
// object that stores hours, minutes and seconds
// can advance clock for one second using tick()
// has different display options (normal, binary, roman)
let clock = {
hours: 0,
minutes: 0,
seconds: 0,
display: function () {
console.log(`${this.hours}:${this.minutes}:${this.seconds}`);
},
romanDisplay: function () {
console.log(
`${toRomanNumber(this.hours)}:${toRomanNumber(
this.minutes
)}:${toRomanNumber(this.seconds)}`
);
},
binaryDisplay: function () {
console.log(
`${this.hours.toString(2)}:${this.minutes.toString(
2
)}:${this.seconds.toString(2)}`
);
},
tick: function () {
this.seconds++;
if (this.seconds == 60) {
this.minutes++;
this.seconds = 0;
}
if (this.minutes == 60) {
this.hours++;
this.minutes = 0;
}
if (this.hours == 24) {
this.hours = 0;
}
},
};
// start of the program
///////////////////////
// get current system time
let now = new Date();
// initialize clock object
clock.hours = now.getHours();
clock.minutes = now.getMinutes();
clock.seconds = now.getSeconds();
// update clock each second
setInterval(() => {
console.clear();
clock.tick();
clock.display();
clock.binaryDisplay();
clock.romanDisplay();
}, 1000);