Keep many numbers in an array, draw one thing for each with a loop, and build the array with random numbers.
Ten bars, or a thousand: it is the same few lines of code if the numbers live in a list and a loop does the drawing.
An array is a list of values in order: let heights = [50, 120, 80, 150];. Each value has a place number, counting from 0: heights[0] is 50 and heights[3] is 150. heights.length is how many there are (4).
A loop repeats code. This one counts:
for (let i = 0; i < heights.length; i++) {
// runs once for each place number i
}
The hard part is that first line. Read it as three small instructions in a row: start with i at 0; keep going while i is less than the length; add 1 to i after each time round. You will copy it a lot before it feels natural, and that is normal. Inside the braces, i is the place number, so heights[i] is each value in turn.
heights.push(7) adds a value to the end, and random(20, 180) gives a random number from 20 up to 180.
Make your prediction first. Help opens once you've locked it in.
let heights = [50, 120, 80, 150];
function setup() {
createCanvas(300, 200);
background(240);
for (let i = 0; i < heights.length; i++) {
rect(i * 40 + 20, 200 - heights[i], 30, heights[i]);
}
}
rect(x, y, width, height) puts (x, y) at the bar's top left, and y counts down. Where is the bottom of the first bar?
The loop in setup() builds the array: ten random heights. The loop in draw() draws a bar for each one. Press Run again a few times. Change the 10 in i < 10 and run again.
let heights = [];
function setup() {
createCanvas(300, 200);
for (let i = 0; i < 10; i++) {
heights.push(random(20, 180));
}
}
function draw() {
background(20, 30, 60);
fill(255, 210, 0);
for (let i = 0; i < heights.length; i++) {
rect(i * 30 + 5, 200 - heights[i], 20, heights[i]);
}
}
let heights = [];
function setup() {
createCanvas(300, 200);
for (let i = 0; i < 10; i++) {
heights.push(random(20, 180));
}
}
function draw() {
background(20, 30, 60);
fill(255, 210, 0);
for (let i = 0; i < heights.length; i++) {
rect(i * 30 + 5, 200 - heights[i], 20, heights[i]);
}
}
What the loop does each time round.
Counting from 0.
Bars hanging the wrong way.
heights is [50, 120, 80, 150]. Fill in what the loop uses each time round.
| i | heights[i] | the bar's top, 200 - heights[i] |
|---|---|---|
| 0 | 50 | 150 |
| 1 | ||
| 2 | ||
| 3 |
The bars hang from the top of the canvas instead of standing on the bottom. Click the line that is wrong.
Draw 20 bars instead of 10, so they all still fit on the canvas.
Two things control how many bars there are. One of them makes the values.
Change 10 to 20 in setup(), and make the bars thinner and closer together so they fit.

Colour the tall bars (over 100) differently from the short ones.
Inside the loop, an if can look at heights[i] before the rect is drawn.
if (heights[i] > 100) { fill(255, 120, 160); } else { fill(255, 210, 0); } goes above the rect line.

Colour the tall bars (over 100) differently from the short ones.
Run your program. This ticks when your code does all of the above.
Draw a chart of your own from a list of numbers: hours of sleep each night this week, scores, or random bubbles. Fill the array with the numbers you choose (let values = [3, 5, 2];) or with push, then use a loop to draw one thing for each value. Circles, bars, lines and coloured squares all work.
Use a loop to draw one thing for each value in your list.
Run your program. This ticks when your code does all of the above.
An array holds many values, and a loop does something for each. Next: an object holds everything about one thing, so a list of objects is a list of stars, balls or bars.
Students keep numbers in an array, count places from 0, draw a bar for each with a for loop, build the array with push and random, find why bars hang from the top, and make a chart of their own. About 55 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Read the unit primer first. This revisits the Python lists lesson in JavaScript: places count from 0, length says how many, and push is append. The new difficulty is the counting loop for (let i = 0; i < heights.length; i++): three parts in one line, which students copy until it makes sense; the lesson's text reads it aloud, and the trace table follows i and heights[i] together. The second idea is that rect hangs from its top-left, and y counts down, so a bar that stands on the bottom has its top at 200 - height: that is the Predict and the bug task. Random numbers make every Run different, so the Predict uses a fixed array.
The three-part for line. Have students say it aloud: start at 0, keep going while less than the length, add 1 each time.
Stretch: Add a second array of names, and draw each name under its bar with text(). Or sort the data first with heights.sort().
Support: Give students the loop and the rect line, and ask them only to change the numbers and colours.
Next lesson: objects, so one list holds stars with their own position, speed and size.