Advertisement
Guest User

Untitled

a guest
Aug 4th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. // <script src="battleship.js"></script>
  2.  
  3. var location1 = 3;
  4. var location2 = 4;
  5. var location3 = 5;
  6.  
  7. var guess;
  8. var hits = 0;
  9. var guesses = 0;
  10.  
  11. var isSunk = false;
  12.  
  13. while (isSunk == false) {
  14. guess = prompt("Ready, aim, fire! (enter a number 0-6):");
  15. if ( guess < 0 || guess > 6 ) {
  16. alert("Please enter a valid number!");
  17. }
  18. else {
  19. guesses = guesses + 1;
  20.  
  21. if (guess == location1 || guess == location2 || guess = location3) {
  22. hits = hits + 1;
  23. alert("HIT!");
  24. else {
  25. alert("MISS!");
  26. }
  27. if (hits == 3) {
  28. isSunk = true;
  29. alert("You sank my battleship!");
  30. }
  31. }
  32. }
  33. }
  34. var stats = "You took " + guesses + " guesses to sink the battleship, " + "which means your shooting accuracy was " + (3/guesses);
  35. alert(stats);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement