mardi 16 juin 2015

condition with suffle in js

i'm french and I create a game on France regions.

In html, in take a map area with dreamweaver.

In js, i create a array with all regions and a second array who asks me questions randomly like this

var questions =[
    {name: 'Nord Pas de Calais', slug:'Nord Pas de Calais', result:1},
    {name: 'Picardie', slug:'Picardie', result:2},
    {name: 'Haute Normandie', slug:'Haute Normandie', result:3},
    {name: 'Champagne Ardenne', slug:'Champagne Ardenne', result:4},
    {name: 'Basse Normandie', slug:'Basse Normandie', result:5},
    {name: 'Lorraine', slug:'Lorraine', result:6},
    {name: 'Alsace', slug:'Alsace', result:7},
    {name: 'Bretagne', slug:'Bretagne', result:8},
    {name: 'Pays de la Loire', slug:'Pays de la loire', result:9},
    {name: 'Centre', slug:'Centre', result:10},
    {name: 'Bourgogne', slug:'Bourgogne', result:11},
    {name: 'Franche-Comte', slug:'Franche-Comte', result:12},
    {name: 'Rhone-Alpes', slug:'Rhone-Alpes', result:13},
    {name: 'Auvergne', slug:'Auvergne', result:14},
    {name: 'Limousin', slug:'Limousin', result:15},
    {name: 'Poitou Charente', slug:'Poitou Charente', result:16},
    {name: 'Aquitaine', slug:'Aquitaine', result:17},
    {name: 'Midi Pyrenees', slug:'Midi Pyrennees', result:18},
    {name: 'Languedoc Roussillon', slug:'Languedoc Roussillon', result:19},
    {name: 'Paca', slug:'Paca', result:20},
    {name: 'Corse', slug:'Corse', result:21},
    {name: 'Ile de France', slug:'Ile de France', result:22},
];

The random like this

Array.prototype.shuffle = function(){
    var i = this.length, j, temp;
    while(--i > 0){
        j = Math.floor(Math.random() * (i+1));
        temp = this[j];
        this[j] = this[i];
        this[i] = temp;
    }
    return this;
}
var result = questions.shuffle();

and the game like this

while (result[i]){

    current_question = result[i].name + "<br>";
    current_result = result[i].result + "<br>";     

    $('.map area').click(function(){
        //l endroit ou l on clique
        index = $(this).index();
        index += 1;

        if  (index == current_result){
            $(".compare2").html("ok");
        }

        if (index != current_result){
            $(".compare2").html("non");
        }
    });
    i++;
}

My problem is i can't go to the next question if a click on the right area.

For go to the next question, i test if l index where i click is same of result but either it does not loop or loop it to infinity.

thanks




Aucun commentaire:

Enregistrer un commentaire