psyconaut - 1 year ago 70

Javascript Question

Create class/object in Javascript that would accept an array in the

form of

`[3, “Plus”, 1]`

`[6, “Times”, 2]`

properties

`“operand1”`

`“operator”`

`“operand2”`

prefer, and returns the result of the arithmetics.

Note that element

representing the operator is the name of a function or a reference to

that function – implement them as well (Plus, Minus, Times and

Divide).

Note that in the future there may be a need to support

another operations without changing the existing code or changing it

as little as possible.

`function plus(one, operator, two) {`

var total = one + two;

alert(total);

return total;

}

function minus(one, operator, two) {

var total = one - two;

return total;

}

function times(one, operator, two) {

var total = one * two;

return total;

}

function divide(one, operator, two) {

var total = one / two;

return total;

}

function solver(operand1, operator, operand2) {

//conditional to give user answer

if (operator.toLowerCase == "add") {

var total = operand1 + operand2;

alert("Your answer is " + total);

} else if (operator.toLowerCase == 'subtract') {

var total = operand1 - operand2;

alert("Your answer is " + total);

} else if (operator.toLowerCase == 'multiply') {

var total = operand1 * operand2;

alert("Your answer is " + total);

} else if (operator.toLowerCase == 'divide') {

var total = operand1 / operand2;

alert("Your answer is " + total);

} else {

alert("There was an error with the input");

}

}

numOne = prompt("Enter your first number");

numTwo = prompt("enter a second number");

oper = prompt("Would you like to divide, multiply, subtract or add");

//convert Input into an int

numOne = parseInt(numOne);

numTwo = parseInt(numTwo);

userIn = [numOne, oper, numTwo];

console.log(userIn);

solver(userIn[0], userIn[1], userIn[2]);

Any suggestions?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

The problem is that `toLowerCase`

is a function and so needs to be used as such.

So you need to change your operator lines:

```
else if(operator.toLowerCase == 'multiply')
```

to:

```
else if(operator.toLowerCase() == 'multiply')
```

Visit this page for more information: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**