Created
August 27, 2021 03:16
-
-
Save natafaye/f43431991afdd3fc1ff127cc52e91ecf 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
/** | |
* A prompt that asks a question and only allows certain answers, case insensitive | |
*/ | |
class AcceptedAnswersPrompt { | |
constructor(question, acceptedAnswers) { | |
this.question = question; | |
this.acceptedAnswers = acceptedAnswers; | |
} | |
getAcceptedAnswer(potentialAnswer) { | |
for(let a of this.acceptedAnswers) { | |
if(a.toLowerCase() === potentialAnswer.toLowerCase()) { | |
return a; | |
} | |
} | |
return null; | |
} | |
prompt() { | |
let answer = prompt(this.question); | |
let acceptedAnswer = this.getAcceptedAnswer(answer); | |
while(acceptedAnswer === null) { | |
answer = prompt(this.question + "\n\nExcuse me, pick from this list: \n" + this.acceptedAnswers.join(", ")) | |
acceptedAnswer = this.getAcceptedAnswer(answer); | |
} | |
return acceptedAnswer; | |
} | |
} | |
// We can use it to confirm an action | |
const confirmPrompt = new AcceptedAnswersPrompt("Are you sure?", ["YES", "NO", "Y", "N"]) | |
const shouldDelete = confirmPrompt.prompt() | |
if(shouldDelete === "YES" || shouldDelete === "Y") { | |
alert("They said yes!") | |
} | |
else { | |
alert("They said no.") | |
} | |
const shouldAdd = confirmPrompt.prompt() | |
if(shouldAdd === "YES" || shouldAdd === "Y") { | |
alert("They said yes!") | |
} | |
else { | |
alert("They said no.") | |
} | |
// We can use it to ask for votes | |
const votePrompt = new QuestionPrompt("Which animal is the best?", ["Cat"]) | |
const vote = votePrompt.prompt(); | |
console.log("vote", vote); |
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
// We can use a class to make the objects | |
class Animal { | |
constructor(type, name) { | |
this.type = type; | |
this.name = name; | |
this.votes = 0; | |
} | |
toString() { | |
return this.name + " (" + this.type + ")" | |
} | |
} | |
let animals = [ | |
new Animal("cat", "Floof"), | |
new Animal("dog", "Spot"), | |
new Animal("cat", "Max"), | |
new Animal("elephant", "Elle") | |
] | |
// Or we can just make the objects ourselves | |
animals = [ | |
{ | |
type: "cat", | |
name: "Floof", | |
votes: 0, | |
toString: () => this.name + " (" + this.type + ")" | |
}, | |
{ | |
type: "dog", | |
name: "Spot", | |
votes: 0, | |
toString: () => this.name + " (" + this.type + ")" | |
}, | |
{ | |
type: "cat", | |
name: "Max", | |
votes: 0, | |
toString: () => this.name + " (" + this.type + ")" | |
}, | |
{ | |
type: "elephant", | |
name: "Elle", | |
votes: 0, | |
toString: () => this.name + " (" + this.type + ")" | |
} | |
] |
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
/** | |
* A prompt that asks a question and only allows numbers within a particular range | |
* | |
* WARNING: This code was not tested, it's just a quick proof of concept we made in class, | |
* there's a bit more we'd want to do if we wanted it to work correctly :) | |
*/ | |
class MinMaxNumberPrompt { | |
constructor(question, min, max) { | |
this.question = question; | |
this.min = min; | |
this.max = max; | |
} | |
prompt() { | |
let answer = prompt(this.question); | |
while(answer > this.max || answer < this.min) { | |
answer = prompt(this.question + "\n\nExcuse me, pick within this range \n" + this.min + " - " + this.max) | |
} | |
return answer; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment