← Teacher guide

Creature scores

Students meet functions that give back a value. BarWidth(stat) replaces the same width calculation written three times, then Overall(power, speed, defence) gives back one score shown for the clicked creature. Nib's Overall works the answer out into a variable and never gives it back. Make it yours adds Rank(score), with an If inside the function.

App Builder apps · Tidy up · about 45 minutes · Open the student lesson

Classroom resources

Open these in Waypoints Prepare with one click. To share a file another way, copy its address and paste it into Load URL, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Try 3 min · Spot 4 min · Build 1 12 min · Trace 4 min · Build 2 10 min · Check 6 min · Make it yours 6 min

Before class

This lesson grows the Creature stats app and comes after Tidy up the journal, which introduced functions that do something. It's Level 4 (Functions); each step opens App Builder at Level 4. The starting app is the finished creature stats viewer with an empty OverallText added at the bottom.

BarWidth is a refactor (the app looks the same afterwards), so the run in step 6 of Build 1 is the check, as in the journal lesson. Overall is new behaviour.

The call fills parameters in order: Overall(Creature.power, Creature.speed, Creature.defence) puts the first value into power and so on. Swapping them doesn't change an average, which is worth noticing: for most functions order matters (try a function Difference(a, b) giving back a - b).

Scores: Blaze 57, Tide 60, Zip 55.

Running the lesson

  1. Spot the repetition: same as the journal lesson. Circle what's the same, underline what's different.
  2. Build 1: checkpoint: do the bars look exactly as before? That's the test for a refactor.
  3. Trace: work out Zip's speed bar by hand before anyone clicks.
  4. Check: vote on Blaze's score, then click.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Students mix up the two kinds of function. Pick (last lesson) does something and is used with Call; BarWidth gives back a value and is used inside a value box. If a student tries Call BarWidth, ask: where would its answer go?

When students get stuck

What you will seeA question to askThe fix
The problem says BarWidth doesn't give a value back yet.Is there a Set return value inside BarWidth?Click + Give back a value in the function, and fill in stat / 100 * 200.
All three bars are the same width.What's inside the brackets of each BarWidth?Each bar passes its own stat: BarWidth(Creature.power), BarWidth(Creature.speed), BarWidth(Creature.defence).
Overall shows a decimal like 56.666.Which built-in function rounds a number?Wrap it: round((power + speed + defence) / 3).

Answers to the checks

  1. Spot the repetition: Open the Events tab. The three bar actions each set a width. What is different between them?
    Only the stat: power, speed or defence
    All three work out stat / 100 * 200. Only the stat changes, so it becomes the parameter, and the calculation goes in the function once.
  2. What does it give back?: Zip's speed is 95. What does BarWidth(Creature.speed) give back when Zip is clicked?
    190
    For this call, stat is 95: 95 / 100 * 200 = 190. The next call gives stat a different value.
  3. Nib's slip: Clicking a creature shows the problem "Overall doesn't give a value back yet". Click the line where Nib goes wrong.
    line 2
    Line 2: Nib puts the answer in a variable, so Overall works it out and then keeps it. Only Set return value hands it back to the place that asked.
  4. Blaze's score: Blaze has power 80, speed 60 and defence 30. What does Overall give back?
    57
    (80 + 60 + 30) / 3 = 56.67, and round gives 57.

Solutions

One possible solution for each task. Other correct programs are fine.

Make it yours: a rank

