Skip to content

Instantly share code, notes, and snippets.

@natafaye
Created July 21, 2021 00:44
Show Gist options
  • Save natafaye/80c819da1a5fbaa7ba0df003725bc217 to your computer and use it in GitHub Desktop.
Save natafaye/80c819da1a5fbaa7ba0df003725bc217 to your computer and use it in GitHub Desktop.
class Animal {
constructor(type, name) {
this.type = type;
this.name = name;
this.votes = 0;
}
// Method using arrow function syntax
toString = () => this.name + " (" + this.type + ")";
}
// This class doesn't do much, probably isn't worth making, but it's an example of extending
class Cat extends Animal {
constructor(name) {
super("cat", name);
}
}
let animals = [
{
type: "cat",
name: "Floof",
votes: 0,
},
{
type: "dog",
name: "Spot",
votes: 0
},
{
type: "cat",
name: "Max",
votes: 0
},
{
type: "elephant",
name: "Elle",
votes: 0
}
]
// We could also create our animals using a class:
animals = [
new Animal("cat", "Floof"),
new Animal("dog", "Spot"),
new Cat("Max"), // We could even use our Cat class that extends Animal
new Animal("elephant", "Elle")
]
function start() {
// const cats = animals.filter(animal => animal.type === "cat")
// const catString = cats.join(", ")
// alert(catString)
alert(animals.filter(animal => animal.type === "cat").join(", "))
}
class QuestionPrompt {
constructor(question, acceptedAnswers) {
this.question = question;
this.acceptedAnswers = acceptedAnswers;
}
// Handle upper and lower case, find the accepted answer that matches
getAcceptedAnswer = (answer) => {
return this.acceptedAnswers.find(accepted => accepted.toLowerCase() === answer.toLowerCase())
}
prompt() {
let answer = prompt(this.question);
// Keep asking for an answer until they give one that matches the accepted answers
while (this.getAcceptedAnswer(answer) === undefined) {
answer = prompt(this.question + "\n\nAcceptable Answers: " + this.acceptedAnswers.join(", "));
}
return this.getAcceptedAnswer(answer);
}
}
let animals = [
{
type: "cat",
name: "Floof",
votes: 0,
},
{
type: "dog",
name: "Spot",
votes: 0
},
{
type: "cat",
name: "Max",
votes: 0
},
{
type: "elephant",
name: "Elle",
votes: 0
}
]
const animalNames = animals.map(animal => animal.name); // ["Floof", "Spot", "Max", ...]
// We can make a prompt for picking an animal to view
const ViewAnimalPrompt = new QuestionPrompt("What animal do you want to view?", animalNames);
function start() {
let animalName = ViewAnimalPrompt.prompt();
const animal = animals.find(a => a.name === animalName);
alert(animal.name + " - " + animal.type);
// We could use it multiple times, if we wanted to
animalName = ViewAnimalPrompt.prompt();
animal = animals.find(a => a.name === animalName);
alert(animal.name + " - " + animal.type);
// We could also make a prompt for the difficulty of a game
const DifficultyPrompt = new QuestionPrompt("What difficulty?", ["Hard", "Intermediate", "Easy"]);
let difficulty = DifficultyPrompt.prompt();
alert("Your difficult is " + difficulty);
}
class YesNoPrompt extends QuestionPrompt {
constructor(question) {
// Call the constructor on QuestionPrompt with these two parameters
super(question, ["Yes", "No", "Y", "N"]);
}
prompt() {
// Call the prompt function on QuestionPrompt and get the value returned from it
let answer = super.prompt()
// Convert it into a boolean and return that
let booleanAnswer = (answer === "Yes" || answer === "Y");
return booleanAnswer;
}
}
const ShouldContinuePrompt = new YesNoPrompt("Should we continue?");
// You could use this to confirm if they want to delete
const ShouldIDeletePrompt = new YesNoPrompt("Should I delete?")
function start() {
const shouldContinue = ShouldContinuePrompt.prompt();
// alert(shouldContinue);
if(shouldContinue) {
// Continue with something
}
// You could use it again if you need to ask the question again
const shouldContinueAgain = ShouldContinuePrompt.prompt();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment