As a wee little bit of background, I’ve been following along with the on-going Javascript Basics Workshop/shindig, including yesterdays recording on 11/28 where we did a recap of Exercises 11 through 15. Folks in the live chat from the session might have witnessed my “lightbulb” moment regarding callback functions related to exercise 11.
That said, I’m playing a wee bit of catch up today, and hadn’t quite done Exercise 15. I was reading it over and following along, and part of the challenge bit stuck out at me.
You should try to solve the puzzle, but be warned that I mean for you to edit this file multiple times to get each console.log to run. It’s not possible to set x and y in a way that would cause all branches of those if-statements to run.
Oh? I don’t think that last sentence was a challenge, I’m pretty sure it was informative. But what if I modified the code a little bit here. What if I was really lazy and didn’t want to set x and y to some value to run all the “log” prints?
Exercise 15 Spoilers?
const console_print = (x, y) => {
if (y == 100) {
if (x < 10) {
console.log("log 1", x, y);
x = 11;
console_print(x, y);
} else {
console.log("log 2", x, y);
y = 1;
console_print(x, y);
}
} else {
if (x > 10) {
console.log("log 3", x, y);
x = 9;
console_print(x, y);
} else {
console.log("log 4", x, y);
}
}
}
console_print(9, 100);
I know that’s such a tiny little thing, but it tickled me enough to post it here.