Skip to content

Instantly share code, notes, and snippets.

@gringogidget
Last active February 16, 2016 20:20
Show Gist options
  • Save gringogidget/e43b24d6de3608021474 to your computer and use it in GitHub Desktop.
Save gringogidget/e43b24d6de3608021474 to your computer and use it in GitHub Desktop.
JS Notes
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
== Equal to (equality operator eg "3" == 3 is true)
=== Strict equal to (strict equality operator type as well as value eg "3" == 3 is false)
!= Not equal to
!== Strict not equal to
// Conditional Statements // (Bascially, "IF this is true THEN do that")
var answer = prompt('What programming language is the name of a gem?');
if () {
}
- Conditional statements begin with the "if" keyword.
- After the keyword, come a pair of parentheses ().
- Inside the parentheses goes the condition.
A condition is a simple trick whose answer is either true or false.
- After the condition are a set of curly braces {}.
This is for the set of code to run IF the condition is TRUE.
The braces form what's called a "code block" which can hold one or more JS statements.
There is no semicolon after the last curly brace.
- An equality operator (===) checks to see if two values are the same.
eg:
if (answer === 'Ruby') {
document.write("<p>That's right!</p>");
}
// Else Clause (What to do if the IF is false)
var answer = prompt('What programming language is the name of a gem?');
if (answer === 'Ruby') {
document.write("<p>That's right!</p>");
} else {
document.write("<p>Sorry, that's wrong.</p>");
}
*****Bonus:*****
Since the answer is case sensitive, change:
if (answer === 'Ruby') {
to
if (answer.toUpperCase() === 'RUBY') {
@gringogidget
Copy link
Author

// Build a Random Number Guessing Game

var randomNumber = Math.floor(Math.random() * 6 + 1;
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');
if (parseInt(guess) === randomNumber ) {
document.write('<p>You guessed the number!</p>');
} else {
document.write('<p>Sorry. The number was ' + randomNumber + '</p>');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment