Randell Dawson e9212c61d2 fix(curriculum): Remove unnecessary assert message argument from English challenges JavaScript Algorithms and Data Structures - 01 (#36401)
* fix: rm assert msg basic-javascript

* fix: removed more assert msg args

* fix: fixed verbiage

Co-Authored-By: Parth Parth <34807532+thecodingaviator@users.noreply.github.com>
2019-07-13 08:07:53 +01:00

2.0 KiB

id, title, challengeType, videoUrl
id title challengeType videoUrl
56533eb9ac21ba0edf2244cc Accessing Nested Objects 1 https://scrimba.com/c/cRnRnfa

Description

The sub-properties of objects can be accessed by chaining together the dot or bracket notation. Here is a nested object:
var ourStorage = {
  "desk": {
    "drawer": "stapler"
  },
  "cabinet": {
    "top drawer": { 
      "folder1": "a file",
      "folder2": "secrets"
    },
    "bottom drawer": "soda"
  }
};
ourStorage.cabinet["top drawer"].folder2;  // "secrets"
ourStorage.desk.drawer; // "stapler"

Instructions

Access the myStorage object and assign the contents of the glove box property to the gloveBoxContents variable. Use dot notation for all properties where possible, otherwise use bracket notation.

Tests

tests:
  - text: <code>gloveBoxContents</code> should equal "maps"
    testString: assert(gloveBoxContents === "maps");
  - text: Use dot and bracket notation to access <code>myStorage</code>
    testString: assert(/=\s*myStorage\.car\.inside\[\s*("|')glove box\1\s*\]/g.test(code));

Challenge Seed

// Setup
var myStorage = {
  "car": {
    "inside": {
      "glove box": "maps",
      "passenger seat": "crumbs"
     },
    "outside": {
      "trunk": "jack"
    }
  }
};

var gloveBoxContents = undefined; // Change this line

After Test

(function(x) { 
  if(typeof x != 'undefined') { 
    return "gloveBoxContents = " + x;
  }
  return "gloveBoxContents is undefined";
})(gloveBoxContents);

Solution

var myStorage = {
  "car":{
    "inside":{
      "glove box":"maps",
      "passenger seat":"crumbs"
    },
    "outside":{
      "trunk":"jack"
    }
  }
};
var gloveBoxContents = myStorage.car.inside["glove box"];