mardi 14 juin 2016

Create a game of 25. Beginner Javascript edit code:

The aim of the game is to see which player gets to a score of 25 first without going over a score of 25 but I don't know how to make it so that it only stays within 25 and if a player scores over 25 they lose. They keep playing till 25 is met and the winners name will be shown in the pop up box. HELP! It is a two player game. How do I make it so that if the person presses cancel on the confirm box when asked if they want to play, it doesn't continue with the other parts of the code? Also, how do I make so that you can only enter a name with words and not numbers, and you can only enter a number between 1 and 3? I also want it to have a replay option so they can play the game again and the score is reset. How do I do this? I am only a beginner and want to learn how to code with JavaScript.

// Asks the users to confirm whether they want to play or not
var intro = confirm ('This is a two player game of 21. The first player to reach a score of 21 wins.')

if (intro == true){
  alert ('Do you have a partner ready to play?Press OK to enter your name's ')
  var player1 = prompt ('Please enter Player 1's Name!')
  var player2 = prompt ('Please enter Player 2's Name!')
} else if (confirm ==false)  { 
  alert ('We're sorry to see you go. Come back again later')
}

var score = 0;
while (score<21 ){ 
  var player1input = Number(prompt('Please input 1,2, or 3'))
  score = score + player1input
  alert ( ' The total score is now ' + score)
}

if  (score >21){
  alert ('Congratulations!' + ' ' + player1 + ' ' +  'You have won!') 
}else if (score <21){
  var player2input = Number(prompt('Please input 1,2, or 3'))
  score = score + player2input 
  alert  (player2 + ' ' + 'inputted the number ' + ' ' + player2input +  ' ' +'And the total score is now ' + score)
} else if (player2input >20){
  alert ('Congratulations!' + ' ' + player2 +  ' ' + 'You have won!')
}

Aucun commentaire:

Enregistrer un commentaire