mardi 5 mai 2015

Javascript random button position and counter

I am making a small tool to learn Korean characters. However i am stuck at the buttons that wont randomly arrange after being created and my counter keeps going crazy after 20 - 30 clicks.

The rest seems fine so far i think.

How i want to make it:

  • Start button starts the game
  • A timer starts running backwards(not added yet)
  • Random Korean characters are generated and put into the display div and the three buttons. (Seems to work)

  • The user has to choose, which button represents the romanized letters of the Korean character.

  • If its the right button, the user gets a point and if not, then the user loses a point.

  • After the timer runs out, the tool stops and the points are displayed along with the wrong choices.(not added yet)

I tried to make a working fiddle but its doing nothing for some reason. http://ift.tt/1ciJ2aY

I have tried to put the variables outside of the function as globals, but it didn't work, so i put them into the function as well.

I have no clue clue how to fix the points going backwards all of a sudden. And the random order of the buttons doesn't seem to work as well.

Here is my code:

var chars = [
    ['ㄱ', 'G'],
    ['ㄲ', 'GG'],
    ['ㄴ', 'N'],
    ['ㄷ', 'D'],
    ['ㄸ', 'DD'],
    ['ㄹ', 'L'],
    ['ㅁ', 'M'],
    ['ㅂ', 'B'],
    ['ㅃ', 'BB'],
    ['ㅅ', 'S'],
    ['ㅆ', 'SS'],
    ['ㅇ', 'NG'],
    ['ㅈ', 'J'],
    ['ㅉ', 'JJ'],
    ['ㅊ', 'CH'],
    ['ㅋ', 'K'],
    ['ㅌ', 'T'],
    ['ㅍ', 'P'],
    ['ㅎ', 'H'],
    ['ㅏ', 'A'],
    ['ㅐ', 'AE'],
    ['ㅑ', 'YA'],
    ['ㅒ', 'YAE'],
    ['ㅓ', 'EO'],
    ['ㅔ', 'E'],
    ['ㅕ', 'YEO'],
    ['ㅖ', 'YE'],
    ['ㅗ', 'O'],
    ['ㅛ', 'YO'],
    ['ㅜ', 'U'],
    ['ㅠ', 'YU'],
    ['ㅡ', 'EU'],
    ['ㅣ', 'I']
];




var points = 0;
var contents = new Array();
var randomNumber0 = Math.floor((Math.random() * 33) - 1);
var randomNumber1 = Math.floor((Math.random() * 33) - 1);
var randomNumber2 = Math.floor((Math.random() * 33) - 1);
var randomChar = chars[randomNumber0][0];
var randomChar0 = chars[randomNumber0][1];
var randomChar1 = chars[randomNumber1][1];
var randomChar2 = chars[randomNumber2][1];

var f = "'";

document.getElementById("letterToGuess").innerHTML = randomChar;



var i = 0;
var random;
var spacing = " ";

while (i < contents.length) {

    random = Math.floor(Math.random() * contents.length);


    i++;

}



contents[0] = '<button id=' + randomChar0 + ' onclick="check(' + f + randomChar0 + f + ');">' + randomChar0 + '</Button>';
contents[1] = '<button id=' + randomChar1 + ' onclick="check(' + f + randomChar1 + f + ');">' + randomChar1 + '</Button>';
contents[2] = '<button id=' + randomChar2 + ' onclick="check(' + f + randomChar2 + f + ');">' + randomChar2 + '</Button>';

document.getElementById("response").innerHTML = contents;





document.getElementById('btn').addEventListener(onclick).function.check();


function check(char) {




    if (char === randomChar0) {

        points++;
        document.getElementById("points").innerHTML = points;

        randomNumber0 = Math.floor((Math.random() * 33) - 1);
        randomNumber1 = Math.floor((Math.random() * 33) - 1);
        randomNumber2 = Math.floor((Math.random() * 33) - 1);
        randomChar = chars[randomNumber0][0];
        randomChar0 = chars[randomNumber0][1];
        randomChar1 = chars[randomNumber1][1];
        randomChar2 = chars[randomNumber2][1];

        f = "'";
        contents[0] = '<button id=' + randomChar0 + ' onclick="check(' + f + randomChar0 + f + ');">' + randomChar0 + '</Button>';
        contents[1] = '<button id=' + randomChar1 + ' onclick="check(' + f + randomChar1 + f + ');">' + randomChar1 + '</Button>';
        contents[2] = '<button id=' + randomChar2 + ' onclick="check(' + f + randomChar2 + f + ');">' + randomChar2 + '</Button>';

        document.getElementById("response").innerHTML = contents;
        document.getElementById("letterToGuess").innerHTML = randomChar;
    } else {
        points--;
        document.getElementById("points").innerHTML = points;
    }
}




Aucun commentaire:

Enregistrer un commentaire