Skip to content

Commit b18e135

Browse files
authored
Implement getters and setters for robot sensors
1 parent f31e24a commit b18e135

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

Codecademy/gettersAndSetters.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const robot = {
2+
_model: "1E78V2",
3+
_energyLevel: 100,
4+
_numOfSensors: 15,
5+
get numOfSensors() {
6+
if (typeof this._numOfSensors === "number") {
7+
return this._numOfSensors;
8+
} else {
9+
return "Sensors are currently down.";
10+
}
11+
},
12+
set numOfSensors(num) {
13+
if (typeof num === "number" && num >= 0) {
14+
this._numOfSensors = num;
15+
} else {
16+
console.log("Pass in a number that is greater than or equal to 0");
17+
}
18+
},
19+
};
20+
21+
console.log(robot.numOfSensors);
22+
robot.numOfSensors = 100;
23+
console.log(robot.numOfSensors);

0 commit comments

Comments
 (0)