{"version":1,"name":"Creature stats (scores)","features":{"level":4},"variables":[],"screens":[{"id":"main","name":"Creatures","objects":[{"name":"Title","kind":"text","instanceVars":[],"instances":[{"id":"t0","x":229,"y":20,"w":342,"h":34,"text":"Click a creature","visible":true,"vars":{}}]},{"name":"Creature","kind":"image","instanceVars":[{"name":"name","value":""},{"name":"power","value":0},{"name":"speed","value":0},{"name":"defence","value":0},{"name":"ability","value":""}],"instances":[{"id":"k0","x":235,"y":70,"w":100,"h":100,"text":"Blaze","visible":true,"vars":{"name":"Blaze","power":80,"speed":60,"defence":30,"ability":"Fire breath"},"image":"star"},{"id":"k1","x":350,"y":70,"w":100,"h":100,"text":"Tide","visible":true,"vars":{"name":"Tide","power":50,"speed":40,"defence":90,"ability":"Water shield"},"image":"water"},{"id":"k2","x":465,"y":70,"w":100,"h":100,"text":"Zip","visible":true,"vars":{"name":"Zip","power":30,"speed":95,"defence":40,"ability":"Super dash"},"image":"music"}]},{"name":"NameText","kind":"text","instanceVars":[],"instances":[{"id":"k10","x":229,"y":200,"w":342,"h":34,"text":"","visible":true,"vars":{}}]},{"name":"PowerText","kind":"text","instanceVars":[],"instances":[{"id":"k11","x":229,"y":246,"w":120,"h":26,"text":"","visible":true,"vars":{}}]},{"name":"SpeedText","kind":"text","instanceVars":[],"instances":[{"id":"k12","x":229,"y":282,"w":120,"h":26,"text":"","visible":true,"vars":{}}]},{"name":"DefenceText","kind":"text","instanceVars":[],"instances":[{"id":"k13","x":229,"y":318,"w":120,"h":26,"text":"","visible":true,"vars":{}}]},{"name":"PowerBar","kind":"shape","instanceVars":[],"instances":[{"id":"PowerBar","x":355,"y":250,"w":0,"h":18,"text":"","visible":true,"vars":{},"color":"#e5484d"}]},{"name":"SpeedBar","kind":"shape","instanceVars":[],"instances":[{"id":"SpeedBar","x":355,"y":286,"w":0,"h":18,"text":"","visible":true,"vars":{},"color":"#2f8fdd"}]},{"name":"DefenceBar","kind":"shape","instanceVars":[],"instances":[{"id":"DefenceBar","x":355,"y":322,"w":0,"h":18,"text":"","visible":true,"vars":{},"color":"#2fa36b"}]},{"name":"AbilityText","kind":"text","instanceVars":[],"instances":[{"id":"k14","x":229,"y":362,"w":342,"h":30,"text":"","visible":true,"vars":{}}]},{"name":"OverallText","kind":"text","instanceVars":[],"instances":[{"id":"ov1","x":229,"y":400,"w":342,"h":30,"text":"","visible":true,"vars":{}}]}],"events":[{"id":"k","else":false,"conds":[{"type":"onClicked","target":"Creature"}],"actions":[{"type":"setText","target":"NameText","value":"Creature.name"},{"type":"setText","target":"PowerText","value":"\"Power: \" & Creature.power"},{"type":"setText","target":"SpeedText","value":"\"Speed: \" & Creature.speed"},{"type":"setText","target":"DefenceText","value":"\"Defence: \" & Creature.defence"},{"type":"setText","target":"AbilityText","value":"\"Special: \" & Creature.ability"},{"type":"setSize","target":"PowerBar","prop":"w","value":"BarWidth(Creature.power)"},{"type":"setSize","target":"SpeedBar","prop":"w","value":"BarWidth(Creature.speed)"},{"type":"setSize","target":"DefenceBar","prop":"w","value":"BarWidth(Creature.defence)"},{"type":"setText","target":"OverallText","value":"\"Overall: \" & Overall(Creature.power, Creature.speed, Creature.defence) & \" (\" & Rank(Overall(Creature.power, Creature.speed, Creature.defence)) & \")\""}],"children":[]}]}],"device":"web","functions":[{"id":"fbar","name":"BarWidth","params":["stat"],"events":[{"id":"fbare","else":false,"conds":[],"actions":[{"type":"setReturn","value":"stat / 100 * 200"}],"children":[]}]},{"id":"fall","name":"Overall","params":["power","speed","defence"],"events":[{"id":"falle","else":false,"conds":[],"actions":[{"type":"setReturn","value":"round((power + speed + defence) / 3)"}],"children":[]}]},{"id":"frank","name":"Rank","params":["score"],"events":[{"id":"fr1","else":false,"conds":[{"type":"compareValues","left":"score","op":"≥","right":"60"}],"actions":[{"type":"setReturn","value":"\"Strong\""}],"children":[]},{"id":"fr2","else":true,"conds":[],"actions":[{"type":"setReturn","value":"\"Still training\""}],"children":[]}]}]}

Stretch and support

Stretch: A function Stars(score) that gives back a row of stars: "★" for a score under 50, "★★" under 70, "★★★" otherwise (If, Else if, Else inside the function). Which is easier to read: Stars(Overall(…)) in the text, or the Ifs written out in the click event?

Support: Do Build 1 together for PowerBar on the board; pairs change the other two bars and do Build 2.

Exit ticket

A canteen app works out price × quantity for every item on an order. What function would you make, what would it give back, and how would you use it?

Total(price, qty) gives back price * qty. Each item's line uses Total(item price, item quantity) in a value box, and the order total adds them up.

What each step is for

Where it leads

Next: the challenge, a prototype of their own. A function that gives back a value is worth reaching for whenever the same calculation appears twice.