Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yitonghe00/e9044d17311a83d7473b09cdca352fd8 to your computer and use it in GitHub Desktop.
Save yitonghe00/e9044d17311a83d7473b09cdca352fd8 to your computer and use it in GitHub Desktop.
class PGroup {
// Your code here
constructor(values) {
this.values = values;
}
add(elem) {
if (this.has(elem)) {
return this;
} else {
return new PGroup([...this.values, elem]);
}
}
delete(elem) {
if (this.has(elem)) {
return new PGroup(this.values.filter(e => e !== elem));
} else {
return this;
}
}
has(elem) {
return this.values.includes(elem);
}
}
// To add a property (empty) to a constructor that is not a method,
// you have to add it to the constructor after the class definition, as a regular property.
PGroup.empty = new PGroup([]);
let a = PGroup.empty.add("a");
let ab = a.add("b");
let b = ab.delete("a");
console.log(b.has("b"));
// → true
console.log(a.has("b"));
// → false
console.log(b.has("a"));
// → false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment