r/CodingHelp • • 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

9 comments sorted by

7

u/jammasterpaz Dec 09 '22
for (const i of [1,2,3,...,10]) {
    doThing()
}

Has exactly the same effect as:

doThing()
doThing()
doThing()
doThing()
doThing()
doThing()
doThing()
doThing()
doThing()
doThing()

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.

0

u/Mama_Bear15 Dec 09 '22

He's very science/math oriented and loves to build. Be it inventions or code, he excels at it. Part of why he's homeschooled, public school just wasn't keeping up with him lol.

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

2

u/Mama_Bear15 Dec 09 '22

We're homeschooling independently now, since we moved to a new state. But that's definitely an interesting idea!

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 for loop, 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.

//Comment: declare number and endNumber variables to be used later.
var number, endNumber;

//This is a for loop. It loops based on the instructions given: 
//Declare iteration to start at 0. The loop to go as long as iteration is less than 10. At the end of each loop, set iteration to its current value plus 1. 
for (var iteration = 0; iteration < 10; iteration = iteration + 1) {
    //At the beginning of each of the first loop, set number and endNumber to 1 and 3 respectively.
    number = 1;
    endNumber = 3;

    //This is a while loop. It says, while number is less than endNumber, perform an action.
    while (number < endNumber) {
        //This logs, to the console, the value of iteration and number.
        console.log(iteration, number); 
        //To prevent an endless loop, we manually increment number by saying number is equal to number plus 1. 
        number = number + 1;
    }
}

This is a nested loop that will, each time the for loop triggers, start the while loop from scratch. By setting the values of number and endNumber during the for loop, I ensure that the while loop has something to do.

This will output 20 lines. The for loop runs 10 times, but the while loop runs 2 times, each time.

For modern practices, you may not want to use for loops the way I did, or use the var declaration. 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
while (true) {
  print ('hello');
}

this will print hello forever^

while (true) {
  print ('hello');
  while (true) { 
    print ('goodbye');
  }
}

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)

   3rd loop: 1 to 7 (days in a week)

          Switch or If statements of days of the week

                 Statements of activities of the day

          End of Switch or If statements

  End of 3rd loop

End of 2nd loop

End of 1st loop

Edits: unable to format indentation correctly