A JavaScript Quiz - Randomize so the second result isn't the same
I usually program in ActionScript and haven't worked with any JavaScript for a really long time. So I have pasted a few chunks of code together and would like to get a bit of help if it is all right. I'm making a very simple quiz and need a bit of help with a few things.
The first thing is making sure when the code is randomized that just the first result doesn't get repeated twice.
The second thing is when I push the enter button the quiz says correct when an answer is correct but I want it to ask another question instead.
Any help is greatly appreciated.
Code:
function doSet(){ // this prepares the question
var score, prompting, answer, txt;
offset = Math.floor(Math.random()*(question.length + 1)); //get the random number
if(offset >= question.length){ // make sure its not out of range
doSet();
return;
}
score = document.getElementById('score'); //here we kill off the old text and enter in the new for the question
score.removeChild(score.firstChild);
txt = document.createTextNode('A4JP.com: What is the answer for:');
score.appendChild(txt);
prompting = document.getElementById('prompting'); //put the word to be translated into the 'prompting' div
prompting.removeChild(prompting.firstChild);
txt = document.createTextNode(english[offset]);
prompting.appendChild(txt);
answer = document.getElementById('answer'); // empty out the text field to make it blank
answer.value = '';
}
function doCheck(){ // this checks the answer
var x, y, score, txt;
score = document.getElementById('score');
score.removeChild(score.firstChild); // empty the 'score' div
x= document.getElementById('answer');//read the value in the text input box
if(x.value == question[offset]){ //react to the user input
txt = "Correct!";
}
else{
txt = question[offset];
}
txt = document.createTextNode(txt);
score.appendChild(txt); // display the output
}
function doClick(e){
var key;
if(window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox
if (key == 13)
{
doCheck();
}
}
Regards,
edit by admin;please do not post full proper names, please wait for signature permissions before posting the first link only, the 2nd link is not permitted at all. Thank you
Okay. Where are the signature settings? Also where is the second message I sent? I thought saying this code was to help people study Japanese was ok. I've been working on the databade for a while now and would like to help people.
I don't understand why I can't I say my real name here. That is weird. Is this forum filled with people doing illegal stuff or something? Please let me write my name. Otherwise how can I search for my posts on the internet???
I looked in user cp there aren't any signature settings.
Can someone help with the code to get the next question working when I press enter please?
Code:
if (key == 13)
{
some function name here ();
}
Code:
function doCheck(){ // this checks the answer
var x, y, score, txt;
score = document.getElementById('score');
score.removeChild(score.firstChild); // empty the 'score' div
x= document.getElementById('answer');//read the value in the text input box
if(x.value == question[offset]){ //react to the user input
txt = "Correct!";
something here instead of say correct
}
else{
txt = question[offset];
}
txt = document.createTextNode(txt);
score.appendChild(txt); // display the output
}
Code:
window.onload = function(){
//here is where you load in the data by using the dL function--you can add more word pairs as time permits
dL('a4jp', 'design');
dL('agreatdream', 'study);
doSet();
}
I moved the code that displays the result in the score text field. That way it is easier to get rid of the "Correct!" text.
Code:
function doCheck(){ // this checks the answer
var x, y, score, txt;
score = document.getElementById('score');
score.removeChild(score.firstChild); // empty the 'score' div
x= document.getElementById('answer');//read the value in the text input box
if(x.value == question[offset]){ //react to the user input
txt = "Correct!";
txt = document.createTextNode(txt);
score.appendChild(txt); // display the output
}
else{
txt = question[offset];
txt = document.createTextNode(txt);
score.appendChild(txt); // display the output
}
}
For some reason the box would just dissapear and the code stopped working before... Maybe I made a small mistake somewhere...
I don't work with unicode that much but if you want to limit a string to only showing japanese, then i'd look at a regex that filters all non-hiragana/katakana characters. For that i'd look here: http://blog.stevenlevithan.com/archi...ex-and-unicode
Edit: Just in case its not obvious, i believe japanese fits into the ideographic unicode space category.
Last edited by Jarrod1937; 02-11-2011 at 11:52 AM.
Ok. Maybe I'll give up on trying to get the text box working in Japanese for now.
Can someone help me make a for loop to replace the random code?
Code:
var qnumber;
function makenumber(){
for(i=0;i<question.length;i++)
{
var qnumber = i;
}
}
function doSet(){ // this prepares the question
var score, prompting, answer, txt;
/*offset = Math.floor(Math.random()*(question.length + 1)); //get the random number*/
makenumber();
offset = qnumber;
etc...
I know this doesn't work but it is probably close.
The random code works but I would like to loop through the list instead of just picking a random number. Sometimes the same questions are repeated with the random code.
Bookmarks