Right out of how many
Show on a big screenThe program guessed. The seller knows the real word. How many guesses were right?
Loading lab…
- Five kept-back crates: a guess and a real word each.
- Pair 1: full, full. They agree. Tally: 1.
- Pairs 2 and 3 agree too. Tally: 3.
- Pairs 4 and 5 disagree. Final count: 3 of 5.
The rule
Check the pairs one at a time and count the matches. Here 3 of 5 match.
Walk both lists together. Add one when a pair agrees. Hand back the count. Two lists that agree on nothing give 0.
- guesses — what the rule said about each kept-back crate.
- labels — the seller's real word on each one.
The count of right guesses, out of all guesses, is the accuracy. Here it is 3 of 5.
| Crate | Guess | Real word | Agree? |
|---|---|---|---|
| 1 | full | full | yes |
| 2 | cut | cut | yes |
| 3 | cut | cut | yes |
| 4 | cut | full | no |
| 5 | full | cut | no |
The function agreed(guesses, labels) starts a box, count, at 0. A while loop visits both lists, one pair per turn. Here i is the position, starting at 0. So position 0 is crate 1. An if checks guesses[i] === labels[i]. The sign === asks if two values are the same. On a match, the box adds one. The box goes 1, 2, 3, 3, 3. Then return count gives back the number in the box.
Try the lab to finish this lesson.