JavaScript For Kids For Dummies
Book image
Explore Book Buy On Amazon

In order to be compiled correctly into machine language instructions, JavaScript programs need to be written very precisely. As a programmer, your job is to think about the big picture of what you want the program to do, and then break it down into bite‐size steps that can be accomplished by the computer without errors.

For example, if you wanted to ask a robot to go downstairs and get you a sandwich, you might start your instructions like this:

  1. Rotate head toward stairs.

  2. Use visual sensors to look for obstacles.

  3. If an obstacle is found, determine what it is.

  4. If the obstacle is a cat, try to lure the cat away from the top of the stairs by:

    • Throwing a toy down the hall

    • Speaking the cat's name

    • Gently nudging the cat with your hand until it walks away

  5. If there is no obstacle, rotate left foot in the direction of the stairs.

  6. Place left foot in front of right foot.

  7. Look for an obstacle.

  8. Determine whether you're at the top of the stairs.

  9. If you're not at the top of the stairs, rotate right foot in the direction of the stairs.

  10. Place right foot in front of left foot.

  11. Repeat steps 1 through 10 until you're at the top of the stairs.

You've written 11 instructions already and the robot hasn't even started walking down the stairs, much less making a sandwich!

A real computer program to tell a robot to go downstairs and make a sandwich would need to contain far more detailed instructions than the ones shown here. At each step along the way, each motor would need to be told precisely how long to turn on, and each possible condition and obstacle would need to be described and dealt with in detail.

All these instructions need to be written as individual JavaScript commands, or statements.

You can find out more about how to control robots with JavaScript by visiting NodeBots!

About This Article

This article is from the book:

About the book authors:

Chris Minnick and Eva Holland are experienced web developers, tech trainers, and coauthors of Coding with JavaScript For Dummies. Together they founded WatzThis?, a company focused on training and course development.

This article can be found in the category: