You can create a set of romannumerals
let numeralSet = new Set(romannumerals);
Then you can check that each digit is in that set
let badDigits = romanDigits.filter((c) => !numeralSet.has(c))
and then check whether there are any badDigits:
if (badDigits.length) {
console.error(`Invalid roman number ${roman} contains non-digits ${badDigits}`);
}
So putting it all together
let roman = prompt("Enter roman number");
let romandigits = [...roman];
let romannumerals = ["M", "D", "C", "L", "X", "V", "I"];
let numeralSet = new Set(romannumerals);
let badDigits = romandigits.filter((c) => !numeralSet.has(c))
if (badDigits.length) {
console.error(`Invalid roman number ${roman} contains non-digits ${badDigits}`);
} else {
console.log(`${roman} is OK`);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…