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.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 15 min · Modify 15 min · Make 15 min
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.
| What you will see | A question to ask | The fix |
|---|---|---|
| Nothing is drawn. | Is the loop inside draw(), and is the array filled in setup()? | The array must have values before draw() uses them: push in setup(), loop in draw(). |
| A red line: Cannot read properties of undefined. | Does the loop go past the end of the array? | Use i < heights.length, not <=. The last place is length - 1. |
| The bars hang from the top. | Which way does y count, and where is the top of a bar? | The top is at 200 - heights[i]; the height stays heights[i]. |
| Only one bar shows. | Are all the bars at the same x? | Make x depend on i, for example i * 30 + 5. |
One possible solution for each task. Other correct programs are fine.
let heights = [];
function setup() {
createCanvas(300, 200);
for (let i = 0; i < 20; 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 * 15 + 2, 200 - heights[i], 10, 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);
for (let i = 0; i < heights.length; i++) {
if (heights[i] > 100) {
fill(255, 120, 160);
} else {
fill(255, 210, 0);
}
rect(i * 30 + 5, 200 - heights[i], 20, heights[i]);
}
}
let sleep = [7, 6, 8, 5, 9, 10, 8];
function setup() {
createCanvas(300, 200);
}
function draw() {
background(20, 30, 60);
fill(120, 220, 255);
for (let i = 0; i < sleep.length; i++) {
circle(30 + i * 40, 190 - sleep[i] * 15, sleep[i] * 5);
}
fill(255);
text("Hours of sleep, Monday to Sunday", 10, 20);
}
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.
In heights[i], what is i, and where does it start?
i is the place number of the value in the array, and the first place is 0.
Next lesson: objects, so one list holds stars with their own position, speed and size.