[JavaScript] function question

In summary, the power function has two parameters and returns a value. When calling the function, make sure to include the correct number of parameters and to store or use the return value. You can use the == operator for comparing numbers in this case.
  • #1
mindauggas
127
0
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?
 
Technology news on Phys.org
  • #2
mindauggas said:
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.
 

1. What is a function in JavaScript?

A function in JavaScript is a block of code that performs a specific task or calculation. It can be called or invoked multiple times throughout a program, making it a reusable piece of code. Functions are an important concept in JavaScript as they help organize code and make it more efficient.

2. How do you declare a function in JavaScript?

A function in JavaScript can be declared using the keyword "function" followed by the name of the function and a set of parentheses. This is then followed by curly braces which contain the code to be executed when the function is called. For example: function myFunction() { // code to be executed }

3. What is the difference between parameters and arguments in a function?

Parameters are used in the function declaration as placeholders for values that will be passed into the function when it is called. Arguments, on the other hand, are the actual values that are passed into the function when it is called. Parameters are defined in the function declaration, while arguments are passed in when the function is invoked.

4. Can a function in JavaScript return a value?

Yes, a function in JavaScript can return a value. This is done by using the keyword "return" followed by the value or expression that is to be returned. Functions can also be written without a return statement, in which case they will return undefined by default.

5. How do you call a function in JavaScript?

To call or invoke a function in JavaScript, you simply use the function name followed by parentheses. If the function has parameters, you would pass in the arguments inside the parentheses. For example: myFunction(5, "Hello") would call the function "myFunction" with the arguments 5 and "Hello".

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
9
Views
991
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
983
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
4
Views
735
  • Programming and Computer Science
Replies
3
Views
307
  • Programming and Computer Science
Replies
12
Views
2K
Back
Top