typescript · 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, isTasty hands back true or false, so its return type is boolean. The loop asks it about every rating in the array, and each tiny piece does one job.
When something goes wrong, you : find the bug, fix it, run again. The checker finds many bugs for you. The rest you hunt down by reading the output.
function isTasty(rating: number): boolean {
return rating >= 7;
}
for (const rating of [9, 3, 8, 5]) {
console.log(rating, isTasty(rating));
} 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 shout(word: string): string {
return word.toUpperCase() + "!";
}
for (const word of ["hip", "hip", "hooray"]) {
console.log(shout(word));
}
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?
- TypeScript is case-sensitive, so Paris and paris are different strings. Change the guess before comparing.
- On line 2, change guess to guess.toLowerCase(), then 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, functions answer questions.
- 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.