Table of Contents
show
Example 23
Write a JavaScript to perform calculator functions using switch case
const prompt = require("prompt-sync")();
let operator = prompt("Enter the operator (+, -, *, /) :");
let n1 = prompt("Enter the number : ");
n1 = parseInt(n1);
let n2 = prompt("Enter the number : ");
n2 = parseInt(n2);
let res;
switch (operator) {
case "+":
res = n1 + n2;
console.log(`${n1} ${operator} ${n2} = ${res}`);
break;
case "-":
res = n1 - n2;
console.log(`${n1} ${operator} ${n2} = ${res}`);
break;
case "*":
res = n1 * n2;
console.log(`${n1} ${operator} ${n2} = ${res}`);
break;
case "/":
res = n1 / n2;
console.log(`${n1} ${operator} ${n2} = ${res}`);
break;
default:
console.log("Operator is not specified");
break;
}
Output
Views: 132