<div id="pnlWeighPillsContainerLeft" class="pnlWeighPillsContainer">
<div class="pnlWeighPills" id="pnlWeighPills_left" onclick="pillgame.placePill('left')">
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div><div class="bowl"></div></div>
</div>
</div>
<div class="scaleContainer">
<div class="pnlScale">⚖</div>
<div>Times Used: <span id="pnlTimesUsed"></span></div>
<div><button id="btnWeigh" onclick="pillgame.weigh()">WEIGH</button></div>
</div>
<div id="pnlWeighPillsContainerRight" class="pnlWeighPillsContainer">
<div class="pnlWeighPills" id="pnlWeighPills_right" onclick="pillgame.placePill('right')">
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div><div class="bowl"></div></div>
</div>
</div>
<div class="pnlWeighPills" id="pnlWeighPills_left" onclick="pillgame.placePill('left')">
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div><div class="bowl"></div></div>
</div>
</div>
<div class="scaleContainer">
<div class="pnlScale">⚖</div>
<div>Times Used: <span id="pnlTimesUsed"></span></div>
<div><button id="btnWeigh" onclick="pillgame.weigh()">WEIGH</button></div>
</div>
<div id="pnlWeighPillsContainerRight" class="pnlWeighPillsContainer">
<div class="pnlWeighPills" id="pnlWeighPills_right" onclick="pillgame.placePill('right')">
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div class="pillSlot"></div>
<div><div class="bowl"></div></div>
</div>
</div>
We'll work on the placePill() method next. It accepts a parameter, slot. We have the default return statement, and a guard clause that says if playerStatus is anything but "ill", nothing happens. Which makes sense; in those cases the game is over and nothing needs to happen.
placePill: function(slot)
{
if (this.playerStatus != "ill") return;
return;
},
{
if (this.playerStatus != "ill") return;
return;
},
Now let's have a few cases. The first case is for if no pill is selected; that's when we return the pill rather than placing it. The second case is for if slot is "mouth", which means a pill has been selected and we're about to feed it to the face at the bottom. The last two are "left" and "right", and those don't have curly brackets because they're one-line operations.
placePill: function(slot)
{
if (this.playerStatus != "ill") return;
if (this.pill_selected == -1)
{
return;
}
if (slot == "mouth")
{
return;
}
if (slot == "left")
if (slot == "right")
return;
},
{
if (this.playerStatus != "ill") return;
if (this.pill_selected == -1)
{
return;
}
if (slot == "mouth")
{
return;
}
if (slot == "left")
if (slot == "right")
return;
},
Let's handle the last two cases first. If pill_selected is not -1, that means there is a pill selected. If that's the case, we run the unshift() method on the appropriate array to place the selected pill at the beginning of the array.
placePill: function(slot)
{
if (this.playerStatus != "ill") return;
if (this.pill_selected == -1)
{
return;
}
if (slot == "mouth")
{
return;
}
if (slot == "left") this.pills_left.unshift(this.pill_selected);
if (slot == "right") this.pills_right.unshift(this.pill_selected);
return;
},
{
if (this.playerStatus != "ill") return;
if (this.pill_selected == -1)
{
return;
}
if (slot == "mouth")
{
return;
}
if (slot == "left") this.pills_left.unshift(this.pill_selected);
if (slot == "right") this.pills_right.unshift(this.pill_selected);
return;
},
After that, declare index, and check if pills_middle still has the pill selected. If it does, remove it using the splice() method. Then set pill_selected to -1 and run renderPills().
placePill: function(slot)
{
if (this.playerStatus != "ill") return;
if (this.pill_selected == -1)
{
return;
}
if (slot == "mouth")
{
return;
}
if (slot == "left") this.pills_left.unshift(this.pill_selected);
if (slot == "right") this.pills_right.unshift(this.pill_selected);
const index = this.pills_middle.indexOf(this.pill_selected);
if (index !== -1) this.pills_middle.splice(index, 1);
this.pill_selected = -1;
this.renderPills();
return;
},
{
if (this.playerStatus != "ill") return;
if (this.pill_selected == -1)
{
return;
}
if (slot == "mouth")
{
return;
}
if (slot == "left") this.pills_left.unshift(this.pill_selected);
if (slot == "right") this.pills_right.unshift(this.pill_selected);
const index = this.pills_middle.indexOf(this.pill_selected);
if (index !== -1) this.pills_middle.splice(index, 1);
this.pill_selected = -1;
this.renderPills();
return;
},
Let's test this! Click START. Click on pill 3 and then on the left side. Does pill 3 appear on the left?
Now click on pill 4 and then on the right side. Does pill 4 appear on the right?
What if you then clicked on pill 7 and then on the left side?
Go on, add more pills on the left and right side of the scale. Notice how the pills disappear from the middle when you place them.
Now let's handle replacing the pills. If no pills are selected (meaning pill_selected is -1), running placePill() should result in running returnPill(). But I'm getting ahead of myself; let's begin with the first If block in the placePill() method. We establish two more If blocks in those, and an early return statement after. These check for the value of slot, and ensure that there are actually elements in the relevant array before taking any action.
if (this.pill_selected == -1)
{
if (slot == "left" && this.pills_left.length > 0)
{
}
if (slot == "right" && this.pills_right.length > 0)
{
}
return;
}
{
if (slot == "left" && this.pills_left.length > 0)
{
}
if (slot == "right" && this.pills_right.length > 0)
{
}
return;
}
Here, you set pill_selected as the first element of pills_left. Then you remove it from pills_left using the shift() method.
if (this.pill_selected == -1)
{
if (slot == "left" && this.pills_left.length > 0)
{
this.pill_selected = this.pills_left[0];
this.pills_left.shift();
}
if (slot == "right" && this.pills_right.length > 0)
{
}
return;
}
{
if (slot == "left" && this.pills_left.length > 0)
{
this.pill_selected = this.pills_left[0];
this.pills_left.shift();
}
if (slot == "right" && this.pills_right.length > 0)
{
}
return;
}
With that, you run the returnPill() method (we'll build that soon) and then renderPills() to update the display.
if (this.pill_selected == -1)
{
if (slot == "left" && this.pills_left.length > 0)
{
this.pill_selected = this.pills_left[0];
this.pills_left.shift();
this.returnPill();
this.renderPills();
}
if (slot == "right" && this.pills_right.length > 0)
{
}
return;
}
{
if (slot == "left" && this.pills_left.length > 0)
{
this.pill_selected = this.pills_left[0];
this.pills_left.shift();
this.returnPill();
this.renderPills();
}
if (slot == "right" && this.pills_right.length > 0)
{
}
return;
}
We perform the analogous operations for the opposite side.
if (this.pill_selected == -1)
{
if (slot == "left" && this.pills_left.length > 0)
{
this.pill_selected = this.pills_left[0];
this.pills_left.shift();
this.returnPill();
this.renderPills();
}
if (slot == "right" && this.pills_right.length > 0)
{
this.pill_selected = this.pills_right[0];
this.pills_right.shift();
this.returnPill();
this.renderPills();
}
return;
}
{
if (slot == "left" && this.pills_left.length > 0)
{
this.pill_selected = this.pills_left[0];
this.pills_left.shift();
this.returnPill();
this.renderPills();
}
if (slot == "right" && this.pills_right.length > 0)
{
this.pill_selected = this.pills_right[0];
this.pills_right.shift();
this.returnPill();
this.renderPills();
}
return;
}
Now, what does returnPill() do?
returnPill()
{
return;
},
{
return;
},
It places the selected pill back in pills_middle via the push() method. Once that is done, pill_selected becomes -1 again.
returnPill()
{
this.pills_middle.push(this.pill_selected);
this.pill_selected = -1;
return;
},
{
this.pills_middle.push(this.pill_selected);
this.pill_selected = -1;
return;
},
Go on, try it. Reproduce what we did in the previous screenshot, then click on the left side of the scale. Does pill 7 get sent back to the middle?
We've been using a fair amount of JavaScript array methods here. If you feel like reading up further, here are some references.
- shift()
- unshift()
- push()
- splice()





















