As I embark on my journey to teach myself JS with the help of a less-than-stellar textbook, I find myself stumped by a challenge that requires deciphering the purpose of this code:
function clunk (times) {
var num = times;
while (num > 0) {
display("clunk");
num = num - 1;
}
}
function thingamajig (size) {
var facky = 1;
clunkCounter = 0;
if (size == 0) {
display("clank");
} else if (size == 1) {
display("thunk");
} else {
while (size > 1) {
facky = facky * size;
size = size - 1;
}
clunk (facky);
}
}
function display(output) {
console.log(output);
clunkCounter = clunkCounter + 1;
}
var clunkCounter = 0;
thingamajig(5);
console.log(clunkCounter);
I'm struggling to comprehend the logic behind the supposed answer provided in the book, claiming that "clunk" will be displayed in the console 120 times. They attribute it to factorials, but I can't pinpoint where the repetition is coming from. Despite numerous attempts at navigating through it mentally and jotting down notes, all I get is a single instance of "clunk" printed in the console. Could someone guide me through this conundrum or shed light on the elusive factorial operation? Your help would be greatly appreciated!