X Tutup
Skip to content

kimschles/Learn-To-Code-JavaScript

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Learn to Code JavaScript!

Brought to you by Galvanize. Learn more about the way we teach code at galvanize.com.

Learning Objectives

By the end of this workshop, you will be able to...

  • Write JavaScript code in a text editor
  • Explore code using Chrome Developer Tools
  • Describe and use the following primitive data types:
    • Strings
    • Numbers
    • Booleans
  • Describe and use the following operators:
    • Assignment
    • Comparison
  • Write a JavaScript function
  • Use a conditional statement to control the flow of a program
  • Build a “Rock, Paper, Scissors” application using primitive data types, operations, functions and conditional statements

While not required, reviewing our HTML & CSS course can help!

Setting Up Your Computer

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

What IS JavaScript?

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.

A Quick Mini-Tutorial

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

Dive a Little Deeper

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 data
  • function() - 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

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 or False
  • Null - literally nothing
  • “” - undefined value
  • Functions - here we go…!

Check for Understanding!

What's the difference between =,==, and ===?

#####= is known as the assignment operator It sets variables equal to a specific value.

var foo = 1
== is known as the abstract equality comparison

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
=== is known as the strict equality comparison

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

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?

More on the syntax of functions:
  • 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

Conditional Statements

Remember Choose Your Own Adventure books?

Conditional statements work a lot like them and follow the basic format: if, else, else if...

If Statements

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 Statements

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 Statements

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?

Recap of Conditional Rules
  • 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?

LET'S CODE

Time for us to make our Rock, Paper, Scissors application!

  1. Go to the top of this page, click the green 'download' button and select DOWNLOAD ZIP
  2. Open the files in your text editor a. index.html b. CSS/style.css
  3. Open the index.html file in your web browser

5 Steps To Building This App

  1. Get the user's name
  2. Get the user's throw: either rock, paper, or scissors
  3. Have the computer generate a random number between 0.01-0.99.
  4. Use a conditional statement to assign rock, paper, and scissors to the computer's random number
  5. Compare the user's throw and the computer's throw, and tell the user who won
1. Get the user's name

Assign a prompt method to the variable userName: 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 userName = prompt("What is your name?");
2. Get the user's throw: either rock, paper, or scissors

Assign a prompt method to the variable userThrow:

var userThrow = prompt("Rock, Paper or Scissors?");

This line creates a variable called userThrow to represent the users response.

3. 1. Have the computer generate a random number between 0-1.

Assign a Math.random() method to the variable randomNum:

What is Math in JavaScript?

Math.random() returns a random floating point number between 0 and 1.

var randomNum = Math.random();

Here we are setting a variable named randomNum to the result of Math.random().

4. 1. Use a conditional statement to assign a throw of either rock, paper, and scissors to the computer's random number

This is our first conditional statement.

We assign the value of computerThrow to either rock, paper, or scissors depending on what number the randomNum 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 (randomNum <= 0.33) {
    computerThrow = "rock";
  } else if (randomNum <= 0.66) {
    computerThrow = "paper";
  } else {
    computerThrow = "scissors";
  }

At this point the computer is ready to rumble with it's choice, and the user has made theirs.

5. Compare the user's throw and the computer's throw, and tell the user who won

Here we're creating a function called compare. The compare function takes two arguments userThrow and computerThrow.

var compare = function (userThrow, computerThrow) {
  if (userThrow === computerThrow) {
    window.alert("The result is a tie!")
  } else if (userThrow === "rock") {
    if(computerThrow === "scissors"){
      window.alert("Rock beats scissors! " + userName + " wins!")
    } else {
      window.alert("Paper beats rock! The computer wins!")
    }
  } else if (userThrow === "paper") {
    if(computerThrow === "scissors") {
      window.alert("Scissors beats paper! The computer wins!")
    } else if (computerThrow === "rock") {
      window.alert("Paper beats rock!" + userName + " wins!")
    }
  } else if (userThrow === "paper") {
    if(computerThrow === "rock") {
      window.alert("Paper beats rock! " + userName + " wins!")
    } else if (computerThrow === "scissors") {
      window.alert("Scissors beats paper! The computer wins!")
    }
  }
}
//Invoke the compare function
compare(userThrow, computerThrow)
6 Call the compare function

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="runGame()" class="button">LETS PLAY Rock, Paper, Scissors!</button>

Play around in the sandbox some more!

  • "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?"

YOU DID IT, YOU'RE NOW A JAVASCRIPT CODER!

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 part-time courses at galvanize.com/workshops:

  • Web Development Foundations with JavaScript
  • Data Science Fundamentals: Intro to Python

If you're ready for a full-fledged immersive program, Galvanize offers the following:

Galvanize Web Development 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.

About

Learn some basic JavaScript by building a "Rock, Paper, Scissors" application!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • JavaScript 55.4%
  • HTML 35.8%
  • CSS 8.8%
X Tutup