#### Calculator Node JS

``````//  import our package

// Creating our interface

input: process.stdin,
output: process.stdout,

});

const validOperators = [
"*",
"-",
"+",
"/",
];

calcInterface.question("What operation would you like to perform? ",
(operatorInput) => {
if(!validOperators.includes(operatorInput)) {

// Invalid operator
console.log("That is not a valid operation, Please try again!");
return calcInterface.close();
}

calcInterface.question("Please enter the first number ", (firstInput) => {
const firstNumber = Number(firstInput);
if(isNaN(firstNumber)) {

// Invalid number
console.log("This is not a number, Please try again!");
return calcInterface.close();
}

calcInterface.question("Please enter the second number ", (secondInput) => {
const secondNumber = Number(secondInput);
if(isNaN(secondNumber)) {

// Invalid number
console.log("This is not a number, Please try again!", );
return calcInterface.close();
}

const result = calculatorLogic(firstNumber, operatorInput, secondNumber);

calcInterface.close();
});
});
});

function calculatorLogic(firstNumber, operator, secondNumber) {
if(operator === "+") return firstNumber + secondNumber;
else if(operator === "-") return firstNumber - secondNumber;
else if(operator === "*") return firstNumber * secondNumber;
else if(operator === "/") return firstNumber / secondNumber;
}
``````

1 Java Script + Node.js计算器问题

2 JS中的计算器

4 我已经尝试编写这个 node.js 计算器代码有一段时间了

5 JS中的简单计算器

6 在 JS 中构建计算器的问题

7 js中的衣服尺寸计算器

8 JS表格计算器

9 尝试用 JS 构建计算器

10 初学者JS计算器

2017-11-13 10:31:08 3 154   html