Skip to content

Instantly share code, notes, and snippets.

@mmeigooni
Created May 3, 2017 05:07
Show Gist options
  • Save mmeigooni/06da2b63003f492ac9db83f4d617e1ae to your computer and use it in GitHub Desktop.
Save mmeigooni/06da2b63003f492ac9db83f4d617e1ae to your computer and use it in GitHub Desktop.
// Data type
// Examples: String, number, boolean
// Data structure
// Examples: arrays, objects
var firstName = "Ram";
var age = "30";
// console.log(age + " + " + age);
// // "30 + 30"
/*
// first step in compilation of console.log(age + " + " + age);
// console.log("30" + " + " + age);
// second step in compilation of console.log(age + " + " + age);
console.log("30 + " + age);
// third step in compilation of console.log(age + " + " + age);
console.log("30 + " + "30");
// fourth step in compilation of console.log(age + " + " + age);
console.log("30 + 30");
*/
// ARRAYS -- a container of elements (aka values, aka items)
var firstNames = ['Ram', 'George', 'Irwing', 'Swami', 'Andrew'];
// console.log(firstNames.length);
// console.log(firstNames[0]);
// console.log(firstNames[4]);
// console.log(firstNames[firstNames.length - 1]);
firstNames[1] = "Mickey Mouse";
firstNames[5] = "Goofy";
firstNames.push("Donald");
// console.log(firstNames);
// I want another array which is the first half of the firstNames array
var firstHalfNames = firstNames.slice(0, firstNames.length / 2 );
// console.log(firstHalfNames);
// OBJECTS -- container of key/value pairs (aka properties)
var personInfo = {firstName: 'Swami', 'lastName': 'Varadan', 'age': 39, 'favorite food': 'pizza'};
var surpriseKey = 'lastName';
// console.log(personInfo['firstName']); // we want 'Swami' to output
// console.log(personInfo.firstName);
// console.log(personInfo['favorite food']);
// RPS
// Write some code that that randomly selects either Rock, Paper, or Scissors from an array
// create an array with the options
var rps = ['Rock', 'Paper', 'Scissors'];
// generate a random number between 0 and 2
var random = Math.floor(Math.random() * (max - min)) + min;
//
var max = 3;
var min = 0;
var random = Math.floor(Math.random() * (max - min)) + min;
console.log(random);
// one more step here
// RPS 2
// Write some code that that randomly selects either Rock, Paper, or Scissors from an object
var rps2 = {1: 'Rock', 2: 'Paper', 3: 'Scissors'}
// more code down here
/*
List of array methods to be familiar with
* push
* pop
* slice
* shift
List of useful built-in Math functions
* Math.ceil();
* Math.round();
* Math.floor();
Important things to know
* There are two ways to access key/value pairs inside an object.
* Way 1: [] USE THIS IF YOU'RE USING A VARIABLE
* Way 2: . (dot notation) USE THIS IF YOU'RE REFERENCING AN EXPLICIT KEY
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment