Domanda

I made this 'game', and now I want to show some message (winner) when are all values of input's equals zero, and I want to show message when 3 field values are less than 0.

examples: when the fields are like this (connect link)

http://prntscr.com/3jgvz6 

then user is winner, but when it is like this (just two fields with 0)

http://prntscr.com/3jgw6f

then user is automatically loser.

Link to jsbin : http://jsbin.com/kezesefu/1

My code is so long to put it here..

User lose the game when of 25 fields are just two 0. Ex.

2 3 1 2 0
2 2 1 1 1
3 0 3 1 2
2 3 4 1 2

There is just two zeros, so user is loser.

È stato utile?

Soluzione

Working demo

Try adding this to the end of your document ready function:

function verifyWinningConditions () {
    var nonZeroBoxes = 25;
    $('input').each(function(){
        if($(this).val() == 0) {
            nonZeroBoxes--;
        }
    });

    if (nonZeroBoxes == 0) {
        alert('WINNER!! All 25 boxes are 0!');
    }

    if(nonZeroBoxes >= 23) {
        alert('LOSER! Only 2 out of 25 boxers were 0!');
    }
}

$('input').on('click', verifyWinningConditions);

Altri suggerimenti

Something like this:

var inputs = $('input'), sum;
$('button').on('click', function(){
  sum=0;
  inputs.each(function(){
  sum += +$(this).val();
 });
if(sum > 0){
  console.log('not all zeros');
 }else{
  console.log('all zeros');
}
});

DEMO

DEMO

$('.inputi').click(function() {
   var counter = 0; 
   $(".inputi").each(function() { 
     if(this.value==0){
         counter= counter+1;
       }
   });

   if(counter==0) {
     // show message here
     alert("You won!");
   } 
   if(counter < 3 & counter >0) {
    //show message when 3 field values are less than 0.
    alert("Sorry, you lost the game");
   }
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top