Brought to you by Galvanize. Learn more about the way we teach code at galvanize.com.
In this course, we'll be going over the following!
- Basic syntax of JavaScript
- Variables and Functions
- Conditional statements (if, else if, else)
- Build a “Rock, Paper, Scissors” application
While not required, reviewing our HTML & CSS course can help!
For this workshop, you need to have the following:
- Install a text editor! We recommend Atom.io
- Have an updated web browser! We recommend Google Chrome
Making its first appearance in 1995, JavaScript was created by an engineer at Netscape to provide a user-friendly, lightweight programming language that can be easily adapted for the rise of the Internet. Now, with HTML and CSS, it is one of the core languages of the Internet and is growing quickly to accommodate beyond the client-side.
JavaScript allows web pages to do more than just “sit there." You can animate, calculate, etc. - you can do it all! It is a great programming bridge between “design” and “development” that allows for endless creativity.
Common confusion: JavaScript is NOT Java. They are largely different programming languages and should not be confused with one another.
In order to go over some basic JavaScript concepts, let's follow a tutorial provided by the JavaScript.com team.
It's only 8 lessons and takes less than 5 minutes.
https://www.javascript.com/try
Let's review some of the basic syntax of JavaScript.
var
- defines a variable (an object of some value);
- terminator, commonly found at the end of a code operation"word"
- quotes create strings, which are a form of datafunction()
- performs some action or method{}
- block notation, contains code that can be initialized by a function.
- dot notation, allows for the chaining of variables and functions
JavaScript is an Object-Oriented Language, a common paradigm of coding that occurs in many other languages and can help you learn them as well.
Variables are essentially containers for storing data, values, etc.
In JavaScript, you must declare them with var
first, then define them with =
.
Syntax:
var price1 = 5;
var price2 = 6;
var total = price1 + price2;
What is the value of total
?
Variables can store a variety of data types:
- Strings -
“Hello, my name is Lee.”
- Numbers -
40, 0.15
- Boolean -
True
orFalse
- Null - literally nothing
- “” - undefined value
- Functions - here we go…!
What's the difference between =
,==
, and ===
? I see this all the time.
#####=
is known as the assignment operator
It sets variables equal to a specific value.
var foo = 1
It compares two items to see if they are of equal value, but it ignores if they are the same exact type of data.
“1” == 1 => True
Null == undefined => True
It compares the value & type of the items to see if they are exactly the same. In the case of null vs undefined, null is more specifically typed than undefined, so they are not exactly the same value.
“1” === 1 => False
Null === undefined => False
Functions are blocks of code that perform tasks for us.
In JavaScript, you follow the general syntax: 1) declare, 2) define, 3) call (invoke).
Syntax:
var multiply = function(a,b){
return a * b
};
multiply(2,4);
~What is the value produced by this function?
- Parameters -
(a,b,c)
- hypothetically what passes through the function - Arguments - real values of the parameters the function affects
- Block -
{...}
- the function’s operational code - Return command - the output of the function
Remember Choose Your Own Adventure books?
Conditional statements work a lot like them and follow the basic format: if, else, else if...
if - if what’s in the parameters is True
, then a block of code will run.
If it’s False
, the code will not run.
if (hour < 18) {
greeting = "Good day";
}
if statements by themselves default to True
.
else - what if you wanted the code to do something else besides nothing if it’s False?
if (hour < 18) {
greeting = "Good day";
} else
{ greeting = “Go away.”;
}
else if - What if another scenario comes up?
Add an else if
in between the if
and else
statements.
if (hour < 18)
{greeting = "Good day";}
else if (hour < 9)
{greeting = “OK day”;}
else {greeting = “Go away.”;}
This code is actually broken. Can you figure out why?
- If statements perform an action if the statement is True
- Else statements perform an action if the statement is False
- Else if statements perform an action if the first is False but the second is True Is there any other way to do this?
Time for us to make our Rock, Paper, Scissors application!
- Go to: https://github.com/GalvanizeOpenSource/Learn-To-Code-JavaScript/
- Download the zip file of our code
- Open the files in your text editor 4. index.html 5. CSS/style.css
- Open the index.html file in your web browser
- Get the user's choice
- Get the computer's choice
- Teach the computer how to guess rock, paper, or scissors
- Compare their choices and tell the user the result
Assign a prompt
method to the variable userChoice
:
The prompt
method gets input from the user, prompt
has an optional message parameter which you can use to ask the user for a response.
var userChoice = prompt("Do you choose rock, paper or scissors?");
This line creates a variable called userChoice
to represent the users response.
Question: Why is this a terrible way to get user input?
Assign a Math.random()
method to the variable computerChoice
:
What is Math
in JavaScript?
Math.random()
returns a random floating point number between 0 and 1.
var computerChoice = Math.random();
Here we are setting a variable named computerChoice
to the result of Math.random()
.
Question: How else can we get a random choice?
This is our first conditional statement.
We change the value of computerChoice
to either rock, paper, or scissors depending on what number the computerChoice
variable gets set to when we run the program.
Computers don't speak English (well, not exactly), so we need to speak in a language they understand: numbers.
if (computerChoice <= 0.33) {
computerChoice = "rock";
} else if (computerChoice <= 0.66) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
At this point the computer is ready to rumble with it's choice, and the user has made theirs.
IT'S GO TIME!!! (Not so fast, bub.) First we need to tell the computer how to decide who wins. In order to do that, we're going to need to create a function!
Here we're creating a function called compare
. The compare
function takes two
arguments choice1
and choice2
.
var compare = function(userChoice, computerChoice) {
if (userChoice === computerChoice) {
window.alert("The result is a tie!");
} else if(userChoice === "rock") {
if (computerChoice === "scissors") {
window.alert("Rock wins!");
} else {
window.alert("Paper wins");
}
} else if(userChoice === "paper") {
if(computerChoice === "rock") {
window.alert("paper wins!");
} else {
window.alert("scissors wins!");
}
} else if(userChoice === "scissors") {
if (computerChoice === "rock") {
window.alert("Rock wins");
} else {
window.alert("scissors wins");
}
}
};
We're passing values of userChoice and computerChoice to run the equation.
The function is called when someone clicks the button via the onclick
attribute!
<button class="button" onclick="compare(userChoice, computerChoice);">LETS PLAY RPS!</button>
- "I want to play the game again. Make a button I can press to play again!"
- "When I win, I want the game to congratulate me by my name!"
- "I don't ever want to lose. Make it so I always win."
- "I want the JavaScript code to work on other HTML files. How do I do this?"
Want to learn more? Visit one of our nearby Learn to Code sessions or check out our other tutorials:
You can also check out our evening courses at galvanize.com/workshops:
- Zero to Web Designer in 8 Weeks
- Foundations in JavaScript in 8 Weeks
If you're ready for a full-fledged immersive program, Galvanize offers the following:
Galvanize Full Stack Immersive Program
- 24 Week Full-Time Program
- 97% Job Placement Rate within six months
- Average starting salary: $77,000 per annum
- Scholarships available for those who qualify
To learn more, email our enrollment department at enrollment@galvanize.com.
Graham McBain is an alum of the 3rd cohort of the Galvanzie Web Development Program. He previously served as Evangelist for Galvanize based in the Colorado area. Graham believes that programming is more accessible than people think and is passionate about showing people the path to becoming a developer.
Lee Ngo is an evangelist for Galvanize based in Seattle. Previously he worked for UP Global (now Techstars) and founded his own ed-tech company in Pittsburgh, PA. Lee believes in learning by doing, engaging and sharing, and he teaches code through a combination of visual communication, teamwork, and project-oriented learning.
You can email him at lee.ngo@galvanize.com for any further questions.