Nested Loops Practice Quiz¶
Note that the same code fragments are used in each question below. You might find it useful to sketch out on paper what you think each code fragment will create, then match your answers with the images provided in the questions below. You can assume that for each code fragment, a 200 by 200 canvas has been created, the background has been filled in as white, and the stroke is black.
Question 1¶
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(width/2, height/2, i, j); } }
-
Yes! Nicely done!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { rect(i, j, 10, 10); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(i+20, 0, i, j); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { rect(i, j, 5, i); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(width, height, i, j); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(i, 10, 5, j); } }
-
Try again!
nestedloops-quiz1: Which of the following code fragments would generate the image below?
Question 2¶
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(width/2, height/2, i, j); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { rect(i, j, 10, 10); } }
-
Yes! Nicely done!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(i+20, 0, i, j); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { rect(i, j, 5, i); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(width, height, i, j); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(i, 10, 5, j); } }
-
Try again!
nestedloops-quiz2: Which of the following code fragments would generate the image below?
Question 3¶
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(width/2, height/2, i, j); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { rect(i, j, 10, 10); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(i+20, 0, i, j); } }
-
Yes! Nicely done!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { rect(i, j, 5, i); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(width, height, i, j); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(i, 10, 5, j); } }
-
Try again!
nestedloops-quiz3: Which of the following code fragments would generate the image below?
Question 4¶
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(width/2, height/2, i, j); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { rect(i, j, 10, 10); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(i+20, 0, i, j); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { rect(i, j, 5, i); } }
-
Yes! Nicely done!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(width, height, i, j); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(i, 10, 5, j); } }
-
Try again!
nestedloops-quiz4: Which of the following code fragments would generate the image below?
Question 5¶
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(width/2, height/2, i, j); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { rect(i, j, 10, 10); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(i+20, 0, i, j); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { rect(i, j, 5, i); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(width, height, i, j); } }
-
Yes! Nicely done!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(i, 10, 5, j); } }
-
Try again!
nestedloops-quiz5: Which of the following code fragments would generate the image below?
Question 6¶
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(width/2, height/2, i, j); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { rect(i, j, 10, 10); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(i+20, 0, i, j); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { rect(i, j, 5, i); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(width, height, i, j); } }
-
Try again!
for (let i = 0; i <= 200; i+=20) { for (let j = 0; j <= 200; j+=20) { line(i, 10, 5, j); } }
-
Yes! Nicely done!
nestedloops-quiz6: Which of the following code fragments would generate the image below?