Intro to JS - Wizard Check

by Satish Kesiboyana

JavaScript

/** 
 * Hello!
 * Below are some questions to measure your exposure to 
 * JavaScript syntax and some core concepts. Don't worry 
 * if you don't know the answer, just give it a whirl 
 * and see how far you can get.
 *
 * Don't forget to open up your console and use 
 *   console.log(value);
 * To echo results to the console
 */

// 1. Declare a variable "myArray" and reference an array of 3 values (colors?)
var myArray1 = ['red', 'blue', 'green'];
var myArray2 = [1, 2, 3];
console.log(myArray1);
console.log(myArray2);

// 2. Declare a variable "person" and reference an object with 2 properties, "name", which is your name, and "toString", which is a function that returns the name value.
var person = {
    name: Satish,
    toString(): function() {
        console.log(this.name);
    }
}
person.name;
person.toString();

// 3. Create a function that returns either "elf" or "dwarf", based on a random number
// If less than 0.5, return "elf"
// If more than or equal to 0.5, return "dwarf"
function foo() {
    if (Math.random < 0.5) {
        console.log("less than 0.5, elf");
        return "elf";
    }
    else {
        console.log("greater than 0.5, dwarf");
        return "dwarf";
    }
}

// 4. Create a function that iterates over an array and logs each value to the console. Invoke the function passing "myArray" (from above) as the argument.
function itr (arr) {
    for (var i=0; i<arr.length; i++) {
        console.log(arr[i]);
    }    
}

// 5. Create a new object called "me" that uses "person", the object you created up above, as its prototype.
var me = Object.create(person);

// 6. "call" your "toString" function from the "person" object in the context of the "me" object.
person.toString.call(me);