Course: JavaScript

Progress (0%)

  1. Foundation

  2. Numbers

  3. Strings

  4. Conditions

  5. Loops

  6. Arrays

  7. Functions

  8. Objects

  9. Exceptions

  10. HTML DOM

  11. CSSOM

  12. Events

  13. Drag and Drop

  14. opt Touch Events

  15. Misc

  16. Project: Analog Clock

Exercise: Rudimentary Calculator

Exercise 6 Easy

Prerequisites for the exercise

  1. JavaScript Control Flow
  2. All previous chapters

Objective

Create a program that allows the user to choose from a given range of arithmetic operations, and then performs that operation on two input numbers.

Description

Back in the Addition Calculator Exercise, we created a program to add two input numbers.

Now you need to extend that program and perform any given operation on the two input numbers, other than just addition. The desired operation is itself specified as an input value.

As before, we begin with the two input prompts displaying the messages 'x:' and 'y:', respectively and then obtain the number out of each of the entered values.

The third prompt should ask for a letter that would specify the operation desired on the two input numbers, using the message 'Operation:'.

The entered letter must be one of the following:

  1. a for addition.
  2. s for subtraction.
  3. m for multiplication.
  4. d for division.
  5. e for exponentiation.
  6. r for remainder.

Finally, when all the inputs are received, the program should make the following output if the letter denoting the operation is one of the aforementioned letters:

<x> <operation_symbol> <y> = <result>

Here <x> and <y> are the first and second input numbers, <operation_symbol> is the symbol denoting the selected operation in JavaScript, and <result> is the result of the operation (as performed on the two input numbers).

For instance, if the value letter for the operation is s, then <operation_symbol> should be -. Similarly, if the letter is e, <operation_symbol> should be **, and so on.

However, if the letter for the operation is not one of those mentioned above, then the program should simply output the following:

Unknown operation

Here's how the program should work:

Live Example

View Solution

New file

Inside the directory you created for this course on JavaScript, create a new folder called Exercise-6-Rudimentary-Calculator and put the .html solution files for this exercise within it.

Solution

We start off by laying out three different calls to prompt() for the three inputs — the first two for the numbers x and y, while the third for the arithmetic operation.

var x = Number(prompt('x:'));
var y = Number(prompt('y:'));
var op = prompt('Operation:');

Next up, we check which letter did the user enter in the last input prompt, i.e. what is the value of op identical to, in order to decide what operation to perform. This is done using the switch conditional statement:

var x = Number(prompt('x:'));
var y = Number(prompt('y:'));
var op = prompt('Operation:');

switch (op) {
   case 'a':
      document.write(x + ' + ' + y + ' = ' + (x + y));
      break;
   case 's':
      document.write(x + ' - ' + y + ' = ' + (x - y));
      break;
   case 'm':
      document.write(x + ' * ' + y + ' = ' + (x * y));
      break;
   case 'd':
      document.write(x + ' / ' + y + ' = ' + (x / y));
      break;
   case 'e':
      document.write(x + ' ** ' + y + ' = ' + (x ** y));
      break;
   case 'r':
      document.write(x + ' % ' + y + ' = ' + (x % y));
      break;
   default:
      document.write('Unknown operation');
}

And this completes our exercise.

Live Example