javascript · level 1 · unit 7 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Big programs are small pieces working as a team.
Real programs are built from small parts. An array holds the data. A loop walks through it. A function answers one small question, and an if decides what to do with each answer.
Here, return rating >= 7; hands back true or false, so the answer slots straight into an if. The function is short, so its block sits on one line.
When something goes wrong, you : find the bug, fix it, run again. Every programmer does this every day. Small functions help, because you can test each one on its own.
function isTasty(rating) { return rating >= 7; }
let ratings = [9, 3, 8, 5];
for (let rating of ratings) {
if (isTasty(rating)) { console.log(`${rating} out of 10. Yum!`); }
} Test each function on its own first, then put the pieces together.
guess first
Every piece at once. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
function double(number) { return number * 2; }
let total = 0;
for (let goal of [1, 2, 3]) {
total = total + double(goal);
}
console.log(total);
your turn
Your turn. Plan a trip to the snack shop.
Put three snack prices in the array, then type how much money you have in the last blank. Make sure you can afford at least one snack!
fix it
Only one Paris counts as right. Debug it!
- Only one guess counts as right. What is different about the other two?
- JavaScript is case-sensitive, so Paris and paris are different strings. Change the guess before comparing.
- On line 1, change guess === to guess.toLowerCase() ===, then press run.
nailed it
you put all the pieces together. :D
- Small functions, each with one job, build big programs.
- Arrays hold data, loops walk through it, and
ifdecides. - To debug, find the bug, fix it, then run again.
new words in your collection
- debug
This lesson isn’t saved. A free account remembers every lesson you finish.