Skip to content

Instantly share code, notes, and snippets.

@mmeigooni
Created May 10, 2017 05:09
Show Gist options
  • Save mmeigooni/6b99e203d34948338ed3855dc885dcd2 to your computer and use it in GitHub Desktop.
Save mmeigooni/6b99e203d34948338ed3855dc885dcd2 to your computer and use it in GitHub Desktop.
var library = {
books: {
orwell: ['1984', 'animal farm'],
hemingway: ['A Farewell to Arms', 'The Old Man and the Sea', 'The Sun Also Rises']
},
plays: {
shakespeare: ['romeo and juliet', 'macbeth']
}
};
console.log(library['plays']['shakespeare'][1]);
function areArraysEqual(array1, array2) {
var areEqual = true;
for (var i = 0; i < array1.length; i++) {
if (array1[i] !== array2[i]) {
areEqual = false;
}
}
return areEqual && array1.length === array2.length;
}
if (condition) {
// stuff if true
} else if (another condition) {
// more stuff if another condition is true
} else {
// catch all in case no conditions are met
}
var name = 'Jessica';
if (name === 'George') {
console.log('The name is George!');
} else if (name === 'Jessica') {
console.log('The name is Jessica');
} else {
console.log('The name is neither George nor Jessica')
}
var grade = 82;
// write some if/else statements to console logs a letter
// grade based on the var grade above
if (grade >= 90) {
console.log('A');
} else if (grade >= 80 && grade <= 89) {
console.log('B');
} else if (for c) {
// etc etc
}
var grade = 82;
if (grade >= 90) {
console.log('A');
} else if (grade >= 80) {
console.log('B');
} else if (grade >= 70) {
console.log('C');
} else if (grade >= 60) {
console.log('D');
} else {
console.log('You Fail')
}
function square(x) {
};
// var rps = ['rock', 'paper', 'scissors']
var rps = {1: 'rock', 2: 'paper', 3: 'scissors'};
// using the above rps object, write some code that randomly
// selects a userChoice, a computerChoice, then compares the
// choices and returns a winner
var min = 1;
var max = 3;
var userChoice = rps[Math.floor(Math.random() * (max - min + 1)) + min];
var computerChoice = rps[Math.floor(Math.random() * (max - min + 1)) + min];
// feel free to use MDN to see how to generate random numbers
if (userChoice === computerChoice) {
console.log('User picked ' + userChoice + ' and the computer picked ' + computerChoice + '. It is a tie!');
} else if (userChoice === 'rock' && computerChoice === 'paper') {
console.log('User picked ' + userChoice + ' and the computer picked ' + computerChoice + '. Computer wins!');
} else if (userChoice === 'rock' && computerChoice === 'scissors') {
console.log('User picked ' + userChoice + ' and the computer picked ' + computerChoice + '. User wins!');
} else if (userChoice === 'paper' && computerChoice === 'scissors') {
console.log('User picked ' + userChoice + ' and the computer picked ' + computerChoice + '. Computer wins!');
} else if (userChoice === 'paper' && computerChoice === 'rock') {
console.log('User picked ' + userChoice + ' and the computer picked ' + computerChoice + '. User wins!');
} else if (userChoice === 'scissors' && computerChoice === 'paper') {
console.log('User picked ' + userChoice + ' and the computer picked ' + computerChoice + '. User wins!');
} else if (userChoice === 'scissors' && computerChoice === 'rock') {
console.log('User picked ' + userChoice + ' and the computer picked ' + computerChoice + '. Computer wins!');
} else {
console.log('Something went wrong');
}
// write some logic to console.log a winner
// Solve the above using functions. Use helper functions when it makes sense to.
function decidingWinner() {
// logic of what beats what and console.logs the winner
}
function computerChoice() {
// should randomly return either rock, paper, or scissors
}
function userChoice() {
// should return a random number between min and max
}
function randomNum(min, max) {
// should return a random number between min and max
}
/*
What are examples of comparison operators?
>
<
>=
<=
=== or ==
!==
ternary operator: a way to write simple if statements in one line
objects are passed in by reference not by value
function declaration vs function expression
http://stackoverflow.com/questions/1013385/what-is-the-difference-between-a-function-expression-vs-declaration-in-javascrip
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment