Exercise - JS Variables - SOLUTION

by Ryan Morris

JavaScript

/**
 * Exercise:
 * Get to know your data types and variables
 *
 * Follow the directions below but feel free to experiment and veer off course.
 * Work in the console, or use "console.log()" to output to the console from the script.
 */

// 1)
// Declare 2 to 5 variables (name them as you like)
// Make at least one string, one number and one boolean. 
// Log the typeof each to the console
var myInt = 5;
var myStr = "Ryan Morris";
var dayTime = true;

console.group("My Vars");
console.log("My string", typeof myStr);
console.groupEnd();

// 2)
// Declare a new variable that stores the result of a mathematical expression
// You can add/multiply/divice/modulo any set of numbers 
// Log the result to the console
var x = 5 + 2;
console.log("X is", x);

// 3)
// Combine at least three strings together
// Log the result to the console
var myStrings = "A" + " " + "B";
console.log("My strings", myStrings);

// 4)
// Create an array with at least 5 values stored inside
// Log the result to the console
var myArr = [1,2,3,4,5];
console.log(myArr);

// 5)
// Add another value to your original array
// Hint: use length
// Log the result to the console
myArr[myArr.length] = 6;
console.log(myArr);

// 6) 
// Create an object literal that represents a person (perhaps you!)
// Define several properties within the object (name, hairColor, age, etc..)
// Define a property that stores an array ("siblings" or "favoriteColors")
// Log the result to the console
// Log ONLY the "name" property (or any single property) to the console
// Change the "name" property to something else, then log the object to the console
var me = {
    name: "Ryan",
    numToes: 10,
    favColors: ["red", "blue"],
    // bonus...
    speak: function(words) {
        console.log(this.name + " says: " + words);   
    }
}

// 7) Bonus
// Add a function (method) to the previous person object you created to allow it to "speak" to the console.