# JUNTO Practice: Eloquent JavaScript, Chapter 6 Exercises

Discussed on March 25, 2020.

https://eloquentjavascript.net/06_object.html

Exercises:

• "A Vector Type"
• "Groups"
• "Iterable Groups"
• "Borrowing A Method"

## Solutions

Click to see:

### John Lekberg

"A Vector Type"

``````class Vec {
constructor(x, y) {
this.x = x;
this.y = y;
}

plus(vec) {
return new Vec(
this.x + vec.x,
this.y + vec.y,
);
}

minus(vec) {
return new Vec(
this.x - vec.x,
this.y - vec.y,
);
}

get length() {
return Math.sqrt(
Math.pow(this.x, 2) + Math.pow(this.y, 2)
);
}
}
``````

"Groups"

``````class Group {
constructor() {
this.members = [];
}

has(value) {
return this.members.includes(value);
}

if (!this.has(value)) {
this.members.push(value);
}
}

delete(value) {
if (this.has(value)) {
const memberIdx = this.members.indexOf(value);
this.members.splice(memberIdx, 1);
}
}

static from(values) {
const g = new Group();
for (let value of values) {
}
return g;
}
}
``````

"Iterable Groups" the easy way:

``````class Group {
...
*[Symbol.iterator]() {
yield* this.members;
}
}
``````

"Iterable Groups" the other way:

``````class GroupIterator {
constructor(group) {
this.members = [...group.members];
this.i = 0;
}

next() {
const done = this.i === this.members.length;
if (done) {
return { done };
} else {
const value = this.members[this.i];
this.i++;
return { value, done }
}
}
}

Group.prototype[Symbol.iterator] = function() {
return new GroupIterator(this);
};
``````

"Borrowing A Method":

``````console.log(Object.prototype.hasOwnProperty.call(map, "one"));
``````

### Daniel Bassett

"A Vector Type"

``````function Vector(x,y) {
this.x = x;
this.y = y;
}

Vector.prototype.plus = function(vector) {
return new Vector(this.x + vector.x, this.y + vector.y)
}

Vector.prototype.minus = function(vector) {
return new Vector(this.x - vector.x, this.y - vector.y)
}

Object.defineProperty(Vector.prototype, "length", {
get: function() {
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2))
}
});
``````

“Groups”

``````class Group {

constructor() {
this.values = new Array();
}

has(value) {
if (this.values.indexOf(value) >= 0) return true;
return false;
}

if (!this.has(value)) this.values.push(value);
}

delete(value) {
const index = this.values.indexOf(value);
if (index === -1) {
return;
}
this.values.splice(index, 1);
}

static from(array) {
let group = new Group();
array.forEach(element => {
});
return group;
}

}
``````

“Iterable Groups”

``````const Group = require("./groups");

class GroupIterator {
constructor(group) {
this.index = 0;
this.group = group;
}

next() {
if (this.index === this.group.values.length) return { done: true };
const value = this.group.values[this.index];
this.index++;
return { value, done: false };
}

}

Group.prototype[Symbol.iterator] = function() {
return new GroupIterator(this);
};

for (let value of Group.from(["a", "b", "c"])) {
console.log(value);
``````

“Borrowing a Method”

``````console.log(Object.prototype.hasOwnProperty.call(map, "one"));
``````