FCC JS Objects

by vanduzled

JavaScript

/* Accessing Objects with Dot Notation */

// Setup
var testObj = {
  "hat": "ballcap",
  "shirt": "jersey",
  "shoes": "cleats"
};

// Only change code below this line

var hatValue = testObj.hat;      // Change this line
var shirtValue = testObj.shirt;    // Change this line

//Accessing Object Properties with Bracket Notation

// Setup
var testObj = {
  "an entree": "hamburger",
  "my side": "veggies",
  "the drink": "water"
};

// Only change code below this line

var entreeValue = testObj["an entree"];   // Change this line
var drinkValue = testObj["my side"];    // Change this line

console.log(entreeValue);



// Accessing Object Properties with Variables
var testObj = {
  12: "Namath",
  16: "Montana",
  19: "Unitas"
};

// Only change code below this line

var playerNumber = 16;       // Change this line
var player = testObj[playerNumber];   // Change this line

console.log(player);
//Montana


// Updating Object Properties
var myDog = {
  "name": "Coder",
  "legs": 4,
  "tails": 1,
  "friends": ["freeCodeCamp Campers"]
};

myDog.name = "Happy Coder";


/* Add New Properties to a JavaScript Object */
var myDog = {
  "name": "Happy Coder",
  "legs": 4,
  "tails": 1,
  "friends": ["freeCodeCamp Campers"]
};

myDog.bark = "woof";

/* Delete a property */
delete myDog.tails;




// Using Objects for Lookups
// use an object to lookup values rather than a switch statement or an if/else chain
function phoneticLookup(val) {
  var result = "";

  // Only change code below this line
  var lookup = {
alpha: "Adams",
bravo: "Boston",
charlie: "Chicago",
delta: "Denver",
echo: "Easy",
foxtrot: "Frank"
  }

  result = lookup[val];

  // Only change code above this line
  return result;
}

phoneticLookup("charlie");
//Chicago


//Testing Objects for Properties
function checkObj(obj, checkProp) {
  // Only change code below this line
  if(obj.hasOwnProperty(checkProp)){
    return obj[checkProp];
  }else {
    return "Not Found";
  }
  // Only change code above this...