Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Random selection for quiz script???

    Hey Everyone!

    I am trying to adapt the script found at the following page:

    http://www.javascriptkit.com/script/...omboquiz.shtml

    I am trying to find a modification to where instead of having the questions appear in numbered order, to generate in a random order?

    Any input would be appreciated!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,906
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You can easily alter the popsolution.js file as follows:-

    Code:
    var total=5
    var question=new Array()
    for (i=1;i<=total+1;i++){
    j = parseInt(Math.random() * total+1);  // 1-5
    temp="choice"+j+"=new Array()"
    eval(temp);
    }
    But of course this means that the same question may be repeated.

    If you want to allow each question to appear only once you need to keep a record of which questions have already been selected.


    "There is hardly anything in the world that some man cannot make a little worse and sell a little cheaper, and the people who consider price only are this man's lawful prey." John Ruskin
    Last edited by Philip M; 04-22-2008 at 10:32 AM. Reason: Add quote of day

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    726
    Thanks
    35
    Thanked 132 Times in 123 Posts
    You might try adapting something like this:

    Code:
    function Qwiz(){
    	var q= [
    	['2', 'Any fool can make a rule, and any fool will mind it.'],
    	['5', 'Always forgive your enemies, nothing annoys them so much.'],
    	['4', 'An ignorant person is one who doesn\'t know what you have just found out.'],
    	['3', 'Beware of the young doctor and the old barber.'],
    	['1', 'Better to remain silent and be thought a fool than to speak out and remove all doubt.']
    	]
    	this.questions= q.sort(function(a,b){return Math.random()- .5});
    	this.selectfrom= '1. Abraham Lincoln,2. Henry David Thoreau,3. Benjamin Franklin,4. Will Rogers,5. Oscar Wilde';
    }
    Qwiz.prototype.next= function(){
    	var score= 0, temp= '';
    	while(this.questions.length){
    	
    		var tem= this.questions.shift();
    		var A= this.selectfrom.split(',');
    		var i= A.length;
    		var q= tem[1];
    		var a= tem[0];
    		var s= A.join('\n');
    		var s2= '\n'+q+'\n\nWho said it? Pick a number.\n\n'+s;
    		var el= prompt(temp+s2)
    		if(el.charAt(0)!== a.charAt(0)){
    			var el= prompt('Try again?\n'+s2);
    			if(!el || el.charAt(0)!= a.charAt(0)){
    				i= 0;
    			}
    		}
    		if(el.charAt(0)== a.charAt(0)){
    			score+= i;
    			temp= ('That is correct! '+score + 'points\n' );
    		}
    		else temp= 'Sorry. Wrong answer.\n';
    	}
    	var final= score>20? (score==25? 'Perfect!\n': 'Excellent!\n'): 'Final score: ';
    	alert(final +score +' Points')
    }
    var Quiz= new Qwiz();
    Quiz.next()


  •  

    Posting Permissions

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