Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does anybody understand the thingamajig code? #25

Open
meelaik opened this issue Apr 28, 2020 · 4 comments
Open

Does anybody understand the thingamajig code? #25

meelaik opened this issue Apr 28, 2020 · 4 comments

Comments

@meelaik
Copy link

meelaik commented Apr 28, 2020

Hi all,

I just started with JavaScript by reading this book.

So far so good :) But I stuck on the thingamajig code.

I cannot exactly understand how the code is executed and why the console says 120, if we call the thingamajig function with 5.

Thanks a lot in advance

Cheers
Mee

@bethrobson
Copy link
Owner

Hi there.
Did you try writing down the value of all the variables for each function call?
That's the easiest way to figure it out. Also remember clunkCounter is a global variable so all the references to clunkCounter in the functions are modifying that one global variable.

E.g. for size = 0:
facky = 1
clunkCounter = 0
call display() = clunkCounter = 1
We don't call clunk().
print clunkCounter, 1

For size = 1:
facky = 1
clunkCounter = 0
call display() = clunkCounter = 1
We don't call clunk()
print clunkCounter, 1

For size = 2:
facky = 1
clunkCounter = 0
facky = 1 * 2 = 2
size = 1
call clunk(2)
call display twice (because times = 2)
clunkCounter = 2
print clunkCounter, 2

And continue. Now you do this for size = 5.

@meelaik
Copy link
Author

meelaik commented Apr 29, 2020

Hi Elisabeth,
thanks so much for answering :)

You have helped me a lot, but I still do not understand why we get 6 clunks when calling thingamajig with argument 3.

What's the main difference between the argument 2 and 3? (I think an explanation here would solve my problem... ;))

Thank you very much

@hrkoren
Copy link

hrkoren commented Feb 10, 2021

I agree! Sizes up through 2, make sense. After that is where I am getting lost - if it’s a while loop, I still can’t get it to work. Driving me nuts trying to figure out how this works.

@bethrobson
Copy link
Owner

For size = 3:
facky = 1
clunkCounter = 0
while loop:
facky = 3
size = 2
facky = 6
size = 1
end while loop
call clunk(6)
num = 6
while loop
display clunk 6 times
increase clunkCounter 6 times to 6
end function calls
display clunkCounter, which is 6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants