Problem Solving using JS 1 i-Assess
Sat Oct 05 2024 05:46:39 GMT+0000 (Coordinated Universal Time)
Saved by
@signup
#html
#javascript
const fs = require('fs');
// Custom error class for invalid input
class InvalidInputError extends Error {
constructor(message) {
super(message);
this.name = "InvalidInputError";
}
}
function validateInput(inputText) {
if (!/^[a-zA-Z]+$/.test(inputText)) {
throw new InvalidInputError("Invalid input, Please enter alphabet characters only");
}
return "Valid Input";
}
function main() {
fs.readFile('input.txt', 'utf8', (err, data) => {
if (err) {
if (err.code === 'ENOENT') {
console.error("The file 'input.txt' was not found.");
} else {
console.error("An unexpected error occurred:", err);
}
return;
}
const inputText = data.trim();
try {
const result = validateInput(inputText);
console.log(result);
} catch (e) {
if (e instanceof InvalidInputError) {
console.error(e.message);
} else {
console.error("An unexpected error occurred:", e);
}
}
});
}
main();
content_copyCOPY
Comments