Professional Documents
Culture Documents
Programming for Absolute Beginners: Using the JavaScript Programming Language 1st Edition Jonathan Bartlett full chapter instant download
Programming for Absolute Beginners: Using the JavaScript Programming Language 1st Edition Jonathan Bartlett full chapter instant download
https://ebookmass.com/product/modern-c-for-absolute-beginners-a-
friendly-introduction-to-the-c-programming-language-2nd-edition-
slobodan-dmitrovic/
https://ebookmass.com/product/javascript-crash-course-the-
complete-beginners-guide-to-learning-javascript-programming-in-
no-time-angularjs-jquery-javascript-visually-nathaniel-hill/
https://ebookmass.com/product/modern-c-for-absolute-beginners-a-
friendly-introduction-to-the-c-programming-language-
and-c11-to-c23-standards-2nd-edition-slobodan-dmitrovic/
https://ebookmass.com/product/the-awk-programming-language-2nd-
edition-aho/
C Programming Absolute Beginner’s Guide (3rd Edition)
Perry
https://ebookmass.com/product/c-programming-absolute-beginners-
guide-3rd-edition-perry/
https://ebookmass.com/product/go-programming-language-for-
dummies-1st-edition-wei-meng-lee/
https://ebookmass.com/product/c-programming-for-beginners-by-
sett-john-hafeez-abdulmutolib/
https://ebookmass.com/product/coding-for-kids-5-books-
in-1-javascript-python-and-c-guide-for-kids-and-beginners-coding-
for-absolute-beginners-mather/
https://ebookmass.com/product/c-programming-language-c-
programming-lang-_p2-2nd-edition-ebook-pdf/
Programming for
Absolute Beginners
Using the JavaScript
Programming Language
Jonathan Bartlett
Programming for Absolute Beginners: Using the JavaScript Programming Language
Jonathan Bartlett
Tulsa, OK, USA
—Linus Torvalds
Table of Contents
About the Author����������������������������������������������������������������������������������������������������� xi
Acknowledgments���������������������������������������������������������������������������������������������������xv
Chapter 1: Introduction�������������������������������������������������������������������������������������������� 1
1.1 What You Will Learn����������������������������������������������������������������������������������������������������������������� 1
1.2 How to Use This Book������������������������������������������������������������������������������������������������������������� 3
1.3 For Younger Programmers������������������������������������������������������������������������������������������������������ 4
v
Table of Contents
vii
Table of Contents
Chapter 11: Grouping Values Together with Objects and Arrays�������������������������� 145
11.1 A Basic Introduction to Objects����������������������������������������������������������������������������������������� 146
11.2 Simplifying Object Creation����������������������������������������������������������������������������������������������� 149
11.3 Storing Sequences of Values Using Arrays����������������������������������������������������������������������� 150
11.4 Using Arrays in Programs�������������������������������������������������������������������������������������������������� 151
11.5 Mixing Objects and Arrays������������������������������������������������������������������������������������������������ 154
11.6 Object Methods����������������������������������������������������������������������������������������������������������������� 156
11.6.1 Review���������������������������������������������������������������������������������������������������������������������� 159
11.6.2 Apply What You Have Learned���������������������������������������������������������������������������������� 160
viii
Table of Contents
ix
Table of Contents
Index��������������������������������������������������������������������������������������������������������������������� 323
x
About the Author
Jonathan Bartlett is a software developer, researcher, and
writer. His first book, Programming from the Ground Up, has
been required reading in computer science programs from
DeVry to Princeton. He has been the sole or lead author for
eight books on topics ranging from computer programming
to calculus. He is a senior software developer for McElroy
Manufacturing, spearheading projects in web, mobile, and
embedded software. He is now the author of several Apress
books including Electronics for Beginners and more.
xi
About the Technical Reviewer
Germán González-Morris is a polyglot software architect/engineer with more than 20
years in the field, with knowledge in Java(EE), Spring, Haskell, C, Python, and JavaScript,
among others. He works with web-distributed applications. Germán loves math
puzzles (including reading Knuth) and swimming. He has tech-reviewed several books,
including an application container book (Weblogic), as well as titles covering various
programming languages (Haskell, Typescript, WebAssembly, Math for coders, and
regexp). You can find more details at his blog site (https://devwebcl.blogspot.com/)
or Twitter account (@devwebcl).
xiii
Acknowledgments
I want to take a moment and thank everyone who helped me write this book. First, I want
to thank those who read and appreciated my first programming book, Programming
from the Ground Up. The encouragement I received from that book has given me the
encouragement to continue writing and educating throughout the years.
Next, I want to thank my homeschool summer co-op class for being guinea pigs
for this material. Your questions, your successes, and your difficulties all informed the
writing of this book. You were both my motivation to write in the first place and the first
proving ground for the material.
I would also like to thank my family, my friends, and my church, all of whom are
essential parts of my life. Thanks especially to my wife who puts up with me when I am
too focused on my writing to notice what the kids have been up to or to put a stop to
whatever trouble they have found themselves in!
xv
CHAPTER 1
Introduction
The modern world is filled with computers. Computers run our phones, our cars, and
even our refrigerators. Computers manage our businesses, our calendars, and our social
lives. With the world relying on computers for so many functions, it is important to know
how these devices work. Even if you never need to program a computer yourself, chances
are that, at some point in your life, you will be involved with software development.
You may be an accountant who needs to tell a computer programmer how you want
your purchasing system set up. You may be an engineer who needs to describe your
engineering process so that a programmer can automate it. In all such tasks as these, it
is important to know something about how computers are programmed, even if you are
not personally writing the software.
1
© Jonathan Bartlett 2023
J. Bartlett, Programming for Absolute Beginners, https://doi.org/10.1007/978-1-4842-8751-4_1
Chapter 1 Introduction
hired people for my development team who already knew the programming language
that my team uses. If someone learns one programming language and practices until
they are good at it, then the effort to learn a new language is fairly minimal.
You may wonder why, if the languages are so similar, there are so many
programming languages to choose from. The fact is, when engineering anything, trade-
offs have to be made. Sometimes in order to make one type of task easier, another type
of task has to be made harder. In my kitchen I have both a mixer and a blender. Both of
them operate on the same basic principles—you put food into the main container area,
an electric motor turns, and some attachment combines the food together. While these
tasks are very similar and operate on the same principles, there are many types of food
in the world and many ways that they need to be mixed, such that the mixer works better
for some tasks and the blender for others. Similarly, with programming languages, some
of them are better suited to different tasks. Also, the choice of programming language
is dependent on the programmer. Just as different types of cars suit the preferences and
tendencies of different types of drivers, so do different programming languages suit the
preferences and tendencies of different types of programmers. Because of these reasons,
there are numerous programming languages available for nearly any task you might
want to perform.
The programming language covered in this book is called JavaScript. I like to teach
JavaScript as a first language for several reasons. First of all, JavaScript was developed
to be a first language. One of the goals of the language was to make it easy for new
programmers to get started quickly. Even though JavaScript was designed to make
programming easier for new programmers, it is not any less powerful as a language.
Second, JavaScript has become the de facto programming language for website
interfaces. If you use a website that does anything besides link to other web pages,
JavaScript is probably involved. Therefore, learning JavaScript will have immediate
practical benefits in learning how the Web operates. Third, the tools for programming
JavaScript are available on every computer. You don’t need to download any special tools
to program JavaScript. If you have a computer with a web browser, you can program
JavaScript! Finally, JavaScript is very similar to other popular programming languages
such as C#, Java, and Swift. Therefore, knowing JavaScript will not only be immediately
beneficial for programming websites, it is also a language that makes it easy to transition
to other popular systems.
This book is for the first-time programmer. No prior programming experience is
assumed. This book does assume that you have a basic understanding of how to use your
computer and browse the Internet. That is all that you need!
2
Chapter 1 Introduction
You will learn not only the basics of computer programming but also a more general
knowledge of how computers and data work. You will learn where computers came
from, how they work, how computers work with data, how data is transmitted, and how
web pages work. This book will not go in-depth in all of these subjects, but it will give
you a basic working framework that will help you better understand ideas that you may
encounter elsewhere.
When discussing smaller pieces of code within a paragraph, code that is under
discussion will look like this.
Now, there are many different types of computers, each with different operating
systems and software loaded on them, with each of those having different versions.
There are also numerous different web browsers, each with different features available
and slightly different ways of working. This book attempts to walk you through setting
everything up on each operating system. If there is anything in this book that depends
on the specific operating system or browser that you are using, Appendix B has the steps
for several different systems, including Windows and Mac operating systems. This book
will refer you to the appropriate section of the Appendix when needed. Though this
book works with any modern web browser (basically anything released after 2008), I
recommend that you use Google Chrome. As of the time of this writing, Google Chrome
is the easiest browser to work with as a programmer. That being said, you should be just
fine with any web browser, including Brave, Firefox, Safari, Chrome, Opera, or Edge.
3
Chapter 1 Introduction
This book contains several practice questions and practice activities. The goal of
these questions and activities is to provide you with a hands-on way of understanding
the material. By doing the questions and activities, the text will become much more
meaningful and understandable. More importantly, they might show you the places
where you did not fully understand the text. Many people have a tendency to skip over
things if they don’t understand them well. Practice questions and activities give you a
chance to slow down and make sure you know which parts you understood and which
parts you need to read again and spend time thinking about. Practice questions build
on each other, so by doing them all in the order given, you can see exactly where you are
having problems.
At the end of every chapter is a review section which covers the most important
concepts of each chapter. After that is a section to help you practice applying your
knowledge to problems. These questions require you to further engage your brain and
really think about what you learned in that chapter and what it means.
Appendix A contains an extended glossary of terms used in this book, plus others
you are likely to encounter when reading about programming. This chapter will help you
find your bearings as you read and talk with other people about programming. I would
suggest that, concurrent with your readings, you also take the time to look through the
glossary for words that you may have heard but did not understand at the time.
Also, if you run into problems when writing code, Section B.6 has several suggestions
for getting you back on the right track.
4
CHAPTER 2
A Short History
of Computers
The history of computers is weird and wonderful. What started as an abstract
philosophical quest ended up setting the course for society for over a century and
continues to be one of the most profound parts of modern life. The goal of this chapter is
to trace an outline of where computing started, where it has been, and where it is now.
7
© Jonathan Bartlett 2023
J. Bartlett, Programming for Absolute Beginners, https://doi.org/10.1007/978-1-4842-8751-4_2
Chapter 2 A Short History of Computers
to being near streams but could now go anywhere, since the power could be generated
from fire and stored water. Eventually this even allowed the creation of trains, since the
power could move with the vehicle.
The other invention of the industrial revolution was interchangeable parts.
This allowed a standardization and maintenance of equipment that was previously
unattainable. Instead of having each part be a unique piece, the parts became
standardized which allowed for the machines to become more specialized. It is one
of the more curious paradoxes of technology that as the pieces of technology become
less unique, the more advanced and unique the systems created from those parts can
become. Standardization allows for users of technology to stop having to think about all
of the low-level decisions and focus on the larger, more meaningful decisions. This also
allows for better communication about systems, because the parts can be more readily
described. If I can give you a schematic that lists premade parts, it is much easier to
design and communicate that design than if I also had to describe how each individual
part was supposed to be made.
So the introduction of available powered machinery and standardized parts in the
industrial revolution led to an explosion of specialized machines. We then had machines
to perform any number of tasks that a person could want to do. The next step was the
introduction of machines which were directed not by people directly controlling the
machine but by coded instructions. The earliest of these machines was the Jacquard
Loom, which used punched cards to signify a pattern woven into a fabric. The cards had
punched holes to signify to the machine the raising or lowering of the particular thread
causing it to be visible or hidden in the pattern. Thus, the loom could be programmed
to make a pattern by specifying at each point whether each thread should be raised or
lowered.
Later inventions applied this concept to mathematics. Calculating machines had
been around for a long time, with Blaise Pascal’s mechanical calculator having been
invented in the mid-1600s. However, this required the power of physical manipulation
to actually accomplish the addition. Most mathematical tasks are not single-step like
addition but require a process of several steps, sometimes repeating steps, before finding
an answer. Charles Babbage invented a more advanced machine to perform navigational
calculations. In this machine, the user entered the input, and then the machine used
that input to run a series of steps which eventually yielded results. Babbage eventually
designed a machine that could take a list of arbitrary instructions much like a modern
computer, but he was never able to build that design.
8
Chapter 2 A Short History of Computers
Once humans had the ability to power a machine, create a machine that operated
on external instructions, and use those instructions to perform mathematical functions,
they had all of the pieces in place to create a computer. However, the revolution that
brought about computing took place not from an invention, but from a problem in
philosophy.
9
Chapter 2 A Short History of Computers
1
If you want to dive deeper into this subject, you can see my article in MindMatters, “Why I Doubt
That AI Can Match the Human Mind,” available at https://bit.ly/3FOtOsS. You may also want
to check out my YouTube video, “How to Build an Artificial Intelligence Using the Doctrine of
Man,” available at https://youtu.be/FzXW7p3AG1Y.
10
Chapter 2 A Short History of Computers
and for the data that the program generates and operates on. This makes the computers
much easier to program and use, which led to the ability to sell computers commercially.
The first commercially available computer to implement this idea was the Manchester
Mark 1. The first mass-produced computer was the UNIVAC I, followed shortly after by
IBM’s 650. These computers were still massive in size but contained less memory storage
space than a single graphic on a modern computer. The UNIVAC I was the first computer
to have an external tape storage, and external disk storage (similar to modern hard
drives) followed soon after.
The next move for computer hardware was toward miniaturization. The original
computers used large devices called vacuum tubes to perform data processing (see
Figure 2-1, left column). These vacuum tubes would allow or not allow current to flow
based on whether other wires had current flowing through them or not. Combinations of
many of these tubes could allow for data to be stored as current flow, for mathematical
operations to be performed on such data, and for the data to be moved around.
After the vacuum tube came the invention of the transistor (see Figure 2-1, middle
column). Transistors generally have three wires, where the middle wire controls whether
the electricity can flow between the other two wires. As with vacuum tubes, transistors
can be wired together to create digital computer memory, digital computer logic
operations, and digital information pathways. Transistors, while they performed the
same basic functions as the vacuum tube, were able to do so in a much smaller package
and operating on a lot less power. Transistors allowed much smaller devices to be built
which also required almost 1,000 times less power. The Metrovick 950, released in 1956,
was the first commercial computer that operated on this principle.
Miniaturization continued with the advent of integrated circuits or what are often
called microchips or just chips (see Figure 2-1, right column). An integrated circuit
basically allows for miniaturized transistors to be stored on a small, single plate of
silicon. When integrated circuits were first introduced, they only had a few transistors.
Today, integrated circuits come in a variety of sizes, and the ones used for desktop
computing can hold billions of transistor equivalents on a 2-inch square chip. Integrated
circuits basically brought computers as we know them into the world. However, when
they were first introduced, they were primarily used by very large businesses.
In the 1960s, Douglas Engelbart led a research team to look at the future of
computing. In 1968, Engelbart presented what has been termed “the mother of all
demos,” which predicted and demonstrated nearly all aspects of modern personal
computing, including graphical interfaces, networking, email, video conferencing,
11
Chapter 2 A Short History of Computers
collaborative document editing, and the Web. This served as an inspiration for a number
of companies to start pushing to make this vision of computing a reality. Engelbart had
accomplished it in a lab, but others were needed to make it a commercial reality.
12
Chapter 2 A Short History of Computers
13
Chapter 2 A Short History of Computers
The 1990s witnessed the rise of Internet Service Providers, or ISPs, which provided
a way for computer users to use the modems that they used to use for bulletin-board
systems to connect their computers to the Internet. Instead of using a modem to connect
to a single computer, like they did with bulletin-board systems, the ISP allowed a user to
use their modem to connect to a whole network. This began the mass public adoption of
the Internet by both individuals and organizations of all stripes.
In the early days of the Internet, the speed of the network was very slow, and
only text could be transmitted quickly. Eventually, modems were replaced with more
advanced (and faster) ways of connecting to the Internet, such as DSL, cable, and fiber.
This allowed more and more complex content to be transmitted over the Internet. Also,
because these technologies do not tie up a phone line, they can be used continuously,
rather than intermittently. In addition, wireless technologies, such as WiFi and
cellular-based networking, allowed users to connect to the Internet without being tied
down by cables. These developments together led to the near-ubiquitous availability of
the Internet that we have today.
So, today, nearly all computer software is built with the network in mind. In fact,
much of the software that people use on a daily basis operates not on an individual
computer, but over a network. This allows for users to access software programs no
matter where they are or what computer they are using. It has also changed software
development so that the focus of computer software is no longer on individuals and
individual tasks but on organizing groups of people.
2.4.1 Review
In this chapter, we covered the basic history of computers. We have learned the
following:
• The isolated networks around the world were eventually unified into
a single Internet-work, known as the Internet.
15
Chapter 2 A Short History of Computers
16
CHAPTER 3
How Computers
Communicate
Before we start our study of computer programming, we are going to begin by studying
the way that computers communicate. The Internet is basically a giant communication
system. Communication systems operate using protocols. A protocol is a predefined
sequence of steps used to ensure proper communication.
We actually use protocols every day. Think about what happens when you answer
the phone. What do you say and why do you say it? Think about what happens when
you answer the phone (see Figure 3-1). This signals to the person calling us that we
have picked up the phone and we are ready to start talking. If we didn’t say “hello,” the
person might think that we accidentally accepted the call without knowing it or that we
are not quite ready to talk yet. Then, at the end of the call, we usually say something like,
“Thanks for calling! Goodbye!” This signals to the other person that we are done with the
conversation. If we didn’t tell them goodbye, they might think that we are still on the line
and continue talking. If they heard silence, they may presume that either we were not
speaking because we were upset or that there was a technical problem. Therefore, we
end our conversations with a “goodbye” to let the person we are talking to know that the
conversation is over.
This is the essence of a protocol. A communication protocol is a sequence of steps or
possible steps that enable two parties to communicate or interact and know the status of
the communication or interaction. Because computers cannot think or feel, computers
rely on very rigid and exact protocols to allow them to communicate with each other. In
fact, computers use hundreds of different protocols to communicate different types of
data in different ways. Most of the time, there are actually multiple protocols happening
at once.
17
© Jonathan Bartlett 2023
J. Bartlett, Programming for Absolute Beginners, https://doi.org/10.1007/978-1-4842-8751-4_3
Chapter 3 How Computers Communicate
Think about writing a letter. When you write a letter, there is a basic protocol that
governs the form of a letter—at minimum it should have a date, a greeting, and a closing.
However, if you decide to mail the letter, you have to send it through the mail service,
which has its own protocol. To send the letter through the mail, you need to take the
letter, fold it up, and put it in an envelope. What you write on the envelope is governed
by another protocol designed by the US Postal Service. Their protocol requires a return
address on the top left corner of the envelope, a destination address in the middle of the
envelope, and a stamp in the top right corner. Now you have two protocols happening
simultaneously—the letter-writing protocol and the envelope-addressing protocol.
These protocols are layered, which means that one of the protocols runs fully inside
of the other protocol. In computer jargon, we would say that the envelope protocol
encapsulates the letter protocol. The envelope protocol takes the results of the letter
protocol, packages it up, and puts its own protocol on top.
18
Chapter 3 How Computers Communicate
19
Another random document with
no related content on Scribd:
indisputably true, that the crude lymph is sufficient to give the
infection, and that there is no necessity to draw blood at the time of
incision. Add to this, the frequent tumour of the lymphatic glands in
the axilla soon after the operation.
From these premises, it seems rational to conclude, that the
general success of inoculation is chiefly to be attributed to our mixing
the fixed variolous ferment with the lymph on the surface of the body,
by which means the viscera, most essential to our existence, are
less affected, and the poison rendered less virulent by dilution. But
the wonderful success of the present practice of inoculation remains
yet to be accounted for.
I have told you, that the most malignant diseases may be rendered
less malignant by proper treatment. The malignancy of the small-pox
hath been considerably abated by inoculation, and that malignancy
hath been still farther diminished by the gradual introduction of the
anti-phlogistic and antiseptic plan. So that in all places where
inoculation hath been long practised, and the patients thus treated,
the small-pox will naturally become a mild disorder, and the ignorant
operators themselves will be surprised at their unexpected success.
This, gentlemen of the jury, I conceive to be a true picture of the
present state of inoculation in these kingdoms. You will now lay your
heads together, and weigh well the evidence you have heard. If you
are of opinion that the articles of the indictment have been
sufficiently proved; that the Prisoner hath in an especial manner
preserved the lives of his Majesty’s liege subjects, by secret
medicines and modes of practice unknown to all other practitioners,
you will then find him guilty. If on the contrary, you think that these
things are not true, and that this is a malicious prosecution, you will
in that case acquit him.
The jury having laid their heads together, without going out of
Court, were called over, and answered to their names.
Cl. of the Cr. Gentlemen of the jury, are you agreed in your
verdict?
Jury. Yes.
Cl. of the Cr. Who shall say for you?
Jury. Our foreman.
Cl. of the Cr. Daniel Sutton, hold up your hand. You of the jury,
look upon the Prisoner. How say you? Is Daniel Sutton guilty of the
high crimes and misdemeanors of which he stands indicted, or not
guilty?
Jury. Not guilty.
The Prisoner was acquitted, and discharged accordingly.
FINIS.
Transcriber’s Notes
A few minor errors in punctuation were fixed.
A few minor inconsistencies in the treatment of speaker names were fixed.
Page 8: “extreamly full” changed to “extremely full”
Page 16: The missing catchword “the” from the previous page was added before
“colour of Madeira wine”.
Page 22: “he ingredients” changed to “the ingredients”
Page 26: “gentletlemen” changed to “gentlemen”
Page 39 & 69: “administred” changed to “administered”
*** END OF THE PROJECT GUTENBERG EBOOK THE TRYAL OF
MR. DANIEL SUTTON, FOR THE HIGH CRIME OF PRESERVING
THE LIVES OF HIS MAJESTY'S LIEGE SUBJECTS, BY MEANS
OF INOCULATION ***
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the terms
of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
1.F.4. Except for the limited right of replacement or refund set forth in
paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.
Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.
Most people start at our website which has the main PG search
facility: www.gutenberg.org.