Last active
July 29, 2021 21:14
-
-
Save yitonghe00/e9044d17311a83d7473b09cdca352fd8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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