Last active
July 7, 2021 01:24
-
-
Save natafaye/30fc6a6053f8f9bcc0be274efacedd34 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
function getAnimalListString(animals) { | |
// We can do this with reduce or map & join | |
// return animals.reduce(function(animalListString, animal) { | |
// return animalListString + "\n" + animal.name + " (" + animal.type + ")"; | |
// }, "Here are the animals:"); | |
return "Here are the animals:\n" + animals.map(function(animal) { | |
return animal.name + " (" + animal.type + ")"; | |
}).join("\n") | |
} | |
function getVote(animals) { | |
const promptMessage = getAnimalListString(animals) + "\nWhat's your vote?"; | |
const vote = prompt(promptMessage); | |
while(getAnimalByName(vote, animals) === undefined) { | |
vote = prompt("PLEASE ENTER THE NAME OF AN ANIMAL.\n" + promptMessage); | |
} | |
return vote; | |
} | |
function getAnimalByName(name, animals) { | |
return animals.find(function(animal) { | |
return animal.name.toLowerCase() === name.toLowerCase(); | |
}); | |
} | |
function getVotingResultsString(animals) { | |
return animals.map(function(animal) { | |
return animal.votes + " - " + animal.name + " (" + animal.type + ")"; | |
}).join("\n"); | |
} | |
function getWinner(animals) { | |
// Use reduce to figure out who won | |
return animals.reduce(function(currentWinner, animal) { | |
// We first check that currentWinner isn't undefined, then that it has more votes than the animal we're checking | |
if(currentWinner && currentWinner.votes > animal.votes) { | |
// If the animal we're checking has less votes, then currentWinner stays what it is | |
return currentWinner; | |
} | |
else { | |
// If the animal we're checking has more votes | |
// (or currentWinner was undefined, meaning we're checking the first animal), | |
// then the animal in our animal variable will get dumped into currentWinner | |
return animal; | |
} | |
}) | |
} | |
function voteOnAnimals() { | |
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 | |
} | |
] | |
// These three lines do the same thing as line 77: | |
// const vote = getVote(animals); | |
// const animal = getAnimalByName(vote, animals); | |
// animal.votes++; | |
getAnimalByName(getVote(animals), animals).votes++; | |
getAnimalByName(getVote(animals), animals).votes++; | |
getAnimalByName(getVote(animals), animals).votes++; | |
// You could copy these lines more times if you wanted to vote more times | |
alert(getVotingResultsString(animals)); | |
alert("The winner is:\n\n" + getWinner(animals).name); | |
} |
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
function isYesNo(answer) { | |
return answer.toLowerCase() === "yes" || answer.toLowerCase() === "no"; | |
} | |
function convertYesNoToBoolean(answer) { | |
return answer.toLowerCase() === "yes"; // true if answer is "yes", false for everything else | |
} | |
function askBooleanQuestion(question) { | |
let answer = prompt(question); | |
while(!isYesNo(answer)) { | |
answer = prompt("You need to say yes or no! " + question); | |
} | |
return convertYesNoToBoolean(answer); | |
} | |
function askQuestions() { | |
// FILTER | |
const likedFruits = fruits.filter(function(fruit) { | |
return askBooleanQuestion("Do you like " + fruit + "?"); | |
}) | |
alert(likedFruits); | |
// REDUCE | |
const numLikedFoods = fruits.reduce(function(numLiked, fruit) { | |
const isLiked = askBooleanQuestion("Do you like " + fruit + "?"); | |
if(isLiked) { | |
return numLiked + 1; | |
} | |
return numLiked; // We could also put this line in an else, but we don't need to | |
}, 0) | |
alert("You like " + numLikedFruits + " fruits in the list."); | |
} |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<button onclick="voteOnAnimals()">Button</button> | |
<script src="week3.js"></script> | |
</body> | |
</html> |
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
function isYesNo(answer) { | |
return answer.toLowerCase() === "yes" || answer.toLowerCase() === "no"; | |
} | |
function askYesNoQuestion(question) { | |
let answer = prompt(question); | |
while(!isYesNo(answer)) { | |
answer = prompt("You need to say yes or no! " + question); | |
} | |
return answer; | |
} | |
function askQuestions() { | |
const tomatoAnswer = askYesNoQuestion("Do you like tomatoes?"); | |
alert(tomatoAnswer); | |
const mangoAnswer = askYesNoQuestion("Do you like mangoes?"); | |
alert(mangoAnswer); | |
let fruits = ["mangoes", "bananas", "apples"]; | |
// We can do this with a for loop or a forEach | |
// for(let fruit of fruits) { | |
// const answer = askYesNoQuestion("Do you like " + fruit + "?"); | |
// alert(answer); | |
// } | |
fruits.forEach(function(fruit) { | |
const answer = askYesNoQuestion("Do you like " + fruit + "?"); | |
alert(answer); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment