Skip to content

Instantly share code, notes, and snippets.

@kakao-kong-j
Last active December 14, 2019 09:20
Show Gist options
  • Save kakao-kong-j/2f5d3a33a0da4ad14d52fe015f867559 to your computer and use it in GitHub Desktop.
Save kakao-kong-j/2f5d3a33a0da4ad14d52fe015f867559 to your computer and use it in GitHub Desktop.
const readline = require("readline");
const r = readline.createInterface({
input: process.stdin,
output: process.stdout
});
class Converter {
static ARGS_SIZE = 2;
static NUMBER_CHART = {
"0": 0,
"1": 1,
"2": 2,
"3": 3,
"4": 4,
"5": 5,
"6": 6,
"7": 7,
"8": 8,
"9": 9
};
static stringToNumber(str) {
return str.split("").reduceRight((acc, cur, index) => {
return acc + Converter.NUMBER_CHART[cur] * 10 ** (str.length - 1 - index);
}, 0);
}
_args = [];
getSize() {
return this.args.length;
}
get args() {
return this._args;
}
set args(args) {
if (!Array.isArray(args)) {
throw Error("args is wrong format");
}
if (args.length > Converter.ARGS_SIZE) {
throw Error("arges length is over 2");
}
this._args = args;
}
appendArg(arg) {
this.args = [...this.args, arg];
}
calculate() {
return this.args.reduce((sum, n) => sum + Converter.stringToNumber(n), 0);
}
}
r.prompt();
const converter = new Converter();
r.on("line", arg => {
converter.appendArg(arg);
if (converter.getSize() === Converter.ARGS_SIZE) {
console.log(`Your Input is : ${converter.args}\
result : ${converter.calculate()}`);
r.close();
} else {
console.log(`need more ${Converter.ARGS_SIZE - converter.getSize()} arg`);
}
r.prompt();
});
r.on("close", () => {
process.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment