Explain for loop for 10 year old kid 🎮




LET'S CODE TOGETHER

WHAT IS FOR LOOP 

Imagine going to the mall and picking up 9 different video games for your PS4.😍

Tom is coming and you want to show them to him. Yes, you chose them and you know all of them and for sure, there is an easy and messy way. Just throwing them out of the bag to the table and and here they are. But please No. 😅

What if you want to show him each one of them, one by one, and also explain him why you chose them?

There is an easy and tidy way.😉 We will write some simple lines of code that will do this work for you. 💪

First we need to make a list of your new games:

  • Rocket League

  • Sackboy: A Big Adventure

  • Overcooked! All You Can Eat

  • Hot Wheels Unleashed

  • LEGO Star Wars: The Skywalker Saga

  • Minecraft

  • Ratchet & Clank (2016)

  • Marvel’s Spider-Man (2018)

  • Fall Guys: Ultimate Knockout

 Now we need to write it in JavaScript so our code can run.

JavaScript code:

let games = [ "Rocket League" , "Sackboy: A Big Adventure", "Overcooked! All You Can Eat", "Hot Wheels Unleashed",  "LEGO Star Wars: The Skywalker Saga", "Minecraft", "Ratchet & Clank (2016)", "Marvel’s Spider-Man (2018)", "Fall Guys: Ultimate Knockout"];

And here comes the For Loop part, now that we have a list written syntactically correctly in JavaScript, we also need to create the loop.. 

JavaScript code:

For ( let i = 0; i < games.length; i++) {

    console.log(games[i]);
}

As you can see all the red parts are the musts for our code to work. You can make different loops with this red part, Just changing the black letters. The black letters are simple words, and you can choose what to write. But rememeber they need to be the same. As you can see we named our list games, the word games needs to be the same in the whole code, for the computer to understand what you are asking. The same also for the i, this letter keeps the position of your chosen game in the list, and every time will show you which one is in the i position starting from the 0, 1, 2, ...8 (for 9 games, because it starts from the 0 will run 8 times).

Cool, right?😎


Σχόλια

Δημοσίευση σχολίου