Randell Dawson 2c15bcbb43 fix(curriculum): changed test text to use should for JavaScript Algorithms and Data Structures (#37761)
* fix: corrected typo

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>
2019-11-27 11:57:38 +01:00

2.4 KiB

id, title, challengeType, forumTopicId
id title challengeType forumTopicId
587d7daf367417b2b2512b7d Iterate Over All Properties 1 301320

Description

You have now seen two kinds of properties: own properties and prototype properties. Own properties are defined directly on the object instance itself. And prototype properties are defined on the prototype.
function Bird(name) {
  this.name = name;  //own property
}

Bird.prototype.numLegs = 2; // prototype property

let duck = new Bird("Donald");

Here is how you add duck's own properties to the array ownProps and prototype properties to the array prototypeProps:

let ownProps = [];
let prototypeProps = [];

for (let property in duck) {
  if(duck.hasOwnProperty(property)) {
    ownProps.push(property);
  } else {
    prototypeProps.push(property);
  }
}

console.log(ownProps); // prints ["name"]
console.log(prototypeProps); // prints ["numLegs"]

Instructions

Add all of the own properties of beagle to the array ownProps. Add all of the prototype properties of Dog to the array prototypeProps.

Tests

tests:
  - text: The <code>ownProps</code> array should include <code>"name"</code>.
    testString: assert(ownProps.indexOf('name') !== -1);
  - text: The <code>prototypeProps</code> array should include <code>"numLegs"</code>.
    testString: assert(prototypeProps.indexOf('numLegs') !== -1);
  - text: You should solve this challenge without using the built in method <code>Object.keys()</code>.
    testString: assert(!/\Object.keys/.test(code));

Challenge Seed

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

Dog.prototype.numLegs = 4;

let beagle = new Dog("Snoopy");

let ownProps = [];
let prototypeProps = [];

// Add your code below this line



Solution

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

Dog.prototype.numLegs = 4;

let beagle = new Dog("Snoopy");

let ownProps = [];
let prototypeProps = [];
for (let prop in beagle) {
  if (beagle.hasOwnProperty(prop)) {
    ownProps.push(prop);
  } else {
    prototypeProps.push(prop);
  }
}