JavaScript Projects For Kids - Sample Chapter
JavaScript Projects For Kids - Sample Chapter
ee
P U B L I S H I N G
C o m m u n i t y
Discover the vital concepts of object-oriented
programming
Learn about several open source projects
and play with their source code
Build your own web applications following the
guidelines provided
$ 24.99 US
15.99 UK
pl
JavaScript Projects
for Kids
Sa
m
E x p e r i e n c e
JavaScript Projects
for Kids
Gear up for a roller-coaster ride into the world of JavaScript
and programming with this easy-to-follow, fun, and entertaining
project-based guide
D i s t i l l e d
is a technologist, tech speaker, and physics lover from Shahjalal University of Science
and Technology (SUST), Sylhet. He has a passion for programming, tech writing, and
physics experiments.
His recent books include Easy Circuits for Kids, Fundamentals of Ruby, and How
You Should Design Algorithms. He is an Oracle-certified professional developer
currently involved with a number of projects that serve both physics and
computer architecture.
He is the president of one of the largest astronomical organizations (Copernicus
Astronomical Memorial of SUST (CAM-SUST)) in Bangladesh. He also volunteers
for Mozilla as a representative.
Preface
As you can guess from the title of the book, this book is designed and set up for kids so
that they can teach themselves JavaScript and create some projects using JavaScript.
By abstracting the core web programming in an unparalleled way, JavaScript
changed websites and web apps forever. Boring static websites and non-interactive
websites have now become quite awesome with the touch of JavaScript. Using
JavaScript, you can develop web applications, even smartphone applications too,
quickly without compromising quality. You can be very productive and deal with
almost no configuration on your hardware and software if you start playing with
JavaScript. Please remember that this is not a reference book, but you can learn every
basic concepts of JavaScript from it. So, for the kids aged 10 and above, this will be a
perfect book to discover the world of JavaScript.
Preface
Chapter 5, Ahoy! Sailing into Battle, teaches us how to develop the famous game,
Battleship. Building upon what we've learned in the previous chapters, the tiny
tots will learn to put this information into use.
Chapter 6, Exploring the Benefits of jQuery, is all about jQuery, a famous JavaScript
library, and the advantages of using it.
Chapter 7, Introducing the Canvas, discusses HTML canvas, and we will learn how we
can use it on our projects.
Chapter 8, Building Rat-man!, teaches us to develop a famous game, Pac-Man, except
there is a rat, some cats, and lots and lots of cheese balls to eat! ;)
Chapter 9, Tidying up Your Code Using OOP, teaches object-oriented programming
(OOP) and discusses how JavaScript is an OOP language.
Chapter 10, Possibilities, shows the reader what is possible using the skills they have
developed reading this book.
Solving Problems
Using JavaScript
You have learned how to print something using JavaScript on console in the
previous chapter. Now, let's see the fundamentals behind JavaScript syntax,
variables, arithmetic operators, and comments.
In the computer world, there is nothing but data. You can read, modify, and create
new data; however, anything that isn't data simply does not exist. In JavaScript, we
need to handle data to develop a website.
To understand the basic syntax of JavaScript, first of all you need to know that
JavaScript is case sensitive. You cannot interchange lower case and upper case letters
in JavaScript. Therefore, when dealing with the JavaScript syntax, you need to
remember that writing the code is not the only important task, you must also watch
the syntax whether it's written correctly.
Let me show you an example. In the previous chapter, you have successfully printed
Hello World on your browser using the document.write(); syntax.
What would happen if you wrote Document.write("Hello World");? Yes! It won't
run successfully. You will get an error message. This kind of errors is known as
Uncaught SyntaxError.
A JavaScript statement is typically written on one line. You may finish your
statement with a semicolon or not. It is not mandatory to end a statement with a
semicolon. However, it is a good practice to add a semicolon after each statement.
Let's consider the following example:
document.write("Hello");
document.write("World");
document.write("!");
[ 11 ]
JavaScript keywords (such as for, while, if, switch, case, and so on)
are always in lowercase. The build-in objects (such as Date, Math,
Number, and so on) start with uppercase.
Variables
We already know that the computer world has nothing but data.
There are different types of data (we call them data types), as follows:
Yet, they all are different. What is the difference between them? Your name only
contains a group of characters or, as some people also call it, string. Your age is an
integer type data. Your grade is a float type data. The wonderful thing in JavaScript
is that you do not have to specify the data type before writing a variable's name.
JavaScript allows working with three data types. Strings
(for example, "This is an example of string"),
numbers (for example, 2015, 3.1415, and so on), and
Boolean (for example, true or false).
Did we discuss variables? Well, you already know the data types. You will need
something to store your data. This something is called variable. In JavaScript, we
use var before the variable names. Remember that var starts with small letter.
[ 12 ]
Chapter 2
x;
y;
sum;
name;
Let's say that we have 14 apples and 6 oranges. To store them in variables we will
use the following:
var apples = 14;
var oranges = 6;
The following example is not the same. Can you tell why?
var
var
var
var
Apples
apples
APPLES
appleS
=
=
=
=
14;
14;
14;
14;
Yes, JavaScript is case sensitive. All the variables are different here, though the
values of the variables are the same.
Now, let's do some coding. Previously, on console, you printed your name as
homework. I hope you did it without any trouble. How about we now print your
name differently using a variable? Assume that your name is Sherlock Holmes.
What kind of data is it?
You are right, it is string type. Usually for string type data, we put the string between
two quotes.
Let's consider the following example:
var name = "Sherlock Holmes";
var occupation = "Detective"
[ 13 ]
To print them using console, you need to type each statement and press Enter. Take a
look at the following image:
Do not copy and paste the codes on the console. You might get
a syntax error.
You will see an extra line appearing after you hit Enter, stating undefined. Don't
worry about this for now. It just returned a console log.
You stored the Sherlock Holmes string on the name variable and you stored
Detective on occupation. Every time you access name or occupation, you can
access the stated strings.
Consider that you want to print Sherlock Holmes on your screen. Just type the
following:
document.write(name);
After typing, hit Enter. You will see Sherlock Holmes is printed on the screen,
as follows:
[ 14 ]
Chapter 2
You may be wondering why is there no space between Sherlock Holmes and
Detective. As, on the console, the history is not automatically removed from the
web page on the left-hand side and after you hit Enter for your second output
(occupation), the string places itself right after the previous string. This will always
happen, unless you clear your console using the Ctrl + L keyboard shortcut and
reload the web page pressing the key F5.
Your stored variables will also be erased from the memory when
you reload the web page. Don't worry, you will be taught how to
use your variables storing on a file in the next chapter.
If you want to join two (or multiple) variables, you add a plus sign (+) between the
two variables, as follows:
document.write(name+occupation);
document.write(occupation+name);
If you want to add any string (for example, a space) between the two strings other
than any variables, just type the following:
document.write(name+" "+occupation);
Yes! You are absolutely right. The output will be as shown in the following:
My name is Sherlock Holmes and I am a Detective
Now, add another variable on the console. Consider that Sherlock Holmes is 24
years old. Do you remember what kind of data age is?
Yes, it is an integer type of number. Therefore, type the following code and hit Enter:
var age = 24;
Name
Occupation
Age
[ 16 ]
Chapter 2
However, this will give you an error known as SyntaxError. To get rid of
this error, you need to use a backward slash (\) before the two inverted
commas. The correct code will be as follows:
document.write("Shakespeare said, \"To be, or not to
be: that is the question!\"");
[ 17 ]
Comments
Suppose you have done a lot of coding and some logical operations, and used a
number of variables on JavaScript, and you want me to help you with the code
if any errors occur. When you send me the code, I will not know what you have
typed unless I have a clear knowledge of JavaScript or you have commented on
the important lines.
A comment is basically a line of text or code that your browser ignores while
running. You can compare comments to sticky notes or reminder.
Let's consider the following example:
Var name = "Sherlock Holmes"; // This is a string
Var occupation = "Detective"; // This variable stores information
Var age = 14; // This is an integer type of data.
How do you make multiline comments? You mention the comment in the
following manner:
/*
This is a multiline comment.
The browser will ignore this.
You can type any important information on your comment.
*/
Your multiline comment should be between /* and */, as shown in the following
screenshot:
[ 18 ]
Chapter 2
Arithmetic operators
In JavaScript, like other programming languages, we can do some arithmetic
operations. In your school, you might have already learned how to add two
numbers, subtract one number from another number, multiply two numbers, and
divide a number with another. You can do all these things in JavaScript with the help
of a few lines of code.
In JavaScript, we use the following arithmetic symbols for the operations:
Operator
Description
To add
To subtract
To multiply
To divide
Addition
Suppose you have two variables, x and y, with the values 3 and 4, respectively. What
should we do on the console to store the values on the variables?
Yes, we do the following:
var x = 3; // 3 is stored on variable x
var y = 4; // 4 is stored on variable y
[ 19 ]
Yes, you are correct, this will print 7, as shown in the following screenshot:
Subtraction
To subtract a number from another, you need to put a minus sign (-) between them.
Let's consider the following example:
var x = 9; // 9 is
var y = 3; // 3 is
var z = x - y ; //
document.write(z);
[ 20 ]
Chapter 2
Multiplication
To multiply two numbers or variables that have integer or float type of data stored
on them, you just put an asterisk (*) between the variables or numbers.
Let's take a look at the following example:
var x = 6; // 6 is assigned to the variable x.
var y = 2; // 2 is assigned to the variable y.
var z = x * y; // For two numbers you can type z = 6 * 2 ;
document.write(z); // Prints the value of z
Division
To divide a number with another, you need to put a forward slash (/) between the
numbers.
Let's take a look at the following example:
var x = 14; // assigns 14 on variable x.
var y = 2; // assigns 2 on variable y.
var z = x / y; // divides x with y and stores the value on z.
document.write(z); // prints the value of z.
[ 21 ]
Modulus
If you want to find the modulus of a number with another, you need to put a
percentage sign (%) between the numbers.
Let's consider the following example:
var x = 34; // assigns 34 on the variable x.
var y = 3; // assigns 3 on the variable y.
var z = x % y ; // divides x with y and returns the reminder and
stores on the variable z
document.write(z);
[ 22 ]
Chapter 2
Now, let's summarize all the arithmetic operators that we learned so far in one
single code.
Can you tell me the output of the following lines?
var x = 5 ;
var y = 4 ;
var sum = x + y ;
var sub = x - y ;
var mul = x * y ;
var div = x / y ;
var mod = x % y ;
document.write("The
document.write("The
document.write("The
"<br>");
document.write("The
document.write("The
I guess you nailed it. Now, let's explain them in the following:
[ 24 ]
Chapter 2
What will you do if you want to increment your variable by more than 1? Well, you
can follow this:
var x = 4; // assigns 4 on the variable x.
x = x + 3; // Say, you want to increment x by 3.
/* since x = 4, and you are adding 3 with x, so the final value is
4 + 3 = 7, and 7 is stored on the same variable x. */
[ 25 ]
The output will look similar to the following screenshot on the console:
What about the decrement operator? Yes, you are absolutely right. Decrement
operations are same as the increment operations. The only thing that changes is the
sign. Your addition (+) operator will be replaced by the subtraction operator (-). Let's
take a look at an example:
var x = 9; // assigns 9 on the variable x.
x = x - 1;
/* since x = 9, and you are subtracting 1 from x, so the final
value is 9 - 1 = 8, and 8 is stored on the same variable x. */
What will you do if you want to decrement your variable by more than 1? Well, you
can follow this:
var x = 9;
x = x - 4;
/* since x
value is
Chapter 2
These type of operations are very important for logical operations in JavaScript. You
will learn about their uses in Chapter 4, Diving a Bit Deeper.
Assignment operators
An assignment operator assigns a value to an operator. I believe that you already
know about assignment operators, don't you? Well, you use an equal sign (=) between
a variable and its value. By doing this, you assigned the value to the variable.
Let's take a look at the following example:
var name = "Sherlock Holmes"
[ 27 ]
The Sherlock Holmes string is assigned to the name variable. You have already
learned about increment and decrement operators. Can you tell me what will the
output of the following codes be?
var x = 3;
x *= 2;
document.write(x);
[ 28 ]
"+ y+", z =
"+z
);
Chapter 2
Description
==
Equal to
!=
Not equal to
>
Greater than
<
Less than
=>
<=
[ 29 ]
You will learn more about the use of these operators in the following chapters.
Let's discuss a few bitwise logical operators and bitwise operators:
Operators
Description
&&
This means the AND operator. To check whether two or more statements
are true, we use this.
||
This means the OR operator. To check whether any of the statement is true,
we use this.
>>
<<
They might be hard for you to learn right now. Don't worry, you don't have to use
them now. We will use them in Chapter 4, Diving a Bit Deeper.
[ 30 ]
Chapter 2
Summary
In this chapter, you learned about the JavaScript syntax. We discussed the JavaScript
variables and how to assign a value to a variable. You learned how to comment
on the code. You now know why commenting is important. You finally learned an
important topic: operators and operations. JavaScript, without using operators and
logical functions, will not be so rich nowadays. Therefore, learning about the logical
operations is the key to gain good knowledge of JavaScript.
I would like to suggest you to practice all the code in this chapter at home. You just
type them on the console, avoid copying and pasting the codes. This will hamper
with your learning. As a programmer must have a good typing speed, copying and
pasting the codes will not improve this skill. You may face problems in typing codes;
however, you will learn.
You can solve any arithmetic problem using JavaScript. You can also check whether
your logic is true or false on console. If you can do this, we can move on to the next
chapter, Chapter 3, Introducing HTML and CSS, where you will learn about HTML,
CSS, and so on.
[ 31 ]
www.PacktPub.com
Stay Connected: