Created
January 13, 2022 03:15
-
-
Save natafaye/1cebac8cdb19b9d53c88b038150dbc2e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 AcceptedAnswerPrompt { | |
constructor(acceptedAnswers) { | |
this.acceptedAnswers = acceptedAnswers; | |
} | |
checkIfAccepted(answer) { | |
// return true if it is in the list of accepted answer | |
for(const acceptedAnswer of this.acceptedAnswers) { | |
if(acceptedAnswer === answer) { | |
return true; | |
} | |
} | |
// false if not | |
return false; | |
} | |
promptForAnswer(question) { | |
// prompt the user for an answer | |
let answer = prompt(question); | |
// if they didn't give a good one, prompt again (loop) | |
while( !this.checkIfAccepted(answer) ) { | |
answer = prompt(question); | |
} | |
// once they give us a good one, return that answer | |
return answer; | |
} | |
} |
This file contains hidden or 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 Animal { | |
constructor(name, typeOfAnimal) { | |
this.name = name; | |
this.type = typeOfAnimal; | |
} | |
makeDisplayString() { | |
return this.type + " " + this.name | |
} | |
} | |
// We can make objects without a class | |
let cat = { | |
name: "Fluffy", | |
type: "cat", | |
makeDisplayString: () => this.type + " " + this.name | |
} | |
const dog = { | |
name: "Spot", | |
typ: "dog", | |
makeDisplayString: () => this.type + " " + this.name | |
} | |
// Or with a class | |
cat = new Animal("Fluffy", "cat"); | |
// We can access methods and properties on an object | |
cat.makeDisplayString(); | |
cat.name; | |
cat.type; | |
// We can use classes inside of functions | |
function someFunction() { | |
const someAnimal = new Animal("fdjsklfds", "fjdsklfsd") | |
} |
This file contains hidden or 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
// Yes No Prompt | |
const yesNoPrompt = new AcceptedAnswerPrompt( [ "Yes", "No", "y", "n" ]); | |
const shouldContinue = yesNoPrompt.promptForAnswer("Do you want to continue?"); | |
if(shouldContinue === "Yes" || shouldContinue === "y") { | |
alert("Continuing!") | |
} | |
else { | |
alert("Stopping!") | |
} | |
const shouldDelete = yesNoPrompt.promptForAnswer("Are you sure you want to delete?"); | |
if(shouldDelete === "Yes" || shouldDelete === "y") { | |
// delete it | |
} | |
// Vote for things | |
const votePrompt = new AcceptedAnswerPrompt( [ "cat", "dog" ]); | |
const vote = votePrompt.promptForAnswer("Which is better?") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment