Java Why Doesn't the JavaScript Power Function Work?

AI Thread Summary
The discussion focuses on correcting a JavaScript function designed to check if the exponent parameter is zero and return 1 as a base case. The initial code fails because the function is called without parameters, which is necessary for it to execute properly. It is emphasized that the function should be called with both parameters, such as power(0, 10), to store the return value correctly. Additionally, there is a suggestion that using the == operator instead of === for the comparison may suffice since the comparison involves numbers rather than objects.
mindauggas
Messages
127
Reaction score
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
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
2
Views
1K
Replies
9
Views
1K
Replies
12
Views
4K
Replies
12
Views
2K
Replies
11
Views
2K
Replies
11
Views
2K
Replies
4
Views
1K
Replies
9
Views
2K
Back
Top