Workshop › Hangman › Step 22 of 36
Six ifs that all say "the n-th part after n misses" can be one list and one loop. Functions are values, so the
parts can sit in an array, in order, and we call the first wrong of them.
```js
// One drawing per wrong guess.
const PARTS = [
() => {
ctx.beginPath()
ctx.arc(170, 110, 20, 0, Math.PI * 2) // head
ctx.stroke()
},
() => line(170, 130, 170, 200), // body
() => line(170, 150, 140, 180), // left arm
() => line(170, 150, 200, 180), // right arm
() => line(170, 200, 145, 245), // left leg
() => line(170, 200, 195, 245), // right leg
]
for (let i = 0; i < wrong; i++) PARTS[i]()
```PARTS is an array of six functions, in drawing order. The head needs three lines, so its function has a { }
body; the others are one line call each.PARTS[0] is the first function; PARTS[0]() calls it.for (let i = 0; i < wrong; i++) repeats with i = 0, 1, ... up to wrong - 1. With 2 misses it calls
PARTS[0]() and PARTS[1](): head and body.function draw() {, write the PARTS list (with its comment) and leave an empty line.draw, delete the head if block and the five if lines, and write the for line in their place.Skills: Arrays and objects, Functions
← Body, arms and legs Losing →