Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

JavaScript [JavaScript] function question

  1. Sep 27, 2012 #1
    write an if statement that checks if the parameter exponent is 0. If it is, return 1 (a base case).

    Code (Text):
    var power = function(exponent, base){
        if (exponent === 0){
            return 1;

    It does not work, ca anyone tell why?
  2. jcsd
  3. Sep 27, 2012 #2


    Staff: Mentor

    You are not calling your function correctly.
    1. The power function has two parameters. You are calling it with no parameters.
    2. The power function returns a value, so you need to store or otherwise use the return value.
    Code (Text):

    var retValue = power(0, 10);
    After the code above runs, retValue should be set to 1.

    Also, you don't need to use === in your comparison, since you're just comparing numbers, not objects. The == operator should work just fine.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook