Lesson 6 Β· Make a phone app

Build a virtual pet app

Make a pet that gets hungry, happy and sleepy when you tap its buttons.

About 25 minutes

Tab indents. Press Escape, then Tab, to leave the editor.

This is the big one! 🐢 You're going to build a virtual pet app. Your pet will get hungry, sleepy and bored, and you'll look after it by tapping buttons. You'll use everything you've learned so far, plus two new tricks.

How a virtual pet works

A virtual pet isn't really alive, of course. It's just a few numbers that the app keeps track of:

  • πŸ– Food: how full its tummy is
  • 🎾 Fun: how happy and playful it feels
  • ⚑ Energy: how wide awake it is

Each number goes from 0 (empty) to 10 (full). Tapping a button changes the numbers, and the app shows a mood emoji that matches how the pet feels.

ButtonWhat it does
πŸ– FeedFood goes up
🎾 PlayFun goes up, but playing uses energy and makes your pet a bit hungry
πŸ’€ SleepEnergy goes up

The numbers are variables, the same kind of named boxes you used for your click counter.

New trick 1: your own functions

You already know that a function is a set of instructions wrapped up together. You've been handing functions to addEventListener. But you can also make your own function and give it a name:

function show() {
  foodText.textContent = food
}

This makes a function called show. Making it doesn't run it. To run it, you call it by writing its name with round brackets:

show()

It's like teaching a dog a trick called "sit". πŸ• You teach it once, then you just say "sit!" whenever you want it. In your pet app, show() puts all the numbers on the screen. Every button calls it after changing the numbers.

New trick 2: making choices with if

Your pet's mood should depend on its numbers. If it's really hungry, it should look hungry! For that, JavaScript has if:

if (food <= 2) {
  moodText.textContent = '😫'
} else {
  moodText.textContent = 'πŸ˜„'
}

Read it like this: "If food is 2 or less, show the hungry face. Else (otherwise), show the happy face." The computer checks the question in the round brackets. If the answer is yes, it runs the first part. If the answer is no, it runs the part after else.

The <= sign means "less than or equal to". Here are the comparison signs you can use:

SignMeans
<less than
<=less than or equal to
>more than
>=more than or equal to
===exactly equal to

You can ask more than one question with else if. The computer checks each one in order and stops at the first yes:

if (food <= 2) {
  moodText.textContent = '😫'
} else if (energy <= 2) {
  moodText.textContent = '😴'
} else {
  moodText.textContent = 'πŸ˜„'
}
Question

Quick check

Using the code just above, what mood shows if food is 1 and energy is 1?
Pick an answer.

Build your pet

Time to bring your pet to life! The Feed button already works. You'll make the other two buttons work, then give your pet moods.

Challenge

Build a virtual pet

  1. Press Run and tap Feed. Food goes up!
  2. Make Play work: add 3 to fun, take 2 away from energy, and take 1 away from food. Then call show().
  3. Make Sleep work: add 4 to energy, then call show().
  4. In show(), use if and else so the mood emoji changes when your pet is hungry or tired.

Then choose your own pet and name in the HTML tab! πŸ±πŸ°πŸ¦–

Console
  1. Nothing logged yet.
  • Tapping Feed gives your pet more food
  • Tapping Play gives your pet more fun
  • Playing uses up some energy
  • Tapping Sleep gives your pet more energy
  • The numbers always stay between 0 and 10
  • Your pet's mood changes when it's tired or hungry
  • Your pet cheers up again after food and sleep

You built a real app with screens, buttons, numbers and choices. Give yourself a high five! πŸ™Œ

What you learned

  • A virtual pet is really a few variables that buttons change.
  • You can make your own function, like show(), and call it whenever you need it.
  • if, else if and else let the computer make choices, checking each question in order.
  • Signs like <= and > compare numbers.
  • You built a whole virtual pet app that you can play with! πŸΆπŸ†

Finished this lesson?

Mark it complete to track your progress on this device.