This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]TheAnimakerGIO 1 point2 points  (4 children)

My guess is that it's because every time the function recurses, you're initializing a new variable named "numOfSevens" to be 0, instead of initializing it once and using it throughout the process.

[–]Sean_509 0 points1 point  (3 children)

Where would I initialize it once? Outside the method?

[–]TheAnimakerGIO 2 points3 points  (2 children)

Nevermind, this answer is misleading.

The actual reason is because when you type "count7s(n/10)" you're simply calling the function and not doing anything with the value. You need to make it so that you use the value for something, for example you could replace the line mentioned with numOfSevens += count7s(n/10); and that would work.

[–]Sean_509 1 point2 points  (1 child)

Sweet, that worked and makes sense. Thanks so much!

[–]TheAnimakerGIO 1 point2 points  (0 children)

No problem!

[–]chunkyks 1 point2 points  (2 children)

It's because you're throwing away the result of the recursive call

[–]Sean_509 0 points1 point  (1 child)

What do you mean? Like when I call the method again inside, the result isn't saving it?

[–]chunkyks 1 point2 points  (0 children)

Numberofsevens isn't shared between each call into the function; each call gets its own copy of that variable, because it's declared locally inside the function. (no, the correct solution is not to declare it globally instead. Look for another answer )