Does anybody understand the thingamajig code?
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
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.
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
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.
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