## 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.

## Difficulty

## 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:

`a`

for addition.`s`

for subtraction.`m`

for multiplication.`d`

for division.`e`

for exponentiation.`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

*are the first and second input numbers,*

`<y>`

*is the symbol denoting the selected operation in JavaScript, and*

`<operation_symbol>`

*is the result of the operation (as performed on the two input numbers).*

`<result>`

For instance, if the value letter for the operation is `s`

, then * <operation_symbol>* should be

`-`

. Similarly, if the letter is `e`

, *should be*

`<operation_symbol>`

`**`

, 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:

Here's how the program should work:

## 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.