Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created June 29, 2022 01:45
Show Gist options
  • Save natafaye/146277dad3f0cc77babc792f023152de to your computer and use it in GitHub Desktop.
Save natafaye/146277dad3f0cc77babc792f023152de to your computer and use it in GitHub Desktop.
class AcceptedAnswerPrompt {
constructor(acceptableAnswers) { // acceptableAnswers = ["cat", "dog", "unicorn"]
this.acceptableAnswers = acceptableAnswers;
}
promptForAcceptable(question) {
let answer = prompt(question); // calling normal Javascript prompt
// let foundAnswer = undefined;
// for(let i = 0; i < this.acceptableAnswers.length; i++) {
// if(this.acceptableAnswers[i] === answer) {
// foundAnswer = this.acceptableAnswers[i];
// }
// }
while( this.acceptableAnswers.find( acceptableAnswer => acceptableAnswer.toLowerCase() === answer.toLowerCase() ) === undefined ) {
//while( this.acceptableAnswers.includes(answer) ) {
answer = prompt(question + "\nIt needs to be acceptable though.")
}
return answer;
}
}
// This is what would be in the animalPrompt variable below
// {
// acceptableAnswers: ["cat", "dog", "unicorn"]
// promptForAcceptable(question) {
// let answer = prompt(question); // calling normal Javascript prompt
// // let foundAnswer = undefined;
// // for(let i = 0; i < this.acceptableAnswers.length; i++) {
// // if(this.acceptableAnswers[i] === answer) {
// // foundAnswer = this.acceptableAnswers[i];
// // }
// // }
// while( this.acceptableAnswers.find( acceptableAnswer => acceptableAnswer.toLowerCase() === answer.toLowerCase() ) === undefined ) {
// //while( this.acceptableAnswers.includes(answer) ) {
// answer = prompt(question + "\nIt needs to be acceptable though.")
// }
// return answer;
// }
// }
// ANIMAL PROMPT EXAMPLE USAGE
const animals = ["cat", "dog", "unicorn"]
const animalPrompt = new AcceptedAnswerPrompt(animals)
const bestAnimal = animalPrompt.promptForAcceptable("What's the best animal?")
const animalPicked = animalPrompt.promptForAcceptable("Which animal do you want?")
// YES NO PROMPT EXAMPLE USAGE
const yesNoPrompt = new AcceptedAnswerPrompt( [ "yes", "no" ] )
const shouldDelete = yesNoPrompt.promptForAcceptable("Are you sure you want to delete?");
class Animal {
constructor(type, name) { // type = "cat"; name = "Floof"
this.type = type;
this.name = name;
}
toString() {
return this.name + " (" + this.type + ")"
}
}
const animals = [
{
type: "cat",
name: "Floof",
toString() {
return this.name + " (" + this.type + ")"
}
},
new Animal("dog", "Rover")
]
const floof = animals[0]
floof.type = "unicorn"
console.log(animals[0].toString())
const rover = {
type: "dog",
name: "Rover",
toString() {
return this.name + " (" + this.type + ")"
}
}
console.log(floof.toString())
console.log(rover.toString())
// sample method on menu app
// you'd need to hook this up right for it to work!
markAsRead = () => {
let index = prompt('Enter the index of the book you want to mark as read:'); // "0"
this.books[index].read = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment