www.webdeveloper.com
Results 1 to 11 of 11

Thread: A JavaScript Quiz - Randomize so the second result isn't the same

Hybrid View

  1. #1
    Join Date
    Feb 2011
    Location
    Sapporo, Hokkaido, Japan
    Posts
    180

    Smile 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

  2. #2
    Join Date
    Feb 2011
    Location
    Sapporo, Hokkaido, Japan
    Posts
    180
    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???

  3. #3
    Join Date
    Feb 2011
    Location
    Sapporo, Hokkaido, Japan
    Posts
    180
    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();
    }
    G
    http://a4jp.com/index-en.html

  4. #4
    Join Date
    Feb 2011
    Location
    Sapporo, Hokkaido, Japan
    Posts
    180
    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...


    GCR
    My portfolio of websites and 3D images.
    http://a4jp.com/index-en.html

  5. #5
    Join Date
    Feb 2011
    Location
    Sapporo, Hokkaido, Japan
    Posts
    180

    Thumbs up Japanese text field

    I figured out how to get the enter button working properly I just made a silly typing mistake

    It looks like there are lots of people using webdeveloper.com but I just want to see if anyone is really here

    Hello Nice to meet you all.

    Here's a more difficult problem for all those super smart people

    How do I set the text in a text field to Hiragana (Japanese) only? I haven't found an examples of working code yet but I found this:
    Code:
    String.prototype.isHiragana
    Thank you.

    GCR
    Website Design and 3D art http://a4jp.com/index-en.html

  6. #6
    Join Date
    Mar 2010
    Posts
    672
    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.

  7. #7
    Join Date
    Feb 2011
    Location
    Sapporo, Hokkaido, Japan
    Posts
    180
    Thank you for your advice Jarrod1937

    I know I can set the focus of a text field with an id, by using code like this:

    Code:
    answer.focus();
    "answer" is the id of the input area.

    But how do I set the string prototype? The code below doesn't work but it is probably close...

    Code:
    answer = String.prototype.isHiragana;
    Glen
    Free study software coming soon. http://a4jp.com/index-en.html

  8. #8
    Join Date
    Feb 2011
    Location
    Sapporo, Hokkaido, Japan
    Posts
    180
    Do I have to make a function?

    Code:
    String.prototype.isHiragana = function(){
    something;
    }
    Glen
    http://a4jp.com/index-en.html

  9. #9
    Join Date
    Feb 2011
    Location
    Sapporo, Hokkaido, Japan
    Posts
    180
    Can someone please help me?

    I would love to find out how to set the the input language also character type to Hiragana.

    I know people using English don't really do this type of thing, so it is difficult to find information about doing it.

    Glen
    http://a4jp.com/index-en.html

  10. #10
    Join Date
    Feb 2011
    Location
    Sapporo, Hokkaido, Japan
    Posts
    180
    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.

    Regards,

    Glen
    Japanese Lessons for Beginners http://agreatdream.com/index-en.html

  11. #11
    Join Date
    Feb 2011
    Location
    Sapporo, Hokkaido, Japan
    Posts
    180
    I've pretty much figured out everything. I just need to find out how to set the language of the keyboard and randomize the array.

    JavaScript is kind of like ActionScript in a way

    Glen
    Online Games Coming Soon
    http://a4jp.com/index-en.html

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles