Saturday, April 27, 2013

J.S. Project: Choose Your Own Adventure! (Continued)


Here is my really lame and micro version of the choose-your-own-adventure game. It was inspired by this.

confirm("Ready to play this awesome game?");

var age = prompt("How old are you?");
    if (age > 18) {
        console.log("OK, you're allowed to play, but I'm not responsible for anything that goes wrong.");
    } else {
        console.log("Alllllright. Let's get on with the show, shall we?");
    }
 
// intro
console.log("You are a sloth in the middle of the rainforest. Your goal is to stay alive. Let the first trial begin");

console.log("You feel the sudden urge to use the bathroom.");

var userAnswer1 = prompt("Will you climb down from the safety of your tree to relieve yourself?");
if (userAnswer1 === "yes") {
    console.log("Oh no! A sunbear just appeared from out of nowhere! It seems like you're going to end up for dinner! Uh oh.");
} else {
    console.log("Oops. You just peed all over yourself. Gross.");
}

// feedback
var feedback = prompt("How was the game? I know it was a bit short, but please rate it from 1 - 10!");
if (feedback > 8) {
    console.log("So glad you liked it! Stay tuned for more!");
} else {
    console.log("Boo. You poop.");
}


P.S. Here's a vlog I created today! I'd totally appreciate it if you check it out!! :) 

Friday, April 26, 2013

J.S. Project: Choose Your Own Adventure!


Today, we're going to create a 'choose your own adventure' game. Exciting, right? It will have a basic story line, the opportunity for the user to make a couple of decisions, and a happy ending (hopefully).

01 / 07 : Let's use the confirm command to ask your user if they are ready to play.
    HINT - remember, to get the user to confirm something, the syntax looks like this:
        confirm("I understand confirm!");

// Check if the user is ready to play!
confirm("Are you ready to embark on this adventure of fun?");


02 / 07 : However before we start playing, we're going to check to see if the user is old enough to play! This step involves two bits of code. Declare a variable age using prompt. Make that age equal to the answer you prompt the user to give (ie. ask the user 'how old are you?'). Finally, create an if / else statement: if the user is less than 19, use console.log and tel the that they're allowed to play but you take no responsibility. Else, use console.log and give them an encouraging message to play on!
    HINT - recall the syntax for if / else statements:
        if (condition) {
            code code code;
        } else {
            code code code;
        }

var age = prompt("How old are you?");
    if (age < 18) {
        console.log("You're allowed to play, but I take no responsibility.");
    } else {
        console.log("Well, what are you waiting for? Let's get started already!");
    }


03 / 07 : We've gotta set up the scene for our story. Here's a short little introduction that we can use for now: Snow White and Batman were hanging out at the bus stop, waiting to go to the shops. There was a sale on and both needed some new threads. You've never really liked Batman. You walk up to him.
Let's print the intro using console.log. The intro is full of words... remember how words are called 'strings' in JavaScript? Don't forget how to properly use strings!
    HINT - don't forget to put quotes around the intro!

// introduction
console.log("Snow White and Batman were hanging out at the bus stop, waiting to go to the shops. There was a sale on and both needed some new threads. You've never really liked Batman. You walk up to him.");


04 / 07 : Our story is ready to move along! There's a scene and your user is about to talk to batman. Under your existing code, print out the storyline: "Batman glares at you." Then declare a variable userAnswer and make it equal a prompt that asks the user "Are you feeling lucky, punk?" (This will be the question that Batman asks your user).

console.log("Batman glares at you.");
var userAnswer = prompt("Are you feeling lucky, punk?");


05 / 07 : Now we've gotta create different scenarios using the if / else statements! Use an if / else statement to write out the last part of this game. If userAnswer is "yes", print out: "Batman hits you very hard. It's Batman and you're you! Of course Batman wins!" Otherwise, print out: "You did not say yes to feeling lucky. Good choice! You are a winner in the game of not getting beaten up by Batman."
    HINT - Remember: = is for assignment, and === is to check this are equal!

if (userAnswer === "yes") {
    console.log("Batman hits you very hard. It's Batman and you're you! Of course Batman wins!");
} else {
    console.log "You did not say yes to feeling lucky. Good choice! You are a winner in the game of not getting beaten up by Batman.");
}


06 / 07 : It's probably worthwhile to ask your user for feedback. Let's create a variable called feedback and prompt the user to rate your game out of 10. If the score is greater than 8, print out: "This is just the beginning of my game empire. Stay tuned for more!" Otherwise, print out: "I slaved away at this game and you gave me that score?! The nerve! Just you wait!"
    HINT - var feedback  = prompt("Message");

var feedback = prompt("Could you rate my game out of 10?");
    if (feedback > 8) {
        console.log("This is just the beginning of my game empire. Stay tuned for more!");
    } else {
        console.log("I slaved away at this game and you gave me that score?! The nerve! Just you wait!");
    }


07 / 07 : Congrats! You've just programmed your first game! Now it's your turn to program your own 'choose your own aventure game'.






Thursday, April 25, 2013

Javascript: Objects II in Review


01/ 04
Instructions: Examine the languages object. Three properties are strings, whereas one is a number. Use a for-in loop to print out the three ways to say hello. In the loop, you should check to see if the property value is a string so you don't accidentally print a number.
    HINT: Use an if statement in combination with the typeof operator to figure out whether or not something is a "string". If it's a "string", then print it!

Make sure you're checking the property value (e.g., "Hello!") and no the property name (e.g., english). Recall that if we save a property name to a variable, we can access the value associated with that name using bracket notation.

Recall the for-in loop:

for(var x in obj) {
executeSomething();
}

/* This will go through all properties of obj one by one and assign the property name on each run of the loop. */

var languages = {
    english: "Hello!",
    french: "Bonjour!",
    notALanguage: 4,
    spanish: "Hola!"
};

// print hello in the 3 different languages
for(var x in languages) {
if (typeof languages[x]);
}
}

02/ 04
Instructions: Add the sayHello method to the Dog class by extending its prototype. sayHello should print to the console: "Hello this is a [breed] dog", where [breed] is the dog's breed.
    HINT: recall how we previously added a method to the Dog class:
Dog.prototype.bark = function()
{
    console.log("Woof");
}; 

To access a dog's breed from within the method, use this.breed.

function Dog (breed) {
    this.breed = breed;
};

// add the sayHello method to the Dog class
// so all dogs now can say hello
Dog.prototype.sayHello = function()
{
    console.log("Hello this is a " + this.breed + " dog");
};

var yourDog = new Dog("golden retriever");
yourDog.sayHello();

var myDog = new Dog("dachshund");
myDog.sayHello();

03/ 04 
Instructions: Let's first see what type Object.prototype is. Do this in line 2 and save it into prototypeType. If all goes well, you should realize that Object.prototype itself is an object! And since all objects have the hasOwnProperty method, it's pretty easy to check if hasOwnProperty comes from Object.prototype.
    HINT: to see what type Object.prototype is, we should use typeof Object.prototype. The property we want to check for is actually "hasOwnProperty", so line 6 should look like: Object.prototype.hasOwnProperty("hasOwnProperty").

// what is this "Object.prototype" anyway...?
var prototypeType = typeof Object.prototype; 
console.log(prototypeType);

// now let's examine it!
var hasOwn = Object.prototype.hasOwnProperty("hasOwnProperty"); 
console.log(hasOwn);

04/ 04 
Instructions: Modify the StudentReport class so that no grades will be printed to the console in the for-in loop. However, getGPA should still function properly in the last line.
    HINT: You should be changing public variables ( this.grade ) to private variables ( var grade ). If we want to getGPA to be able to be called from outside this class, should we change it to be private? You should find yourself needing to modify getGPA itself. this.grade1 will not be available if you did not declare it previously.

function StudentReport() {
    var grade1 = 4;
    var grade2 = 2;
    var grade3 = 1;
    this.getGPA = function() {
        return ( grade1 + grade2 + grade3 ) / 3;
    };
}

var myStudentReport = new StudentReport();

for(var x in myStudentReport) {
    if(typeof myStudentReport[x] !== "function") {
        console.log("Muahaha! "+myStudentReport[x]);
    }
}

console.log("Your overall GPA is "+myStudentReport.getGPA());







Tuesday, April 23, 2013

Javascript: Private and Public


All properties in JavaScript are automatically public, which means that they can be accessed outside of the class. Look at the Person class below. It has three properties: firstName, lastName, and age. Think of these properties as the information a class is willing to share.

 On lines 8 and 9, we access the firstName and lastName properties of john and assign them myFirst and myLast. Note that we are free to access the firstName, lastName, and age properties, which is what we mean when we say that they are public.

function Person(first, last, age) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
}

var john = new Person( 'John', 'Doe', 30);
var myFirst = john.firstName;
var myLast = john.lastName;
var myAge = john.age;

Yay! But what if an object wants to keep some information hidden? Remember local variables? (--They can only be accessed from within that function!) Well functions can also have private variables which are pieces of information that can only be directly accessed from within the class.

Here, the Person class has been modified to have a private variable called bankBalance. Notice that it looks just like a normal variable except that it is defined inside the constructor for Person, without using this, but instead using var. This makes bankBalance a private variable.


function Person(first, last, age) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    var bankBalance = 7500;
}


We we try to print his bankBalance out, all we get is undefined.

Methods can also be private within a class and inaccessible outside of the class. Changing this.returnBalance from the last exercise to var returnBalance makes this method private. The way to access a private method is similar to accessing a private variable. You must create a public method for the class that returns the private method.

var returnBalance = function() {
    return bankBalance;
};
this.askTeller = function() {
    return returnBalance;
};

var john = new Person('John', 'Doe', 30);
console.log(john.returnBalance);
var myBalanceMethod = john.askTeller();
var myBalance = myBalanceMethod();
console.log(myBalance);

The askTeller function has been modified within the Person class to directly give you your balance. However, it now needs the account password parameter in order to return the bankBalance.


function Person(first,last,age) {
   this.firstname = first;
   this.lastname = last;
   this.age = age;
   var bankBalance = 7500;
 
   this.askTeller = function(pass) {
     if (pass == 1234) {
        return bankBalance;
     }
     else {
        return "Wrong password.";
        }
   };
 
   var myBalance = function(pass) {
       return askTeller;
   };
}

var john = new Person('John','Doe',30);
/* the variable myBalance should access askTeller() with a password as an argument  */

Sunday, April 21, 2013

Javascript: Inheritance in Object Oriented Programming


Inheritance allows one class to see and use the methods and properties of another class. Let's create a class called Animal, along with a sayName method:

function Animal(name, numLegs) {
    this.name  = name;
    this.numLegs = numLegs;
};

animal.prototype.sayName = function()
{
    console.log("Hi my name is " + this.name)
};

// here's a code to test the above constructor and method
var penguin = new Animal("Captain Cook", 2);
penguin.sayName();

Wouldn't it be cool if we created a Penguin class? Let's do that so that we can give it some methods unique only to a penguin and not confuse it with the Animal class.

function Penguin(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
}

Penguin.prototype.sayName = function()
{
    console.log("Hi my name is " + this.name);
};

We seemed to have reused a lot of the same code as the Animal class. This goes against the "DRY" principle--aka "don't repeat yourself"! We're going to create a new Penguin class, but we're going only take a name parameter, and within the constructor itself, set this.numLegs to 2.

function Penguin (name) {
    this.name = name;
    this.numLegs = 2

// set its prototype to be a new instance of Animal
Penguin.prototype = new Animal();

Now we're going to create an Emperor class that takes a single name parameter and sets its name property to be this value.

// create your Emperor class and make it inherit from Penguin
function Emperor (name) {
    this.name = name;
}

Emperor.prototype = new Penguin();
// create an "emperor" object and print the number of legs it has
var emperor = new Emperor("Mr. McWaddle");
console.log(emperor.numLegs);

The "prototype chain" in Javascript sort of like a food chain. If JavaScript encounters something it can't find it the current class's methods or properties, it looks up the prototype chain to see if it's defined in a class that it inherits from. This keeps going upwards until it stops all the way at the top: the Object.prototype. All classes inherit directly from Object, unless we change the class's prototype.

// original classes
function Animal(name, numLegs) {
    this.name = name;
    this.numLegs = numLegs;
    this.isAlive = true;
}
function Penguin(name)
    this.name = name;
    this.numLegs = 2;
}
function Emperor(name) {
    this.name = name;
    this.saying = "Waddle waddle";
}

// set up the prototype chain
Penguin.prototype = name Animal();
Emperor.prototype = new Penguin();

var myEmperor = new Emperor("Jules");

console.log( myEmperor.saying ); // should print "Waddle waddle"
console.log( myEmperor.numLegs ); // should print 2
console.log( myEmperor.isAlive ); // should print true

Saturday, April 20, 2013

Javascript: Object-Oriented Programming Basics


Alright, so now we can finally learn the basics of OOP! The first thing I'm going to introduce is a class.    When you make a constructor, you are actually defining a new class! A class can be thought of as a type, or a category of objects -- remember numbers and strings? Those are types too!

/* take a look at the example below, jenny and alex are two separate objects, but they both belong to the class Person */
function Person(name, age) {
    this.name = name;
    this.age = age;
};

var jenny = new Person("Jenny", 18);
var alex = new Person("Alex", 13);

A prototype keeps track of what a given class can or cannot do. Javascript automatically defines the prototype for class with a constructor.

// for example, this dog constructor ensures that the dog prototype has a breed property
function Dog (breed) {
    this.breed = breed;
};

Classes are really useful because they tell us helpful information about objects. You can actually think of an object as a particular instance of a class. Look at our Person class. We know that any Person will have a  name and age because they are in they are in the constructor. Let's create a function like printPersonName--this will take a Person as an argument and print out their name.

// recall our Person class
function Person(name, age) {
    this.name = name;
    this.age = age;
};

// a function that prints out the name of any given person
function printPersonName(p) {
    console.log(p.name);
};

var jenny = new Person("Jenny", 18);
printPersonName(jenny);

If you want to add a method to a class such that all members of the class can use it, we use the following syntax to extend the prototype:

className.prototype.newMethod = 
function() {
statements; 

}; 

// here's another example using the dog class:
function Dog (breed) {
    this.breed = breed;
};

// here we make fifi and teach her how to bark
var fifi = new Dog("pomeranian");
Dog.prototype.bark = function() {
    console.log("Arf!");
};
fifi.bark();

// here we make maya
var maya = new Dog("Pug");
maya.bark();

P.S. I created a youtube channel where I'll be uploading vlogs and cooking videos and random miscellaneous things, so if you want to get to know me a little better, I suggest you check it out! (And subscribe!) :)

Friday, April 19, 2013

Javascript: Even More on Objects




















How do we figure out if something is an object? We can call typeof thing to figure this out. In general, most types fall under "number", "string", "function", and "object".
var someObject = {someProperty: someValue};
console.log( typeof someObject ); 

// concrete example
var anObj = { job: "I'm an object!" };
console.log( typeof anObj );

Every object in JS comes with some baggage, including a method called hasOwnProperty. This lets us know if an object has a particular property.
// this is how you use hasOwnProperty
var myObj = {
    name: myObj
};

console.log( myObj. hasOwnProperty('name')); // should print true

console.log( myObj. hasOwnProperty('nickname')); // should print false

// here's another example of hasOwnProperty used in an if-else statement: 

/* recall if-else statements: 
if ( expression ) {
} else {
} */

var suitcase = {
    shirt: "Hawaiian"
}; 

if (suitcase.hasOwnProperty("shorts")) {
    console.log(suitcase.shorts); 
} else {
    suitcase.shorts = "red"; 
    console.log(suitcase.shorts);
}

To work with all the properties that belong to an object, let's bring back the for-in loop. 

// define some object 
var pom = {
species: "pomeranian", 
age: 10, 
color: "golden",
};

// for-in loop to print out pom's property names 
for(var property in pom) {
console.log(property); 

We've just learned how to print all of an object's property names with a for-in loop, but how do we print out the values associated with every property?

// for-in loop to print out the value of all pom's properties 

// recall 
pom.species = pom["species"] = "pomeranian" 
// if we say
var x = "species" 
// then
dog[x] = "pomeranian'

for(var x in pom) {
console.log(pom [x]); 

Javascript: Intro to Objects II




















I know we've been reviewing objects I again and again these past couple of blog posts, but I think it's safe to say that there's a reason for this perpetual repetition. Codeacademy really wants you to get down the basics before moving into new territories. Last night I attempted to finish the entire course in one-go--and was almost successful in that endeavor. However, I was stumped by the last lesson... probably because my level of understanding was just sufficient in order for me to complete the problems right in front of me, but not enough to truly understand and apply what I had just learned to more complex situations. Oh well. Hopefully after updating this blog with the lessons I covered last night, I'll be somewhat more refreshed (and a gain deeper understanding)!

// literal notation creates a single object
var jenny = {
    job: "student",
    married: false
};

// constructor notation involves defining an object constructor using the function keyword
function Person(job, married) {
    this.job = job;
    this. married = married;
}
// create a "jenny" object using the Person constructor
var jenny = new Person("student", false);

Methods are essentially functions associated with objects, remember?
function someObj() {
this.someMethod = function() {
};
}

// add a speak method to the Person constructor via constructor notation 
function Person(job, married) {
    this.job = job;
    this.married = married;
    this.speak = "Hello!"
}

// add method to object via literal notation
var jenny = {
    job: "student",
    married: false,
    speak: function(mood) {
        console.log("Hello, I am feeling " + mood); 
};

jenny.speak("fantastic");
jenny.speak("meh");


When defining a method for an object, use this.propertyName to reference other properties in that object. When that method is called, this.propertyName will always refer to the most recent value of propertyName.

var jenny = {
    job: "student",
    married: false,
    sayJob: function() {
        console.log("Hi, I work as a " + this.job); 
    }
};

// jenny's first job
jenny.sayJob();

// change jenny's job to "coding wizard with big fluttery beard"
jenny.job = "coding wizard with big fluttery beard";

//jenny's second job:
jenny.sayJob();

Throughout this post, I've been using dot notation to get the value of an object's property:
someObj.propName
However, we can also use bracket notation:
someObj["propName"] 

The advantage of using bracket notation is that we aren't restricted to using only strings in the brackets. We can also use variables whose values are property names:
var someObj = {propName: someValue};
var myProperty = "propName";
someObj[myProperty]

// some Obj[myProperty] = someObj["propName"] 
/* first set aProperty to a string of the first property in jenny (ie. the job property), then print jenny's job using bracket notation and a property */
var aProperty = "job";

console.log(jenny[aProperty]; 
);

Thursday, April 18, 2013

Javascript: Objects in Review


Objects give us a way to represent things--IRL or virtual. We're able to do this by storing information within the object's properties.

So far, we have learned two ways to make objects:
// literal notation, where we use var Name = { }; 
var cutedork = {
    age: 18,
    country: "United States"
};

// constructor notation, where we use the keyword new
var cutedork2 = new Object(); 
    cutedork2.age = 18;
    cutedork2.country = "United States";

Remember what properties are? They're similar to variables... except that they belong to an object and hold information. We can access properties in two ways:

// dot notation, with ObjectName.PropertyName
var age = jenny.ages

// bracket notation, with ObjectName["PropertyName"] (--don't forget the quotes!)
var country = jenny['country']

Methods act like functions associated with an object and they're especially useful when you ether want to:
1. Update the object properties
2. Calculate something based on an object's properties

// example of defining area and perimeter methods
function Circle (radius) {
    this.radius = radius;
    this.area = function () {
        return Math.PI * this.radius * this.radius;
    };

function Perimeter (radius) {
    this.radius = radius;
    this.area = function() {
        return 2 * Math.PI * this.radius;
    };
}}

Lastly--in addition to our basic object constructor, we can create our own custom constructors which are helpful for two reasons:
1. We can assign our object properties through parameters we pass in when the object is created.
2. We can give our object methods automatically.

// 3 lines are required to make mdfrmscrtch
var mdfrmscrtch = new Object(); 
mdfrmscrtch.blogURL = "http://mdrmscrtch.blogspot.com";
mdfrmscrtch.creator = "Jenny"

// A custom constructor for blog
function Blog (blogURL, creator) {
    this.blogURL = blogURL;
    this.creator = creator;
}

// Use this new creator to make hello_cutedork in one line!
var hello_cutedork = new Blog("http://hellocutedork.blogspot.com", "Jenny");

Song of the day // Lucky Ones (Lana Del Rey)

Javascript: Combining Objects with Arrays, Loops, and Functions


a slice of coconut cake @ souen

Objects are just another type--like a string or number, but a bit more complex. So just as we can make arrays of strings and numbers, we can also makes arrays of objects:

// here, we have a person constructor:
function Person (name, age) {
    this.name = name;
    this.age = age;
};

// now we can make an array of people:
var family = new Array();
family[0] = new Person("jenny", 18);
family[1] = new Person("alex", 13);
family[2] = new Person("mom", 51);
family[3] = new Person("dad", 49);

Arrays filled with objects will work just like arrays filled with numbers and strings. In the same we we can loop through an array of numbers to print them out or to calculate a sum, we can loop through an array of objects and access properties or methods:

// loop through our new array and print out the name of each family member:
for (i = 0; i < family.length; i++ )
{console.log(family[i].name)
};

We can also use objects as parameters for functions as well. That way, we can take advantage of the methods and properties that a certain object type provides. In addition to our Person constructor, we have also introduced a new function, ageDifference. This function takes two Person objects as parameters and returns the difference in age between the two people:

// create var ageDifference
var ageDifference = function(person1, person2) {
    return person1.age - person2.age;
};

// create multiple example people
var jenny = new Person("Jenny", 18);
var alex = new Person("Alex", 13);

// create new variable diff
var diff = ageDifference(jenny, alex);

Javascript: Construction Junctions

chocolate green tea lava cake @ spot dessert bar 


constructor is a way to create an object. So far, we've been using a built-in constructor called Object. Already defined by the JS language, it creates an object with no properties/methods.
var thecutedork = new Object();
thecutedork.url = "http://hellocutedork.blogspot.com";
thecutedork.platform = "blogger"

However, don't you agree that this makes adding properties one at a time for every single object kind of tedious? Well, we can create our own constructions. This way, we can actually set the properties for an object right when it is created!

function Blog(url, platform) {
  this.url = url;
  this.platform = platform;



// let's make goodwillgrrl and mdfrmscrtch, using our constructor:
var goodwillgrrl = new Blog("http://goodwillgrrl.blogspot.com ", "blogger")
var mdfrmscrtch = new Blog("http://mdfrmscrtch.blogspot.com", "blogger")

In addition to setting properties, constructors can also define methods.

/* here is a Rectangle constructor, which sets the length and width properties equal to the arguments, just like our Blog constructor did with url and platform.

Notice the calcArea method. This calculates the area of the rectangle in terms of its length and width. */

function Rectangle(length, width) {
  this.length = length;
  this.width = width;
  this.calcArea = function() {
    return this.length * this.width;
}; 
}

var rex = new Rectangle(7,3);
var area = rex.calcArea();

/* Constructors are a way to make objects with the keyword new. We have one final example of a constructor which defines an adjective property and adescribeMyself method: /*

function Rabbit(adjective) {
  this.adjective = adjective;
  this.describeMyself = function(){
    console.log("I am a " + this.adjective + " rabbit");
  };
}

// now we can easily make all of our rabbits:
var rabbit1 = new Rabbit("fluffy");
var rabbit2 = new Rabbit("happy");
var rabbit3 = new Rabbit("sleepy");

rabbit1.describeMyself();

rabbit2.describeMyself();
rabbit3.describeMyself();

Javascript: Methods

my brother's science project

Methods are an important part of OOP (object oriented programming). Remember last time we worked with properties? Whereas properties are similar to variables, a method is like a function associated with an object. Methods are important because they serve several purposes including:

1. The ability to change object property values
2. The ability to make calculations based on object properties. Functions can only use parameters as an input, but methods can make calculations with object properties. Cool, huh? 

Here's an example: 

// make cutedork here, and first give her an age of 8
var cutedork = new Object();
cutedork.age = 8; 
cutedork.setAge = setAge; 

// here, update cutedork's age to 18 using the method
cutedork.setAge(18);

Another slightly more challenging example: 

var square = new Object();
square.sideLength = 6;
square.calcPerimeter = function() {
  return this.sideLength * 4;
};

// help us define an area method here
square.calcArea = function() {
  return this.sideLength * this.sideLength
};

var p = square.calcPerimeter();
var a = square.calcArea();

Javascript: More on Objects

Plants on the windowsill
Today, I worked a little on objects. In my previous lessons, I've only really worked with numbers and strings. The third data type I'm learning about are objects. They let us code real things and entities by storing all relevant information in one place. To create an object, we use var, followed by the name of the object and =. Each object then starts with {has information inside, and ends with };

// for example:
var Jenny = { };

Each piece of information we include in an object is called a property. Each property has a name, followed by : and then the value of that property. For example, if we want to display my age, we'd type:

age: 18 

we can access properties using two different methods: dot notation and literal notation.

// dot notation
var jenny = new Object();
jenny.username = "cutedork";
jenny.age = 18;

// literal notation
var jenny = {
    username: "cutedork",
    age: 18
};

Song of the day // Static Mind (Girl in a Coma)

Javascript: Intro to Objects

homemade meatballs using Chef John's recipe


We finally get to use 'nouns' and 'verbs' together!!! In Javascript, let's think of 'nouns' as data such as arrays, variables, strings, etc. and 'verbs' as functions... With objects, we can finally put the two--our information and the function that uses our information, together in the same place. REUNITED AT LAST!

// here is a short code:

var blogEntry = {};

blogEntry.name = 'Jenny';
blogEntry.URL = 'thecutedork.blogspot.com';
blogEntry.blog = function(){
  console.log('Check out ' + this.name + ' at ' + this.URL + '...');
};

blogEntry.blog();

Javascript: &&, ||, !

Fifi


Today, I covered: and(&&), or(||), not(!).
For and(&&), remember:
true && true // => true
true && false // => false
false && true // => false
false && false // => false

Whereas or(||):
true || true // => true
true || false // => true
false || true // => true
false || false // => false 

Lastly, not(!):
!true // => false
!false // => true

Here is a code I wrote which uses &&, ||, !:

// set variables to either true or false:
var iLoveYoga = true;
var iLoveRunning = true;

if(iLoveYoga && iLoveRunning) {
    // if iLoveYoga AND iLoveRunning:
    console.log("Awesome, yoga and running work hand-in-hand!");
} else if (!(iLoveYoga || iLoveRunning)) {
    // if NOT iLoveYoga OR iLoveRunning:
    console.log("You've gotta get your lazy butt off the couch and out the door!!!");
} else {
    console.log("You only like one, but not the other?");
}

Javascript: The Switch Statement

a total of 10 bucks

The 'switch' statement is so freaking cool. Excuse the lameness and feel free to replace all the bolded words with whatever you want! Hahaha :)

var outfit = prompt("What do you think would look good with this maxi skirt?","Enter text here");

switch(outfit){
    case 'crop top':
        console.log("Totally agree with you, girl.");
        break;
    case 'flatforms':
        console.log("You like flatforms? Same here!");
        break;
    case 'concho belt':
        console.log("Such a fab accessory, especially for summer!");
        break;
    case 'uggs':
        console.log("Girl, how many times have I told you... don't ever wear those... those things!");
        break;
    default:
        console.log("Huh.... are you sure about " + outfit + " ? How about a crop top?");
}