Loading, please wait...

Simple Calculator with switch in JavaScript | JavaScript

Apr 13, 2021 JavaScript, 283 Views
Simple Calculator with switch in JavaScript | JavaScript

Simple Calculator with switch in JavaScript

let result;
const operator = prompt('Enter operator ( either +, -, * or / ): ');
const number1 = parseFloat(prompt('Enter the first number '));
const number2 = parseFloat(prompt('Enter the second number: '));
switch(operator) {
    case '+':
         result = number1 + number2;
        console.log(`${number1} + ${number2} = ${result}`);
        break;

    case '-':
         result = number1 - number2;
        console.log(`${number1} - ${number2} = ${result}`);
        break;

    case '*':
         result = number1 * number2;
        console.log(`${number1} * ${number2} = ${result}`);
        break;

    case '/':
         result = number1 / number2;
        console.log(`${number1} / ${number2} = ${result}`);
        break;

    default:
        console.log('Invalid operator found');
        break;
}