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 -
TrueorFalse - Null - literally nothing
- “” - undefined value
- Functions - here we go…!
What's the difference between =,==, and ===? I see this all the time.
It sets variables equal to a specific value.
var foo = 1It 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/Doubleshot1122/Learn-to-Code-HTML_CSS_JS-2
- Download the zip file of our code
- Open the files in your text editor
- index.html
- main.js
- 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.
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.
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 onclick="compare(userChoice, computerChoice);">LETS PLAY RPS!</button>- "How can I make this congratulate me by name?"
- "How could I play this against another person?"
- "How could I choose R, P, or S without an alert box?"
- "How want to keep track of the wins and loses?"
- Galvanize - Web Development Immersive
- Galvanize - Web Development Evenings
- Galvanize - Intro to Web Development Evenings
Thanks for attending.