Skip to content

Instantly share code, notes, and snippets.

@kdefliese
Last active August 29, 2015 14:14
Show Gist options
  • Save kdefliese/cfacd59fe235978d3f56 to your computer and use it in GitHub Desktop.
Save kdefliese/cfacd59fe235978d3f56 to your computer and use it in GitHub Desktop.
Code review assignment
<!-- I added the .toUpperCase() function to my original script so the user should be able to type answers either capitalized or lowercase -->
<script>
var answer1 = prompt("Welcome to Harry Potter trivia! (Special Hermione Granger Edition) What is Hermione Granger's middle name?");
while (answer1.toUpperCase() !== "JEAN") {
var answer1 = prompt("Nope! Try again");
}
if (answer1.toUpperCase() === "JEAN") {
alert("Correct!");
}
else {
alert("Nope! Try again");
}
var answer2 = prompt("Who did Hermione punch in the face in the movie 'Harry Potter and the Prisoner of Azkaban'?");
while (answer2.toUpperCase() !== "DRACO MALFOY") {
var answer2 = prompt("Nope! Try again (make sure to have first and last name)");
}
if (answer2.toUpperCase() === "DRACO MALFOY") {
alert("Correct!");
}
else {
alert("Nope! Try again");
}
var answer3 = prompt("Who was Hermione's date to the Yule Ball?")
while (answer3.toUpperCase() !== "VICTOR KRUM") {
var answer3 = prompt("Nope! Try again (make sure to have first and last name");
}
if (answer3.toUpperCase() === "VICTOR KRUM") {
alert("Correct!");
}
else {
alert("Nope! Try again");
}
var answer4 = prompt("What do Hermione's parents do for a living?")
if (answer4.toUpperCase() === "DENTISTS") {
alert("Correct!");
}
else if (answer4.toUpperCase() === "DENTIST") {
alert("Correct!");
}
else if (answer4.toUpperCase() === "THEY'RE DENTISTS") {
alert("Correct!");
}
else if (answer4.toUpperCase() === "THEY ARE DENTISTS") {
alert("Correct!");
}
else {
alert("It's okay, this was a hard one");
}
alert("Congrats on making it through Hermione Granger trivia! Here is your reward: http://www.buzzfeed.com/danieldalton/boss-witch")
</script>
<!-- I could see that I was reusing the same code over and over for this assignment, so I decided to try making a function!
I added three chances to get the right answer for the question, and I wanted to make the user able to answer things like "Malfoy" instead
of the full name, so I was searching for some kind of contains function based on my SQL knowledge and it sounds like there isn't a widely
supported one in Javascript. I used an article where someone used the indefOf() function to make something with a similar effect:
http://adripofjavascript.com/blog/drips/determining-if-a-string-contains-another-string-in-javascript-three-approaches.html -->
<script>
var userAnswer, guessCounter
function aContainsB (a, b) {
return a.indexOf(b) >= 0;
}
function aDoesNotContainB (a, b) {
return a.indexOf(b) === -1;
}
function triviaQuestion(question, correctAnswer) {
userAnswer = prompt(question);
if (aContainsB(correctAnswer, userAnswer.toUpperCase())) {
alert("Correct!");
}
guessCounter = 2
while (aDoesNotContainB(correctAnswer, userAnswer.toUpperCase()) && guessCounter >0) {
if (guessCounter > 1) {
userAnswer = prompt("Nope! You have " + guessCounter + " more guesses. Try again. " + question);
}
else {
userAnswer = prompt("Nope! You have 1 more guess. Try again. " + question);
}
guessCounter --;
}
}
alert("Welcome to Harry Potter trivia (Special Hermione Granger edition!) Are you ready to play?");
triviaQuestion("What is Hermione Granger's middle name?","JEAN");
triviaQuestion("Who did Hermione punch in the face in the movie 'Harry Potter and the Prisoner of Azkaban?'","DRACO MALFOY");
triviaQuestion("Who was Hermione's date to the Yule Ball?","VICTOR KRUM");
triviaQuestion("What do Hermione's parents do for a living?","DENTISTS");
alert("Congrats on making it through Hermione Granger trivia! Here is your reward: http://www.buzzfeed.com/danieldalton/boss-witch")
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment