lundi 9 février 2015

JavasScript Game for Class

I'm having a lot trouble with writing my own Javascript code in an introductory compScie class. I have to make a game that checks a the colors that make up another color like purple = red and blue. I'm doing this by a checkbox in my html. However, I'm having trouble validating the answer because my code marks everything correct and lets the user continue on. So my game really is missing a big component to it.



var aColor = ["purple", "pink", "orange", "yellow", "black", "turquoise"];
var obj = document.getElementById("BD");
var cColor_index= 0;
var cColor = "Color";
var changeState = 0;
var selchb = "n";
function getColors(frm){
var gColor = [];
var inputs = frm.getElementsByTagName('INPUT');
var n_inputs = inputs.length;

for(var i=0; i<n_inputs; i++) {
if(inputs[i].type == 'checkbox' && inputs[i].checked == true) gColor.push(inputs[i].value);
}

return gColor;
}


function change() {
cColor_index= parseInt( Math.random() * aColor.length);
cColor = aColor[cColor_index];
if (changeState <= 3) {
obj.innerHTML = cColor;
} else {
obj.innerHTML = "Another Round?";
changeState = 0;
}

document.getElementById('send').addEventListener("click", checkBoxArray);


}

function checkBoxArray(){
selchb = getColors(this.form);
if (cColor == "purple" && selchb.value == "red", "blue" ){
obj.innerHTML = "Good Job, onto the next one";
changeState++;
}
if (cColor == "black" && selchb.value == "red", "blue", "green" ){
obj.innerHTML = "Good Job, onto the next one";
changeState++;
}
}




Aucun commentaire:

Enregistrer un commentaire