beanz Magazine

How to Make Kittens with JavaScript

Learning how to make kittens with JavaScript is a great way to learn how to use the free Chrome web browser to practice and learn JavaScript.

A subscriber recently suggested the magazine include kid-centric tutorials for Javascript and keeping it fun. Well, kittens are fun so JavaScript and kittens likely meet the criteria.

This tutorial is adapted from an excellent book, JavaScript for Kids, published by No Starch Press, pages 7-9. While you don’t need the book to do the tutorial, buy the book if you’re interested to learn much more about JavaScript in a way kids and non-technical people understand.

Requirements

The best part? You only need a Google Chrome web browser which is free and easy to find at https://www.google.com/chrome, if you don’t have it already on your computer.

Set Up

To get started, you’ll open your Google Chrome web browser, open a blank web page, and then open the coding console for the browser.

  1. First, open your Chrome web browser on your computer, You should see a default page with the Google search field.
  2. In the URL field type about:blank and press the Enter key on your keyboard to display a blank page.
  3. Display the coding console. If you use Windows or Linux, hold the Ctrl and Shift keys down as you press the J key. If you use a Mac, hold the Ctrl and Option keys as you press the J key.

Your Chrome web browser should look like this:

projects-make-kittens-chrome-browser-2
Google Chrome with blank page and coding console

 

Notice the Console tab is one of many tabs, the farthest right tab. Click the Console tab if somehow you get lost. For this tutorial, you only need to use the Console tab.

Let’s Warm Up

Before we make fresh warm kittens with JavaScript, we’ll start with a few simple math problems to show how JavaScript, the coding console, and your Chrome web browser work together.

  1. Click to the right of the > bracket in the console. You’ll see a blinking cursor. The console is ready for you to enter JavaScript.
  2. Type 3 + 4; and don’t forget the semi-colon after the 4. Press the Enter key. Your Chrome web browser used JavaScript to calculate 3 + 4. You should see 7 in the row below the command you entered.

You can repeat this process for any math calculation. For example, try these commands:

120/5;
24*5;

The forward slash (/) tells JavaScript to divide two numbers while the asterisk (*) tells JavaScript to multiply two numbers.

You should see this output in your console:

120/5;
24
24*5;
120

where 24 and 120 are the results of your commands to calculate 120 divided by 5 and 24 times 5.

Let’s Make Kittens!

Of course JavaScript does many more important things than calculations. You also can make kittens with JavaScript. Okay, not real kittens but defnitely digital kittens. Here’s an example:

=^.^=

See how the equals signs (=) on the left and right make whiskers, the carat (^) makes ears, and the period (.) makes a nose? This is called an emoticon. You can make all kinds of neat animals, people, and other things with the keys on your computer keyboard and a little imagination.

Today, however, we want to use JavaScript to make as many kittens as we want. We’ll type in some code, called a function, and then use the code to say how many kittens we want to make. Here’s how it works.

In the coding console, type this function code and pay attention to spaces, capitalization, curly braces, and smooth braces:

var drawKittens = function (howManyTimes) {
    for ( var i = 0; i < howManyTimes; i++ ) {
        console.log(i + " =^.^=");
    }
};

Then press the Enter key on your keyboard to enter this function in the memory of your coding console. If you typed everything correctly, you’ll see this output in your console:

< undefined

The < bracket tells you the console has saved the function to its memory. Now let's make kittens! Type this command in your console: drawKittens(5);.

You should see this output:

drawKittens(5);
0 =^.^=
1 =^.^=
2 =^.^=
3 =^.^=
4 =^.^=
undefined

Type the drawKittens command with a different number to make more kittens. For example, drawKittens(5); will make five kittens while drawKittens(50); will make 50 kittens. Way more fun than using your console to do boring math problems.

How JavaScript Makes Kittens

Once the novelty of using JavaScript to make kittens wears off, you might want to know how the code works. Here’s our function code again:

var drawKittens = function (howManyTimes) {
    for ( var i = 0; i < howManyTimes; i++ ) {
        console.log(i + " =^.^=");
    }
};

