0

I am writing modular javascript code. I wrote a basic calculator with two inputs and four buttons which performs arithmetic operations. When I run my it shows this error in console.

Uncaught ReferenceError: add is not defined

This happens for all buttons. How can I fix this and why it does not work?

Here is my code

1 Answer 1

3

You are mixing up variables and object properties. Javascript never implicitly looks up object properties - it's always a variable lookup with the exception of global object and with-statement.

So specify the object:

    calculation: function(operator) {
        if(operator == 'add')
            return this.add(valone, valtwo);
        else if(operator == 'sub')
            return this.sub(valone, valtwo);
        else if(operator == 'mult')
            return this.mult(valone, valtwo);
        else
            return this.div(valone, valtwo);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.