diff --git a/guessing_game_ryan b/guessing_game_ryan new file mode 160000 index 0000000..e1cd62b --- /dev/null +++ b/guessing_game_ryan @@ -0,0 +1 @@ +Subproject commit e1cd62bf76ae64c6c4c2f796adc65e08a1c81750 diff --git a/guessinggame.html b/guessinggame.html index db112a6..b88d475 100644 --- a/guessinggame.html +++ b/guessinggame.html @@ -1,62 +1,42 @@ - + + + + -// Asks the user to guess how many states I, the developer, -// have lived in. The user keeps guessing with guidance -// until they get the right answer. Once the user gets the -// right answer they will be asked to guess one of the states -// I have lived in. The user gets one shot to correctly guess -// one of the states I have lived in. - -// Prompt the user for their guess -var userGuess = prompt("How many states have I lived in?"); -console.log("The user guessed" + userGuess); - -var answer = 7; -var numbTries = 1; -var message; -// Make the user keep guessing until they get it right. -// Hints are offered. -while (userGuess != answer && numbTries <= 5) { - if (userGuess <= 6) { - var userGuess = prompt("That's a little too low. Try again."); - } else if (userGuess >= 8) { - var userGuess = prompt("That's a bit too high. Guess again."); - } - numbTries +1; -} - -if (userGuess >=5){ - message = "The right answer is 7, Can you name one of the states?"; -}else { - message = "Are you a stalker? You are correct. Can you name one of the states?"; -} - - -// Once the user gets the right answer they're asked to name -// one of the states I've lived in. They get one chance. -var stateGuess = prompt(message); - -var states = ["michigan", "maryland", "virginia", "new york", "colorado", "alaska", "washington" ] - - -// Idea to write this search function from http://stackoverflow.com/questions/143847/best-way-to-find-an-item-in-a-javascript-array -// and from the codecademy homework. - -function include(array1, string1) { - for (var i = 0; i < array1.length; i++) { - if (string1.toLowerCase() == array1[i]) { - return true; - } - } -} - -// call the 'include' function to see if the guess is in the array of right -// answers. -if (include(states,stateGuess)) { - alert("Yup. I've lived there!"); -} else { - alert("I haven't lived there yet but perhaps some day I will."); -} - - - diff --git a/guessinggame.js b/guessinggame.js new file mode 100644 index 0000000..4369f08 --- /dev/null +++ b/guessinggame.js @@ -0,0 +1,75 @@ +// THIS INSTANTIATES THE GAME +// var game5 = new Game(["Metallica", "Red Hot Chili Peppers","311","Melvins"]); + +// var sam = new Game(["Devo", "The Replacements", "Elvis Costello", "Jack White", "Bootsy Collins"]); + +// THIS RUNS THE GAME +// game5.run(); + +var Game = function(answer) { + + this.answer = answer; + this.concerts = answer.length; // property, instance.concerts + // this.counter = 0; + var guess = ""; + + this.run = function() { // method, instance.run() + + var counter = 0; + var play = confirm("Do you want to guess how many concerts I have been to in 2015?"); + + console.log("The user guessed " + guess); + console.log("The correct answer is " + this.concerts); + + while (play && guess != this.concerts && counter < 10) { + + if (counter === 0) { + guess = prompt("Can you guess how many concerts I have been to in 2015?"); + } + + ++counter; + console.log("Counter after increment " + counter); + + if (guess <= (this.concerts - 1)) { + var el = document.getElementById("result"); + el.innerHTML = "I pity the fool that would guess that low! Guess again!" + //guess = prompt("I pity the fool that would guess that low! Guess again!"); + + } else if (guess >= (this.concerts + 1)) { + var el = document.getElementById("result") + el.innerHTML = "Ain't nobody got time for that many concerts!" + //guess = prompt("Ain't nobody got time for that many concerts! Guess again!"); + } + } + + console.log("While loop over after 10 fails"); + + if (guess != this.concerts) { + alert("You aren't my real friend. You don't know me at all!"); + } else { + alert("It only took you " + counter + " guesses! You must have gone to some shows with me. Here's who I saw: "); + } + + for (var i = 0; i < this.concerts; i++) { + alert(this.answer[i]); + } + + alert(this.answer[0]); + alert(this.answer[1]); + alert(this.answer[2]); + alert(this.answer[3]); + + }; +} + +// var game3 = new Game(["dogs","cats","snakes"]); +// game3.run(); +// var game4 = new Game ([1,2,3,4,5,6,7,8,9,10]); +// game4.run(); + +// THIS INSTANTIATES THE GAME +var game5 = new Game(["Metallica", "Red Hot Chili Peppers","311","Melvins"]); + +// THIS RUNS THE GAME +game5.run(); + diff --git a/guessinggame.js~ b/guessinggame.js~ new file mode 100644 index 0000000..b4ded24 --- /dev/null +++ b/guessinggame.js~ @@ -0,0 +1,58 @@ + +// Asks the user to guess how many states I, the developer, +// have lived in. The user keeps guessing with guidance +// until they get the right answer. Once the user gets the +// right answer they will be asked to guess one of the states +// I have lived in. The user gets one shot to correctly guess +// one of the states I have lived in. + +// Prompt the user for their guess +var userGuess = prompt("How many states have I lived in?"); +console.log("The user guessed" + userGuess); + +var answer = 7; +var numbTries = 1; +var message; +// Make the user keep guessing until they get it right. +// Hints are offered. +while (userGuess != answer && numbTries <= 5) { + if (userGuess <= 6) { + var userGuess = prompt("That's a little too low. Try again."); + } else if (userGuess >= 8) { + var userGuess = prompt("That's a bit too high. Guess again."); + } + numbTries +1; +} + +if (userGuess >=5){ + message = "The right answer is 7, Can you name one of the states?"; +}else { + message = "Are you a stalker? You are correct. Can you name one of the states?"; +} + + +// Once the user gets the right answer they're asked to name +// one of the states I've lived in. They get one chance. +var stateGuess = prompt(message); + +var states = ["michigan", "maryland", "virginia", "new york", "colorado", "alaska", "washington" ] + + +// Idea to write this search function from http://stackoverflow.com/questions/143847/best-way-to-find-an-item-in-a-javascript-array +// and from the codecademy homework. + +function include(array1, string1) { + for (var i = 0; i < array1.length; i++) { + if (string1.toLowerCase() == array1[i]) { + return true; + } + } +} + +// call the 'include' function to see if the guess is in the array of right +// answers. +if (include(states,stateGuess)) { + alert("Yup. I've lived there!"); +} else { + alert("I haven't lived there yet but perhaps some day I will."); +}