write a JS program to create a basic calculator using a switch and break statements

  break, calculator, javascript, switch-statement

Hello i am pasting the below mentioned task bewlo can any body help me solve this question with step by step explaination please read the question below:-

The program asks the user to enter 2 numbers and then ask the user for the choice of the operator: 1 to add, 2 to minus, 3 to multiply and 4 to divide the 2 numbers. Your output code should return the resulting variable.
Take input variables (number1, number2, choice) as the parameter of your function.
Steps:

Declare a variable to store the result
Use the switch and specify different cases with execution code and break
Write default case
Create an output variable and store the result in it
Return the output variable

Codes I have used till now but not working at all:
function calculator(n1,n21,ops) {

 switch (ops) {
     case "+":
         var result = n1+n21;
         console.log(result);

        break;
     case "-":
         var result = n1-n21;
         console.log(result);

        break;
     case "*":
         var result = n1*n21;
         console.log(result);

        break;
     case "/":
         var result = n1/n21;
         console.log(result);

        break;
 
     default:
         var result = "Invalid Input";
         console.log(result);
         break;
 }

}

console.log(calculator(4,5,[‘+’,’-‘,’*’,’/’]));

Source: Ask Javascript Questions

LEAVE A COMMENT