Notice these details about our code:

  • var tells JavaScript to create a variable (var is shorthand for variable) called drawKittens then uses the equals sign (=) to say drawKittens will be a function with one input parameter called howManyTimes. When we use drawKittens, we need to tell the function how many times to draw a kitten. The left curly brace ({) marks the start of the definition of our drawKittens function while the right curly brace and semi-colon (};) marks the end of our definition.
  • The for statement in the next line tells JavaScript to run some code for as long as a set of conditions are met:
    1. First, we use var i = 0; create a variable called i (the letter i) and add a semi-colon to tell JavaScript var i = 0 is one condition.
    2. Second, we use i < howManyTimes; to tell JavaScript how long to run this code, for as long as i is less than the howManyTimes value we use.
    3. Finally, we use i++ to tell JavaScript to add 1 to the value of our i variable each time the code is run.

    So this for statement says, in English, "run this code for as long as these conditions are true: the variable i starts out as 0, and i is less than the howManyTimes value, and each time we run this for statement we add 1 to the variable i".

    When we start, the value of i is 0. The second time our code runs the for statement, we add 1 to 0 which means i now equals 1. The third time our code runs the for statement, we add 1 to the value of i (1) which sets i equal to 2. When the value of i is no longer less than the value we set for howManyTimes, then our for statement stops. The conditions of our for statement are no longer true. Also notice the left curly brace ({) and right curly brace (}) mark the start and end of our for statement and the code to run.

  • The console.log(i + " =^.^="); code runs when the for statement conditions we set are true. This makes our kittens. We tell JavaScript to print out in our coding console the value of the i variable plus a blank space and our kitten emoticon. The + (plus) sign tells JavaScript to add (combine) the value of our i variable with whatever we typed between the double quotes (" =^.^="), in this case, a blank space and our kitten emoticon.

With JavaScript, and many programming languages, variables like i do not need double quotes but anything we want to add that is text does need double quotes around it. Why? JavaScript recognizes variables and numbers but is not sure what to do with any other input. The double quotes tell JavaScript, and many other languages, that it should treat the content between the quotes as one item.

Finally, when we type the drawKittens(5); command, we tell JavaScript to use the drawKittens(howManyTimes) function and pass the number 5 as the value for the howManyTimes input parameter of our function.

 

If you want to learn more JavaScript, definitely look for the book this tutorial is cribbed from, JavaScript for Kids, published by No Starch Press. This tutorial also shows you how to use the Chrome web browser to practice as you learn JavaScript.

Learn More

JavaScript for Kids

https://www.nostarch.com/javascriptforkids

Using the Console

https://developers.google.com/web/tools/javascript/console/

JavaScript Documentation

https://developer.mozilla.org/en-US/docs/Web/JavaScript

Crunchzilla Code Monster

http://www.crunchzilla.com/code-monster

What is the JavaScript equivalent of Learn Python the Hard Way? (Quora)

Learn Python the Hard Way is a mostly free online course and this thread offers ideas where and how to learn JavaScript.
https://www.quora.com/What-is-the-JavaScript-equivalent-of-Learn-Python-the-Hard-Way


Also In The October 2015 Issue

October 2015 Issue: Internet of Things (IoT)

The Internet of Things (IoT) connects dumb devices like refrigerators to the internet and uses software to connect them to our daily lives.

There are many operating systems for internet of things devices, from existing software used to control electronic boards to efforts by Google and Apple.

Disney Infinity 3.0, Rocket League, and Super Mario Maker are three fun video games to consider for the 2015 holiday season

You can learn a little software programming and have lots of fun with any number of coding apps available for your phone or tablet computer.

Operating system software is a key part of all computers. But what are they and how do they work?

Learning how to make kittens with JavaScript is a great way to learn how to use the free Chrome web browser to practice and learn JavaScript.

Working through a book can help parents learn programming with their kids or kids learn on their own.

Board games and card games are some of the best ways to learn about programming. You don't need a computer. Play as a family or group.

These robots also can be programmed to move around rooms, one way for kids to learn programming.

Six women were hired to use their math skills to program the ENIAC computer. They called themselves The First Programmers Club.

Nicky is a Linguistics major who learned coding skills to further her research. She's also finished a PhD, won a few big awards, and co-founded Grok Learning.

Two women created an innovative online service to teach teenage girls how to code by using video.

The new Rust programming language is designed to solve problems with operating systems and fix issues with C and other languages.

Data can become alive and pose questions as well as reveal answers to questions we have.

Links from the bottom of all the articles in this issue, collected in one place for you to print, share, or bookmark.

DRY is an acronym for Don't Repeat Yourself. It's a critical programming concept and skill to learn.

Interesting news stories about computer science, software programming, and technology for August and September 2015.

CoderDojo is a free after school club for kids ages 7-17 where kids, parents, mentors, and others play with technology and learn to code.