[JavaScript] function question

  • Java
  • Thread starter mindauggas
  • Start date
  • #1
127
0

Main Question or Discussion Point

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

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

power();
It does not work, ca anyone tell why?
 

Answers and Replies

  • #2
33,079
4,783
write an if statement that checks if the parameter exponent is 0. If it is, return 1 (a base case).

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

power();
It does not work, ca anyone tell why?
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:
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.
 

Related Threads for: [JavaScript] function question

Replies
9
Views
790
  • Last Post
Replies
1
Views
1K
Replies
3
Views
1K
Replies
5
Views
824
Replies
13
Views
3K
Replies
3
Views
1K
  • Last Post
Replies
11
Views
2K
Top