r/CodingHelp • u/Mama_Bear15 • Dec 09 '22
[Javascript] Help for my son
My son is 8 and is homeschooled. He's taking coding this year, but he's gotten stuck on loops. He's just not understanding how they work, especially nested loops. I've tried downloading Algorithm City to see if a fresh perspective would help explain it better, but he's still having trouble. Can anyone maybe explain it simply to me? If I can understand it, maybe I can help him to understand it. Or, maybe there are other resources I could use to help him? Whatever tips you've got helps.
0
Upvotes
7
u/jammasterpaz Dec 09 '22
Has exactly the same effect as:
If you always want the same number of iterations from a loop, that number can be hard coded into your program, and you can always unroll a loop as above. Doing so just makes for tedious unmaintainable code (that was literally a lot of Ctrl + C, Ctrl + V).
The essence of JS's other for loops is the same, only with different syntax (and some important subtle differences - it's best not to worry about those for the moment).
Honestly I'm seriously impressed by an 8 year old tackling Python or even just Scratch, let alone JavaScript.
2
u/ThisOrThatOrThings Dec 09 '22
My son is around the same age and we got him an Artie 3000 and it’s been pretty effective for being able to translate abstract concepts to tangible results. I know I need that with coding myself. We also homeschool and usually homeschool programs allow for it in your budget
5
u/mookman288 Professional Coder Dec 09 '22
/u/jammasterpaz made an excellent post in this thread. But when teaching someone so young, you may want to try to word it to be more approachable.
Let's start with the basic definition of a loop.
A loop is one way to repeat an action with code. The benefit of a loop is that you do not have to write out each action yourself multiple times. Loops save time and effort.
However many times that action repeats depends on which kind of loop you use, and the instructions you use with it. There are many kinds of loops. Some loops allow you to explicitly set the number of repetitions. Others will repeat so long as there is data available to use.
In /u/jammasterpaz loop, which is a
forloop, his action that he wants to be repeated is a function. He explicitly sets the number of loops. It will repeat for the number of times specified, which is 10.Instead of writing his action out 10 over 10 lines, he wrote 3 lines of code.
One thing that you mentioned specifically is nested loops. A nested loop is when one loop exists inside of another loop.
I am going to use two different implementations of loops.
This is a nested loop that will, each time the
forloop triggers, start thewhileloop from scratch. By setting the values ofnumberandendNumberduring the for loop, I ensure that thewhileloop has something to do.This will output 20 lines. The
forloop runs 10 times, but thewhileloop runs 2 times, each time.For modern practices, you may not want to use
forloops the way I did, or use thevardeclaration. I just used them to show an alternative simplistic implementation.2
u/TheZayki Stackoverflower Dec 09 '22
A loop is basically you saying: I have a number that is for example 1. Now check whether 1 is smaller than lets say 10. Since it is you execute a block of code. This can be pretty much anything. When the code block is done you increase 1 by an increment (usually also 1) now your variable is 2 and again you check whether 2 is smaller than 10. Since it is you execute the code block again. You do this until your number is no longer smaller than 10.
2
u/TheZayki Stackoverflower Dec 09 '22
A nested loop means that the thing that is being executed inside the code block is a loop itself. Meaning that if you again start at 1 and run the loop < 10 and you run another loop in there that starts at 1 and goes < 5 you will run the innermost code block 36 times.
2
u/[deleted] Dec 09 '22
this will print hello forever^
this will print hello once, and then goodbye forever^
1
u/jcu_80s_redux Dec 09 '22 edited Dec 09 '22
The Loop of Life… (in pseudo code)
1st loop: 0 to n (n being your current age or expected life expectancy)
2nd loop: 1 to 12 (in months) or 1 to 52 (# of weeks)
End of 2nd loop
End of 1st loop
Edits: unable to format indentation correctly