Files

40 lines
891 B
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Iterate Over All Properties
---
# Iterate Over All Properties
2018-10-12 15:37:13 -04:00
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
The method is to use a `for-in-loop` to iterate through every property in the object. Inside the loop you then check if the property is a `own-property` or a `prototype` and place it in the `ownProps[]` array or the `prototypeProps[]` array. Remember to `push` properties to the `beagle` object and not the `Dog` object to pass all test cases.
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
2018-10-12 15:37:13 -04:00
```javascript
2018-10-12 15:37:13 -04:00
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
2018-10-12 15:37:13 -04:00
for (let property in beagle) {
if (Dog.hasOwnProperty(property)) {
ownProps.push(property);
} else {
prototypeProps.push(property);
2018-10-12 15:37:13 -04:00
}
}
```
</details>