-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (64 loc) · 1.71 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const express = require('express');
const app = express();
app.use(express.json());
//ROMAN NUMERAL TO ARABIC FUNCTION
function romanToArabic (numeral) {
const romanRegex = /^(M{0,3})(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/;
if (!romanRegex.test(numeral)) {
throw new Error("Invalid Roman numeral.");
}
const translator = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
};
let total = 0;
let prevVal = 0;
for (let item of numeral) {
let currentVal = translator[item];
// if previous < current, subtract twice
if (prevVal < currentVal) total += currentVal - (prevVal*2);
else total += currentVal;
prevVal = currentVal;
}
return total;
}
//POST ENDPOINT
app.post("/functions/romanToArabic", (req, res) => {
const { input } = req.body;
if (!input || typeof input !== "string") {
return res.status(400).send({ error: "Invalid input. Please provide a Roman numeral as a string." });
}
try {
const output = romanToArabic(input.toUpperCase());
res.send({ output });
} catch (err) {
res.status(500).send({ error: err.message });
}
});
//GET - API DOCUMENTATION
app.get("/functions/romanToArabic", (req, res) => {
const docs = {
name: "romanToArabic",
description: "Converts a Roman numeral to an Arabic numeral.",
input: {
type: "string",
description: "A valid Roman numeral.",
example: "XIV",
},
output: {
type: "number",
description: "The Arabic equivalent of the provided Roman numeral.",
example: 14,
},
};
res.send(docs);
})
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on ${PORT}`)
});