/calculator

Primary LanguageJavaScript

Calculator Challenge

A simple calculator exercise

Your Task 😃:

Create a function called calculator that takes two numbers num1 and num2 and a string operator as parameters. The function should use an if statement to determine which arithmetic operation to perform based on the value of operator. If operator is "+", the function should add num1 and num2 together and log the result. If operator is "-", the function should subtract num2 from num1 and log the result. If operator is "*", the function should multiply num1 and num2 together and log the result. If operator is "/", the function should divide num1 by num2 and log the result.

Example

calculator(5, 3, "+"); // Output: 8
calculator(5, 3, "-"); // Output: 2
ccalculator(5, 3, "*"); // Output: 15
calculator(5, 3, "/"); // Output: 1.6666666666666667
calculator(5, 3, "%"); // Output: Invalid operator

Good Luck 😀