Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created March 3, 2022 04:35
Show Gist options
  • Save natafaye/a5e6f3db2c252f52605c1ff1f4fa6fdc to your computer and use it in GitHub Desktop.
Save natafaye/a5e6f3db2c252f52605c1ff1f4fa6fdc to your computer and use it in GitHub Desktop.
class AcceptedAnswerPrompt {
constructor(acceptedAnswers) {
this.acceptedAnswers = acceptedAnswers;
}
// Returns true or false depending on if it's in the list or not
isAcceptedAnswer(potentialAnswer) {
return this.acceptedAnswers.includes(potentialAnswer);
}
// prompt until they give an accepted answer, then return that answer
prompt(question) {
let answer = prompt(question);
while( !this.isAcceptedAnswer(answer) ) {
answer = prompt(question + "\nIt needs to be one of these options: " + this.acceptedAnswers.join(", "))
}
return answer;
}
}
class BooleanPrompt extends AcceptedAnswerPrompt {
constructor() {
super(["yes", "no"])
}
prompt(question) {
// call the prompt on AcceptedAnswerPrompt
const answer = super.prompt(question);
// return true if they picked "yes" false if they picked "no"
return answer === "yes";
}
}
// using Boolean Prompt
const confirmPrompt = new BooleanPrompt()
const shouldDelete = confirmPrompt.prompt("Should we delete?");
if(shouldDelete) {
alert("Deleting the thing")
}
// using Accepted Answer Prompt
const yesNoPrompt = new AcceptedAnswerPrompt( ["yes", "no"] )
const answer = yesNoPrompt.prompt("Should we do something?");
if(answer === "yes") {
alert("They said yes!")
}
else if(answer === "no") {
alert("They said no!")
}
else {
alert("This shouldn't happen!") // we should never get here
}
const shouldDelete = yesNoPrompt.prompt("Are you sure you want to delete?");
if(shouldDelete === "yes") {
// delete the thing
}
const votingPrompt = new AcceptedAnswerPrompt( ["cat", "dog", "fish"] )
const answer = votingPrompt.prompt("What is the best animal?");
if(answer === "fish") {
alert("You're wrong")
}
class Animal {
constructor(type, name) { // type = "cat"; name = "Floof"
this.type = type;
this.name = name;
}
toString() {
return this.name + " (" + this.type + ")"
}
}
class Cat extends Animal {
constructor(name, color) { // name = "Floof"; color = "white"
super("cat", name);
this.color = color;
}
pet() {
alert("petting")
}
toString() {
return this.name + " (" + this.type + ") is the best"
}
}
class TaskView extends Component {
constructor(props) {
super(props)
}
render() {
}
}
let animals = [
new Cat("Floof", "white"),
new Animal("dog", "Spot"),
new Animal("cat", "Max"),
new Animal("elephant", "Elle")
]
animals = [
{
type: "cat",
name: "Floof",
color: "white",
toString() {
return this.name + " (" + this.type + ") is the best"
},
pet() {
alert("petting")
}
},
{
type: "dog",
name: "Spot",
toString() {
return this.name + " (" + this.type + ")"
}
},
{
type: "cat",
name: "Max",
toString() {
return this.name + " (" + this.type + ")"
}
},
{
type: "elephant",
name: "Elle",
toString() {
return this.name + " (" + this.type + ")"
}
}
]
const myPet = new Animal("cat", "Floof");
const myPet = {
type: "cat",
name: "Floof",
toString: () => this.name + " (" + this.type + ")"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment