5

I'm new to Objective javascript even I had a good amount of experience in Javascript. How do I pass my parameters to the calc closure here?

var calc = (function() {
    var a = 5;
    var b = 0;
    return {
        add: function() {
            return a + b;
        },
        subtract: function() {
            return a - b;
        },
        multiply: function() {
            return a * b;
        },
        divide: function() {
            if (b != 0) return a / b
            else {
                alert('division by zero');
                return false;
            }
        }
    }
})();​

    console.log(calc.divide());

I want to pass the parameters to calc like (calc.multiply(10,20));

Thanks in advance..

2
  • 1
    Can I just say: 'indentation is a wonderful thing'? Commented Sep 6, 2012 at 20:19
  • I believe the standard is to return NaN in cases of division by zero. But, if you are intending the explicit validation, I suppose the alert is fine. Commented Sep 6, 2012 at 20:27

5 Answers 5

2

According to your call method calc.multiply(a, b): There should be like this:

var calc = (function() {
    return {
        add: function(a, b) {
            return a + b;
        },
        subtract: function(a, b) {
            return a - b;
        },
        multiply: function(a, b) {
            return a * b;
        },
        divide: function(a, b) {
            if (b != 0) return a / b
            else {
                alert('division by zero');
                return false;
            }
        }
    }
})();

console.log(calc.divide(20, 2));
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a need for the IIFE? I'd say no.
0

Do you mean like this:

var calc = (function() {
    var a = 5;
    var b = 0;
    return {
        add: function() {
            return a + b;
        },
        subtract: function() {
            return a - b;
        },
        multiply: function() {
            return a * b;
        },
        divide: function(n, d) {
            if (n != 0) return n / d
            else {
                alert('division by zero');
                return false;
            }
        }
    }
})();

alert(calc.divide(3, 4));​

Comments

0
var calc = (function() {
    return {
        add: function(a, b) {
            return a + b;
        },
        subtract: function(a, b) {
            return a - b;
        },
        multiply: function(a, b) {
            return a * b;
        },
        divide: function(a, b) {
            if (b != 0) return a / b
            else {
                alert('division by zero');
                return false;
            }
        }
    }
})();​

Though I'd consider changing your alert to throwing an error.

Comments

0

You either can give the parameters to calc, which returns an object to compute something with the two numbers, or you just make calc an object with functions that compute something. Your requirement calc.multiply(10,20) is for the second solution:

var calc = {
    add: function(a, b) {
        return a + b;
    },
    subtract: function(a, b) {
        return a - b;
    },
    multiply: function(a, b) {
        return a * b;
    },
    divide: function(a, b) {
        if (b != 0) return a / b
        alert('division by zero');
        return false;
    }
};​

Otherwise, for using calc(10,20).multiply(), it would be building the closure you already have, but with parameters instead of the two local constants:

function calc(a, b) {
    return {
        add: function() {
            return a + b;
        },
        subtract: function() {
            return a - b;
        },
        multiply: function() {
            return a * b;
        },
        divide: function() {
            if (b != 0) return a / b
            alert('division by zero');
            return false;
        }
    };
}

Comments

0

The code below should work for you:

var calc = (function() {
    var a = 5;
    var b = 0;

    return {
        add: function(a, b) {
            return a + b;
        },
        subtract: function(a, b) {
            return a - b;
        },
        multiply: function(a, b) {
            return a * b;
        },
        divide: function(a, b) {
            if (b !== 0) return a / b;
            else {
                alert('division by zero');
                return false;
            }
        }
    };
})();

console.log(calc.multiply(10, 10));

Edits I made were setting a and b parameters on each returned function, and making your comparison to zero safer on the divide function by making it strict.

Hope this helps.

1 Comment

Calc isn't a Javascript constructor -- it uses the module pattern instead. No need for the uppercase function name or the new keyword. It would work just 'as is' as a singleton.

Your Answer

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