2

I have a question and I can't resolve it. Can you help me please? So my php code :

$aTicketsGames = array(134, 137, 165, 136, 177, 181, 190);
$b_isRapidGame = false;
  if(in_array($i_jeu, $aTicketsGames)){
      $b_isRapidGame = true;
}

In .js after click on the button I do :

 if(obj.google_analytics.is_rapid_game == true){
    var i++;
    if(i == 10){
       document.getElementById('div1').setAttribute('class','display-block');
    }
    if(i==20){
       document.getElementById('div2').setAttribute('class','display-block');  
    }
 }

The idea is : suppose I have a variable i signifying the number of games played.

if i = 10,30,50,70,90.... show div1.

if i = 20,40,60,80,100.... show div2.

I'm newbie in .js and I have no idea how to impliment this. Help me please!!!!

Thx in advance.

1
  • if you declare 'i', in this way, it will always be equal to 1, no ?
    – vincent
    Jun 8, 2015 at 9:24

3 Answers 3

3

Use style instead of class and use % for the if condition. Also, i should be set globally not locally.

   var i = 0;
   if(obj.google_analytics.is_rapid_game == true){
        i++;
        if(i % 20 == 10){
           document.getElementById('div1').setAttribute('style','display-block');
        }
        else if(i % 20 == 0 && i > 0){
           document.getElementById('div2').setAttribute('style','display-block');  
        }
     }
5
  • how does i%20 == 10 mean its for showing div1/10?
    – AmmarCSE
    Jun 8, 2015 at 9:39
  • How many times can 20 go into 10? 0 times. What is the remainder? 10. Jun 8, 2015 at 9:42
  • Ok, but what if i was 30?
    – AmmarCSE
    Jun 8, 2015 at 9:44
  • 1
    How many times can 20 go into 30? 1 time. What is the remainder (30-20)? 10. Jun 8, 2015 at 9:45
  • Oh, sorry sorry, I misunderstood the question!
    – AmmarCSE
    Jun 8, 2015 at 9:46
2

See comments inline:

var i = 0; // Define it out of `if`.

if (obj.google_analytics.is_rapid_game === true) {
    i++; // Increment by 1

    if (i && (i % 20) === 0) { // If divisible by 20
        $('#div2').show(); // Show `div2`

        // OR
        $('#div2').addClass('display-block'); // Add Class
    } else if (i && (i % 10) === 0) { // If divisible by 10
        $('#div1').hide(); // Hide `div1`

        // OR
        $('#div1').removeClass('display-block'); // Remove Class
    }
}
1
var i = 0;
if (obj.google_analytics.is_rapid_game === true) {
    i++;

    if ((i % 20) === 0) {
        $('#div2').show();**//or** $('#div2;).css('display','block');
        $('#div1').hide();**//or** $('#div1;).css('display','none');
    } else if ((i % 10) === 0) {
        $('#div1').show();
        $('#div2').hide();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.