Question

Section 3: Create a Calculator For this problem, please create a calculator function that takes two integers and an operato

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

function calculate(number1,operator,number2){
if (!number1 || !number2){
return "Invalid number"
}
if (!['*','-','/','+','^'].includes(operator)){
return "Invalid operator"
}
switch(operator){
case "+":
return number1 + number2;
case "/":
return number1 / number2;
case "*":
return number1 * number2;
case "-":
return number1 - number2;
case "^":
return number1 ** number2;
}
}

To call the function:

calculate(1,"+",4)
calculate(7,"-",2)
calculate(5,"*",1)
calculate(20,"+",5)
calculate(3,"+",3)

Output:

> calculate(1,+,4) < 5 calculate(7,-,2) < 5 calculate(5,*,1) <-5 calculate(3,A, 3) <- 27

Please give thumbsup...

Add a comment
Know the answer?
Add Answer to:
Section 3: Create a Calculator For this problem, please create a "calculator" function that takes two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Datapath To make the calculator work, you have to create datapath components for the Datapath subcircuit:...

    Datapath To make the calculator work, you have to create datapath components for the Datapath subcircuit: Create an ALU that can calculate the result for the basic arithmetic operations (addition, subtraction, multiplication, division) for two 8-bit values. The ALU should output the needed result depending on the selected operation. Use 3 registers to store the first number, selected operator and second number. Remember that registers store the values on the input during a rising edge in the clock input (default...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • Calculator = 0.01 Calculate . Add O subtract O Multiply ODvide uttons on the right determine the operation that is perf...

    Calculator = 0.01 Calculate . Add O subtract O Multiply ODvide uttons on the right determine the operation that is performed on the numbers. For example, the following image shows values 12 and 3 are entered in the ion in selected. When the Calculate button is pressed, the result of the addition operation is shown after the equal sign ( In this example, the result is 1 Calculator 12 .15.01 Calculate | ⓔAdd subtract。Multiply O Divide g images show the...

  • Write an ARM program that implements a simple four-function calculator and prompts the user to enter...

    Write an ARM program that implements a simple four-function calculator and prompts the user to enter a pair of decimal integers (A and B) followed by a character that specifies one of the operators: ‘+’ for addition to compute A+B                             ‘-‘ for subtraction to compute A-B ‘*’ for multiplication to produce the product A*B ‘/’ for division to produce the quotient A/B. Input should be the pair of numbers followed by the operand followed by a return. For example...

  • PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which...

    PLease IN C not in C++ or JAVA, Lab3. Write a computer program in C which will simulate a calculator. Your calculator needs to support the five basic operations (addition, subtraction, multiplication, division and modulus) plus primality testing (natural number is prime if it has no non-trivial divisors). : Rewrite your lab 3 calculator program using functions. Each mathematical operation in the menu should be represented by a separate function. In addition to all operations your calculator had to support...

  • Problem 3 (35) Design a calculator that performs four operations: addition, multiplication, division and subtraction with...

    Problem 3 (35) Design a calculator that performs four operations: addition, multiplication, division and subtraction with 2 integer type numbers a) Ask user what type of operation to perform (+, , * or/) a. If the user inputs 'none' then the program terminates. Otherwise it will keep continuing the calculation. (hint: use while) b) Ask user to provide 2 integer number inputs c) Perform operation (whatever operation they mentioned in a) d) Print result e) In division operation, perform a...

  • CC++ Compiler: CPP (gcc-5.x) 0 Simple Calculator Given two integers and a string, create a simple...

    CC++ Compiler: CPP (gcc-5.x) 0 Simple Calculator Given two integers and a string, create a simple calculator which performs the following operations: • Addition if the string is "+" • Difference if the string is "-" Multiplication if the string is "*" • Return quotient (obtained by dividing the first number by the second) if the string is "/" • Return greater value if the string is "max" • Return lesser value if the string is "min" solution.cpp 2 Create...

  • Write a Python Program that displays repeatedly a menu as shown in the sample run below....

    Write a Python Program that displays repeatedly a menu as shown in the sample run below. The user will enter 1, 2, 3, 4 or 5 for choosing addition, substation, multiplication, division or exit respectively. Then the user will be asked to enter the two numbers and the result for the operation selected will be displayed. After an operation is finished and output is displayed the menu is redisplayed, you may choose another operation or enter 5 to exit the...

  • C++ ONLY Write a function calculator that takes two floating numbers and one operator and prints...

    C++ ONLY Write a function calculator that takes two floating numbers and one operator and prints out the answer based on arithmetic. Assume that there are no overflow, underflow and division by zero cases. Your function should be named calculator Your function takes three input parameter: two double numbers and one char operator Your function does not return anything Your function prints answer in the format specified below Your function should set precision point to 2 Note: You must use...

  • Implement a simple four function calculator in the LC3 assembly language that will run on the...

    Implement a simple four function calculator in the LC3 assembly language that will run on the LC-3 simulator. The LC-3 computer only reads a character at a time but is capable of displaying a string of characters. This calculator program will use multiple subroutines that perform purposes such as reading multi-digit numbers, converting them to integers, performing addition, subtraction, multiplication, or division on these numbers, and displaying the result. The inputs to the calculator will not exceed 4 digits. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT