Learn
Loops
Putting it All Together

Let’s combine all that we’ve learned so far:

  • variables, operators, and data types
  • functions and control flow
  • lists and loops

Examples of for and while loops are provided below in JavaScript. Copy each one to the editor and run it! You’ll see that they do the same things.

In this for loop, the instructions are run 10 times.

for (i = 0; i < 10; i++) { placeTile('mint') placeTile('orange') placeTile('mint') }

In this while loop, we create a counter at 0 and add 1 to it every iteration. The loop is executed until the counter is 10 or more.

let i = 0; while (i < 10) { placeTile('mint') placeTile('orange') placeTile('mint') i++ }

Instructions

  1. Copy the for loop to the text editor. Run it.
  2. Delete the for loop and copy the while loop to the text editor. Run it.
  3. Edit the condition in the while loop so that the loop is executed twice (only 6 tiles should be placed).

Well done. If you’re feeling creative, change the colors! You can replace the strings with any other color you’ve seen in this lesson, like 'purple'.

Folder Icon

Sign up to start coding

Already have an account?