0

Making a JavaScript game for a school assignment and I'm in need of some help.

I've got a 5x5 grid of radio buttons, a start button and a table with a span inside to display points. Upon clicking the start button, the variable 'playing' becomes true and a random radio is selected. if a player clicks the selected radio, the points variable is +1 and another random radio is selected.

So far I've managed to get the random radio select functions to work however I'm not really sure how to go about the rest. Not too sure how to check if a player clicks on the right radio button.

function SelectRadio(){
    var array = document.getElementsByName('radio'); //Selects all the radio buttons.
    var randomNumber=Math.floor(Math.random()*25)+1; // Selects a random number.
    array[randomNumber-1].checked = true; //Selects a random from the array.
}
2
  • I agree with @Frits, I just noticed it's a homework and added the Homework tag, I will keep my answer but I strongly recommend you understand the idea and not just copy paste. Commented Sep 4, 2011 at 11:35
  • Im changing it as needed, and not asking about everything, just that which I cant actually figure out alone... The other 300 lines of php and html was the easy bit xD Commented Sep 4, 2011 at 11:38

2 Answers 2

1

I just made this jsFiddle, check it out and tell if you want other stuff.

Checkboxes Game - JSFiddle

Sign up to request clarification or add additional context in comments.

Comments

0

http://jsfiddle.net/xBbXp/

var array = document.getElementsByName('radio');
var currentlyCheckedRadio;
var points = 0;

for(var i = 0; i < array.length; i++) {
    array[i].onclick = function() {
        if(this == currentlyCheckedRadio) { // if clicked right one, increment points
            points = points + 1;
            alert('Yay, points: ' + points);
            SelectRadio(); // check another random
        } else {
            return false; // otherwise do nothing
        }
    };
}

function SelectRadio() {
    // Selects a random number.
    var randomNumber=Math.floor(Math.random()*25);

    currentlyCheckedRadio = array[randomNumber];

    //Selects a random from the array.
    currentlyCheckedRadio.checked = true;
}

SelectRadio();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.