2020-11-27 19:02:05 +01:00
---
2018-09-30 23:01:58 +01:00
id: 587d7daf367417b2b2512b7d
title: Iterate Over All Properties
challengeType: 1
2019-08-05 09:17:33 -07:00
forumTopicId: 301320
2021-01-13 03:31:00 +01:00
dashedName: iterate-over-all-properties
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --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` .
2019-05-17 06:20:30 -07:00
```js
function Bird(name) {
this.name = name; //own property
}
Bird.prototype.numLegs = 2; // prototype property
let duck = new Bird("Donald");
```
2020-11-27 19:02:05 +01:00
Here is how you add `duck` 's `own` properties to the array `ownProps` and `prototype` properties to the array `prototypeProps` :
2019-05-17 06:20:30 -07:00
```js
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"]
```
2020-11-27 19:02:05 +01:00
# --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` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
The `ownProps` array should include `"name"` .
```js
assert(ownProps.indexOf('name') !== -1);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
The `prototypeProps` array should include `"numLegs"` .
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(prototypeProps.indexOf('numLegs') !== -1);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
You should solve this challenge without using the built in method `Object.keys()` .
```js
assert(!/\Object.keys/.test(code));
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
```js
function Dog(name) {
this.name = name;
}
Dog.prototype.numLegs = 4;
let beagle = new Dog("Snoopy");
let ownProps = [];
let prototypeProps = [];
2020-03-08 07:46:28 -07:00
// Only change code below this line
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
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);
}
}
```