Download Complete (Ebook) Begin to Code with JavaScript by Miles, Rob ISBN 9780136870722, 0136870724 PDF for All Chapters
Download Complete (Ebook) Begin to Code with JavaScript by Miles, Rob ISBN 9780136870722, 0136870724 PDF for All Chapters
com
https://ebooknice.com/product/begin-to-code-with-
javascript-55873262
DOWLOAD EBOOK
https://ebooknice.com/product/begin-to-code-with-python-7203980
ebooknice.com
https://ebooknice.com/product/begin-to-code-with-c-36373908
ebooknice.com
ebooknice.com
ebooknice.com
(Ebook) SAT II Success MATH 1C and 2C 2002 (Peterson's SAT
II Success) by Peterson's ISBN 9780768906677, 0768906679
https://ebooknice.com/product/sat-ii-success-
math-1c-and-2c-2002-peterson-s-sat-ii-success-1722018
ebooknice.com
ebooknice.com
ebooknice.com
https://ebooknice.com/product/source-code-analytics-with-roslyn-and-
javascript-data-visualization-55892642
ebooknice.com
https://ebooknice.com/product/c-programming-yellow-book-49184392
ebooknice.com
Begin to Code with JavaScript
Rob Miles
BEGIN TO CODE WITH JAVASCRIPT
No patent liability is assumed with respect to the use of the information contained
herein. Although every precaution has been taken in the preparation of this book,
the publisher and author assume no responsibility for errors or omissions. Nor is
any liability assumed for damages resulting from the use of the information
contained herein.
ISBN-13: 978-0-13-687072-2
ISBN-10: 0-13-687072-4
ScoutAutomatedPrintCode
TRADEMARKS
Every effort has been made to make this book as complete and as accurate as
possible, but no warranty or fitness is implied. The information provided is on an
“as is” basis. The author, the publisher, and Microsoft Corporation shall have
neither liability nor responsibility to any person or entity with respect to any loss
or damages arising from the information contained in this book or from the use
of the programs accompanying it.
SPECIAL SALES
For information about buying this title in bulk quantities, or for special sales
opportunities (which may include electronic versions; custom cover designs; and
content particular to your business, training goals, marketing focus, or branding
interests), please contact our corporate sales department at
[email protected] or (800) 382-3419.
Editor-in-Chief
Brett Bartow
Executive Editor
Loretta Yates
Development Editor
Rick Kughen
Sponsoring Editor
Charvi Arora
Managing Editor
Sandra Schroeder
Tracey Croom
Copy Editor
Rick Kughen
Indexer
James Minken
Proofreader
Scout Festa
Technical Editor
John E. Ray
Editorial Assistant
Cindy Teeters
Cover Designer
Compositor
Danielle Foster
Graphics
Danielle Foster
To Imogen
About the author
Chapter 9 Objects
Introduction
1 Running JavaScript
What is JavaScript?
JavaScript origins
Tools
Getting Git
What is HTML?
Display symbols
Create headings
Using a button
Egg timer
Text alignment
Cascading styles
Variables in programs
JavaScript identifiers
Performing calculations
Making applications
Adding comments
Global variables
Boolean thinking
Boolean expressions
Logical operators
The if construction
App development
7 Creating functions
Arrays of arguments
8 Storing data
Collections of data
Creating an array
9 Objects
Prototype HTML
Prototype JavaScript
Storing contact details
Finding contacts
Displaying contacts
Saving a contact
Finding a contact
Catching exceptions
Class design
Object-oriented design
Data storage
11 Creating applications
Data analysis
Node.js
12 Creating games
Canvas coordinates
Computer art
Animate images
Control gameplay
Window events
Control object position with a keyboard
Game sprites
Add sound
Adding scores
Programming is the most creative thing you can learn how to do. Why? If
you learn to paint, you can create pictures. If you learn to play the violin,
you can make music, but if you learn to program, you can create entirely
new experiences (and you can make pictures and music, too, if you want).
Once you’ve started on the programming path, there’s no limit to where
you can go. There are always new devices, technologies, and
marketplaces where you can use your programming skills.
But first, a word of warning: I would not say that learning to write
programs is easy. This is for two reasons:
If I tell you that it’s easy, and you still can’t do it, you might feel bad about
this (and rather cross with me).
If I tell you it’s easy and you manage to do it, you might think that it isn’t
worth doing.
Learning to program is not easy. It’s a kind of difficult that you might not
have seen before. Programming is all about detail and sequencing. You
must learn how the computer does things and how to express what you
want it to do.
Imagine that you were lucky enough to be able to afford your own
personal chef. At the start, you would have to explain things like, “If it is
sunny outside, I like orange juice and a grapefruit for breakfast, but if it is
raining, I’d like a bowl of porridge and a big mug of coffee.” Occasionally,
your chef would make mistakes. Perhaps you would get a black coffee
rather than the latte that you wanted. However, over time, you would add
more detail to your instructions until your chef knew exactly what to do.
A computer is like a chef who doesn’t even know how to cook. Rather
than saying “make me a coffee,” you would have to say, “Take the brown
powder from the coffee bag and add it to hot water.” Then you would
have to explain how to make hot water and how you must be careful with
the kettle and so on. This is hard work.
It turns out that the key to success as a programmer is much the same as
for many other endeavors. To become a world-renowned violin player,
you will have to practice a lot. The same is true for programming. You
must spend a lot of time working on your programs to acquire code-
writing skills. However, the good news is that just as a violin player really
enjoys making the instrument sing, making a computer do exactly what
you want turns out to be a very rewarding experience. It gets even more
enjoyable when you see other people using programs that you’ve written
and finding them useful and fun to use.
I’ve organized this book in three parts. Each part builds on the previous
one with the aim of turning you into a successful programmer. We start
off discovering the environment in which JavaScript programs run. Then
we learn the fundamentals of programming and we finish by making
some properly useful (and fun) programs.
Part 2 describes the features of the JavaScript that you use to create
programs that work on data. You will pick up some fundamental
programming skills that apply to a wide range of other languages and
that get you thinking about what it is that programs actually do. You’ll
find out how to break large programs into smaller elements and how you
can create custom data types that reflect the specific problem being
solved.
Now that you can make JavaScript programs, it’s time to have some fun
with them. You’ll discover how to create good-looking applications, learn
how to make programs that are secure and reliable, and finish off with a
bit of game development.
In each chapter, I will tell you a bit more about programming. I’ll show
you how to do something, and then I’ll invite you to make something of
your own by using what you’ve learned. You’ll never be more than a page
or so away from doing something or making something unique and
personal. After that, it’s up to you to make something amazing!
You can read the book straight through if you like, but you’ll learn much
more if you slow down and work with the practical parts along the way.
Like learning to ride a bicycle, you’ll learn by doing. You must put in the
time and practice to learn how to do it. But this book will give you the
knowledge and confidence to try your hand at programming, and it will
also be around to help you if your programming doesn’t turn out as you
expected. Here are some elements in the book that will help you learn by
doing:
Yes, the best way to learn things is by doing, so you’ll find “Make
Something Happen” elements throughout the text. These elements offer
ways for you to practice your programming skills. Each starts with an
example and then introduces some steps you can try on your own.
Everything you create will run on Windows, macOS, or Linux.
CODE ANALYSIS
If you don’t already know that programs can fail, you’ll learn this hard
lesson soon after you begin writing your first program. To help you deal
with this in advance, I’ve included “What Could Go Wrong” elements,
which anticipate problems you might have and provide solutions to those
problems. For example, when I introduce something new, I’ll sometimes
spend some time considering how it can fail and what you need to worry
about when you use the new feature.
PROGRAMMER’S POINTS
I’ve spent a lot of time teaching programming, but I’ve also written many
programs and sold a few to paying customers. I’ve learned some things
the hard way that I really wish I’d known at the start. The aim of
“Programmer’s Points” is to give you this information up front so that you
can start taking a professional view of software development as you learn
how to do it.
You’ll need a computer and some software to work with the programs in
this book. I’m afraid I can’t provide you with a computer, but in the first
chapter, you’ll find out how you can get started with nothing more than a
computer and a web browser. Later, you’ll discover how to use the Visual
Studio Code editor to create JavaScript programs.
Using a PC or laptop
You can use Windows, macOS, or Linux to create and run the programs in
the text. Your PC doesn’t have to be particularly powerful, but these are
the minimum specifications I’d recommend:
Using a Raspberry Pi
If you want to get started in the most inexpensive way possible, you can
use a Raspberry Pi running the Raspbian operating system. This has a
Chromium-compatible browser and is also capable of running Visual
Studio Code.
Downloads
In every chapter in this book, I’ll demonstrate and explain programs that
teach you how to begin to program—and you can then use that code to
create programs of your own. I’ve made a few video walkthroughs for
some crucial tasks. The book text will contain screenshots that you can
use, but these can go out of date. Follow the links to the walkthroughs to
get the latest steps to follow. You can download the book’s sample code
and video walkthroughs from the following page:
MicrosoftPressStore.com/BeginCodeJavaScript/downloads
www.begintocodewithjavascript.com
Video walkthroughs
https://bit.ly/3wEn6zX
Acknowledgments
Thanks to Mary for the cups of tea and Immy for the distraction.
We’ve made every effort to ensure the accuracy of this book and its
companion content. You can access updates to this book—in the form of a
list of submitted errata and their related corrections—at
MicrosoftPressStore.com/BeginCodeJavaScript/errata
http://www.MicrosoftPressStore.com/Support
Please note that product support for Microsoft software and hardware is
not offered through the previous addresses. For help with Microsoft
Random documents with unrelated
content Scribd suggests to you:
“It answers very well to begin with; but I have a regular
locomotive and two cars in process of building, and I shall have
them on the track this fall.”
“Is it a big locomotive?” I asked, curiously.
“No, it’s a small one; and it will be the prettiest plaything you
ever saw. I’m determined that the Toppleton Institute shall be the
most popular one in the country.”
“I suppose Colonel Wimpleton will do something to offset this
movement on your part,” I suggested.
“What can he do?” asked the major, anxiously. “Have you heard
of anything?”
“No, sir. I only know they feel very bad about the Lake Shore
Railroad over there.”
“They will feel worse before we get through with it,” replied the
magnate, shaking his head. “What can they do? They can’t build a
railroad, the country is so rough. We can keep ahead of them now.
But I want that dummy in motion. You must run it every half hour
for the rest of the day between Middleport and Spangleport. Carry
everybody who wishes to ride. I want the Centreport people to see
it, and to know that we are alive on this side.”
“Will the students be with me?” I inquired.
“This afternoon, when they are dismissed from the school-room,
they will be. I will send you a conductor. Let me see; Higgins is too
sick to study, and just sick enough to play. He shall run with you.
Now keep her going, as though you meant business.”
“I will, sir; I will put her through by daylight,” I replied, as I left
the library.
CHAPTER XX.
THE BEAUTIFUL PASSENGER.
I have been so busy with the history of our family affairs, and the
incidents which sent me over to Middleport, that I have not had
much to say about the Lake Shore Railroad; but before I have done
with the subject, I shall fully describe the road, and explain the
operations of the company. Only a small portion of the line had yet
been built, and the dummy was but a temporary substitute for more
complete rolling stock. Major Toppleton intended to have a charter
for the road, to be obtained at the next session of the legislature,
and to continue it to Ucayga. Although it was at the present time a
mere plaything for the students, it was designed to be a useful
institution, and to build up Middleport immensely in the end.
Just as I was about to start on the one o’clock trip, Major
Toppleton presented himself. The car was filled with students,
though a number of ladies and gentlemen had come down to the
station to have a ride in the dummy. The major immediately ordered
the boys to evacuate the premises, which they did with some
grumblings, amounting almost to rebellion. The persons waiting
were invited to get in, and I started for Spangleport with a less noisy
crowd than I had anticipated. As we went off, I heard the major call
the students together, and I concluded that he had some definite
plan to carry out.
On my return, I found the boys had loaded up the two platform
cars with rails and sleepers, and they were attached to the dummy
as soon as she arrived. Several mechanics were standing by, and it
was evident that a piece of work was to be done that day, instead of
play.
“Now, Wolf, we will run a construction train on this trip,” said
Major Toppleton, as he took his place at my side on the dummy, and
directed the students and the mechanics to load themselves into the
passenger apartment and on the cars.
“I think we need a little more construction at Spangleport, sir,” I
suggested.
“Why, what’s the matter?”
“I don’t like to run backwards, sir, on the down trips.”
“But a turn-table will cost too much for the short time we shall
make Spangleport a terminus. We will build one at Grass Springs, for
that will be as far as we shall run the road this season.”
“We need not build a turn-table, sir,” I added. “We can turn the
dummy on switches.”
“How is that?” inquired the major.
“It will take three switches to turn her. First run a track round a
curve to the right, until it comes to a right angle with the main line.
Then run another track on the reverse curve till it strikes the main
line again, a few rods from the point where the first track leaves it.”
“I don’t understand it.”
“I will explain it when we stop, sir. It will not take long to lay it
down, and when it is no longer wanted it can be taken up, and put
down in another place.”
At Spangleport, where we stopped, I made a diagram on a piece
of paper, to illustrate my plan; and here is a copy of my drawing.
The perpendicular lines are the main track. The dummy was to be
switched off at the lowest part of the diagram, and run on the curve
till it had passed a switch on the right. Then it was to be switched on
the upper curve, and run back till it passed the switch on the main
line, which being shifted, the car having been turned entirely round,
it runs back on the perpendicular lines between the curves.
Major Toppleton was satisfied with the scheme, directed that the
switches should be brought up, and the work was commenced at
once by the mechanics. All the boys but two were employed in
laying down more track; but I am sorry to say they grumbled
fiercely, for they wanted to have some fun with the dummy. Higgins
was still to serve as conductor, and the other student who had been
excepted from hard labor was one of the regularly appointed
engineers of the road. His name was Faxon. He had some taste for
mechanics, and had distinguished himself in school by making a fine
diagram of the steam-engine on the blackboard. He was to run with
me on the dummy, and learn to manage the engine. I was directed
to post him up, as well as I could, and to permit him to take an
active part in running the machine.
I was not particularly pleased with the idea of an apprentice in
the engine-room with me, for if the fellow had any “gumption” he
would soon be able to take my place, and I might be discharged
whenever it was convenient. But a second thought assured me that
my fears were mean and unworthy; that I could never succeed in
making myself useful by keeping others in ignorance. The students
were sent to the Institute to learn, and the railroad was a part of
their means of instruction. I had no right to be selfish.
We ran down to the wharf in Spangleport, for the road was built
half a mile beyond the village, when Higgins shouted, “All aboard for
Middleport!” We had quite a crowd of Spangleporters as passengers,
and we ran our trips regularly till five o’clock, to the great
gratification of the people of both places, when the gentlemanly
conductor declined to receive any more who expected to return, as
the half-past five car up would be a construction train. Mr. Higgins
talked very glibly and professionally by this time, and imitated all the
gentlemanly conductors he had ever seen.
Faxon was a very good fellow, though he cherished a bitter
antipathy against the Wimpletonians, and everything connected with
them. He was an ardent admirer of Major Toppleton, and particularly
of Major Toppleton’s eldest daughter, for which I did not like him any
the less, strange as it may appear after the developments of the last
chapter.
“I’ll tell you what it is, Wolf,” said he, as we were running up the
last trip, “this thing won’t go down with the fellows.”
“What?”
“All the fellows are mad because they had to work this
afternoon.”
“I thought they considered it fun to build the road.”
“They did before the dummy came; but now they want the fun of
the thing. They are all rich men’s sons, and they won’t stand it to
work like Irish laborers. I hope there won’t be any row.”
“Of course Major Toppleton knows what he is about.”
“The students don’t growl before him. They do it to the teachers,
who dare not say their souls are their own.”
“But the major told me the boys enjoyed the fun, and insisted
upon building the road themselves when he wanted to employ
laborers for the purpose.”
“That’s played out. I heard some of the fellows say they would
not work another day.”
“Some one ought to tell the major about this. He don’t want
them to work if they don’t like it,” I suggested.
“It was fine fun when we first began to dig, and lay rails, but we
have all got about enough of it.”
“I will speak to the major about it.”
“Don’t say anything to-day,” interposed Faxon. “The students are
vexed because they were not allowed to have a good time this
afternoon; but the major is going to have a great picnic at Sandy
Shore next week, and he is in a hurry to have the road built to that
point—two miles beyond Spangleport.”
“There is only one mile more to build, and if the fellows stick to it
they will get it done.”
“But they say they won’t work another day,” replied Faxon.
Middleport was not paradise any more than Centreport. Boys
were just as foolish and just as willing to get into a scrape, on one
side as the other. The Toppletonians had insisted upon doing the
work of building the road, and then purposed to rebel because they
were required to do it. I had heard of the grand picnic which was to
take place on the occasion of the birthday of Miss Grace Toppleton.
The grove by the Sandy Shore could be reached most conveniently
by the railroad, and the major’s anxiety to have the rails laid to that
point had induced him to drive the work, instead of giving the
students a chance to have a good time with the dummy, as they had
desired to do while it was a new thing.
We ran into the engine-house, and some of the boys forced their
way into my quarters, in spite of my protest. I saw a couple of them
studying the machinery with deep interest. They asked me some
questions; and supposing they were only gratifying a reasonable
curiosity, I gave them all the information they needed, telling them
just how to manage the engine.
“Pooh! I can do that as well as anybody,” said Briscoe, as he
jumped down.
“Of course you can,” replied one of his companions.
“Don’t you think I could run her, Wolf?” asked Briscoe. “I am one
of the engineers of the road, and I ought to know how.”
“Probably you could after you had had some experience.”
They went away, and I wondered what they were thinking about.
It did not much matter, however, for I was satisfied that the major
would not permit them to run the engine till they had become
thoroughly competent to do so. I put out the fires in the dummy,
cleaned the machinery, and left her in readiness for use the next
morning. I then went to the mills; and, as my father had finished his
day’s work, we walked down to the wharf where my skiff lay. On the
way I told him about my interview with Colonel Wimpleton, and we
both enjoyed the great man’s confusion when he learned in what
manner he had punished my father.
“He will not arrest you, Wolf; you may depend upon that,” said
my father. “As the case now stands, we have the weather-gauge on
him, except in the matter of the mortgage. I am afraid I shall lose all
I have in the house. Mortimer has got back, but he hasn’t seen or
heard of Christy.”
“He may turn up yet.”
“He may, but I don’t depend much upon it. I have tried a little
here in Middleport to raise the money to pay off the mortgage; but
people here will not lend anything on real estate on the other side of
the lake.”
“Perhaps Major Toppleton will help you out,” I suggested.
“I don’t like to say anything to him about it. He has done well by
me, and I won’t ride a free horse to death; besides, I don’t want to
be in the power of either one of these rich men. I have had trouble
enough on the other side.”
I pulled across the lake, and we went into the house. My mother
looked anxiously at my father as he entered, and then at me. I
smiled, and she understood me. Father had not drunk a drop, and
she was happy. We never relished our supper any better than we did
that night, and I went to bed early, not a little surprised that we
heard nothing, during the evening, of Colonel Wimpleton and his
son.
The dummy was to make her first trip at eight o’clock, and I left
the house at half-past six, with my father, to cross the lake. When
we reached the wharf, I was utterly confounded to see the dummy
streaking it at the rate of twenty miles an hour along the opposite
shore of the lake. Something was wrong, for there was no one on
the other side who knew how to run the machine, unless it was
Faxon, and I was afraid the discontented Toppletonians were in
mischief. We embarked in the skiff, and I pulled over as quickly as I
had done the day before.
CHAPTER XXIII.
OFF THE TRACK.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebooknice.com