2020-11-27 18:02:05 +00:00
---
2018-09-30 22:01:58 +00:00
id: 587d7daf367417b2b2512b7d
title: Iterate Over All Properties
challengeType: 1
2019-08-05 16:17:33 +00:00
forumTopicId: 301320
2021-01-13 02:31:00 +00:00
dashedName: iterate-over-all-properties
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
2021-08-10 12:44:14 +00:00
You have now seen two kinds of properties: < dfn > own properties</ dfn > and `prototype` properties. Own properties are defined directly on the object instance itself. And prototype properties are defined on the `prototype` .
2019-05-17 13:20:30 +00:00
```js
function Bird(name) {
this.name = name; //own property
}
Bird.prototype.numLegs = 2; // prototype property
let duck = new Bird("Donald");
```
2021-03-31 08:18:37 +00:00
Here is how you add `duck` 's own properties to the array `ownProps` and `prototype` properties to the array `prototypeProps` :
2019-05-17 13:20:30 +00:00
```js
let ownProps = [];
let prototypeProps = [];
for (let property in duck) {
if(duck.hasOwnProperty(property)) {
ownProps.push(property);
} else {
prototypeProps.push(property);
}
}
2021-03-03 00:12:12 +00:00
console.log(ownProps);
console.log(prototypeProps);
2019-05-17 13:20:30 +00:00
```
2021-03-03 00:12:12 +00:00
`console.log(ownProps)` would display `["name"]` in the console, and `console.log(prototypeProps)` would display `["numLegs"]` .
2020-11-27 18:02:05 +00:00
# --instructions--
2021-03-31 08:18:37 +00:00
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 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --hints--
2018-09-30 22:01:58 +00:00
2021-03-03 00:12:12 +00:00
The `ownProps` array should only contain `name` .
2020-11-27 18:02:05 +00:00
```js
2021-01-21 18:52:52 +00:00
assert.deepEqual(ownProps, ['name']);
2020-11-27 18:02:05 +00:00
```
2018-09-30 22:01:58 +00:00
2021-03-03 00:12:12 +00:00
The `prototypeProps` array should only contain `numLegs` .
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
2021-01-21 18:52:52 +00:00
assert.deepEqual(prototypeProps, ['numLegs']);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
You should solve this challenge without using the built in method `Object.keys()` .
```js
assert(!/\Object.keys/.test(code));
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-09-30 22:01:58 +00:00
```js
function Dog(name) {
this.name = name;
}
Dog.prototype.numLegs = 4;
let beagle = new Dog("Snoopy");
let ownProps = [];
let prototypeProps = [];
2020-03-08 14:46:28 +00:00
// Only change code below this line
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00: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);
}
}
```