Quiz script

Demians

Nuovo Utente
21 Ago 2012
9
0
0
Salve, vorrei modificare questo script in modo che generi una lista di domande casuali. Come posso fare? :book:
Codice:
<head>
<script language="JavaScript" type="text/javascript">
//<![CDATA[
<!--
// Prelevato su [url]http://www.web-link.it[/url]

var ans = new Array;
var done = new Array;
var score = 0;

ans[1] = "c";
ans[2] = "d";
ans[3] = "a";
ans[4] = "a";
ans[5] = "b";
ans[6] = "c";
ans[7] = "a";
ans[8] = "a";
ans[9] = "c";
ans[10] = "d";
ans[11] = "d";
ans[12] = "c";
ans[13] = "b";
ans[14] = "d";
ans[15] = "b";
ans[16] = "c";
ans[17] = "a";
ans[18] = "a";
ans[19] = "c";
ans[20] = "d";

function Engine(question, answer) {

        if (answer != ans[question]) {
                if (!done[question]) {
                        done[question] = -1;
                        alert("Sbagliato!\n\nIl tuo punteggio è: " + score);
                        }
                else {
                        alert("Hai gia risposto a questa domanda!");
                        }
                }
        else {
                if (!done[question]) {
                        done[question] = -1;
                        score++;
                        alert("Esatto!\n\nIl tuo punteggio è: " + score);
                        }
                else {          
                        alert("Hai gia risposto a questa domanda!");
                        }
                }
}
                
function NextLevel () 
{
        if (score == 1) {
                alert("Hai totalizzato 1 punti.");
                }
if (score == 2) {
                alert("Hai totalizzato 2 punti.");
                }
if (score == 3) {
                alert("Hai totalizzato 3 punti.");
                }
if (score == 4) {
                alert("Hai totalizzato 4 punti.");
                }
if (score == 5) {
                alert("Hai totalizzato 5 punti.");
                }
if (score == 6) {
                alert("Hai totalizzato 6 punti.");
                }
if (score == 7) {
                alert("Hai totalizzato 7 punti.");
                }
if (score == 8) {
                alert("Hai totalizzato 8 punti.");
                }
if (score == 9) {
                alert("Hai totalizzato 9 punti.");
                }
if (score == 10) {
                alert("Hai totalizzato 10 punti.");
                }
if (score == 11) {
                alert("Hai totalizzato 11 punti.");
                }
if (score == 12) {
                alert("Hai totalizzato 12 punti.");
                }
if (score == 13) {
                alert("Hai totalizzato 13 punti.");
                }
if (score == 14) {
                alert("Hai totalizzato 14 punti.");
if (score == 15) {
                alert("Hai totalizzato 15 punti.");
                }
if (score == 16) {
                alert("Hai totalizzato 16 punti.");
                }
                }
        if (score >= 7 && score <= 10) {
                alert("Impegnati di più e raggiungerai migliori traguardi!")
                location=""
                }
        else {
                alert("Accesso negato!  Ci vogliono almeno 7 risposte esatte per accedere alla prossima pagina.")
                self.location=""
                }
}

// -->
</script>
</head>
<body>

<form action="">
    <p>
        <strong><font color="#1E90FF">xxxxxxx</font></strong>
    </p>
    <input type="radio" value="a" name="1" onclick="Engine(1, this.value)" />xxxx<br />
    <input type="radio" value="b" name="1" onclick="Engine(1, this.value)" />xxxx<br />
    <input type="radio" value="c" name="1" onclick="Engine(1, this.value)" />xxxx<br />
    <input type="radio" value="d" name="1" onclick="Engine(1, this.value)" />xxxxxx<br />
    <br />

</form>
<br />

<form>
    <input type="button" style="background-color: yellow" onclick="NextLevel()" value="Quiz completato!" />
</form>
</body>
 
Ultima modifica di un moderatore:

criric

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
21 Ago 2010
5.607
54
48
TN
ad una prima letta veloce il codice mi sembra incompleto
cmq per generare un numero casuale in javascript si usa Math.random(), ti riporto un pezzodi codice di una discussione recente con cavaliere123
Codice:
 img[0] = 'fantasma.gif';
 img[1] = 'angelo.gif';
 img[2] = 'spade.gif';
 ran = Math.floor(3 * Math.random());        

alert(img[ran]);
 

Demians

Nuovo Utente
21 Ago 2012
9
0
0
Ho trovato lo script, ma le domande si ripetono.

Ciao, ho trovato questo script che genera domande casuali, ma queste possono ripetersi.
Come posso fare per evitarlo? Questo è il codice dello script.
Codice:
<html>
	<head>
	<meta http-equiv="Content-Language" content="en-us">
	<title>TITOLO</title>
	<script type="text/Javascript">
	var rightAnswer=new Array();
	var questPerPage=2;
	var quests=Array("Quest0","Quest1","Quest2","Quest3","Quest4","Quest5","Quest6","Quest7");
	var answers=Array();
	answers[0]=Array(1,"answer1","answer2","answer3","answer4")
	answers[1]=Array(2,"answer5","answer6","answer7","answer8")
	answers[2]=Array(3,"answer9","answer10","answer11","answer12")
	answers[3]=Array(4,"answer13","answer14","answer15","answer16")
	answers[4]=Array(4,"answer17","answer18","answer19","answer20")
	answers[5]=Array(3,"answer21","answer22","answer23","answer24")
	answers[6]=Array(2,"answer25","answer26","answer27","answer28")
	answers[7]=Array(1,"answer29","answer30","answer31","answer32")
	 
	function showQuestion(){
	htmlStr="";
	for(qpp=0;qpp<questPerPage;qpp++){
	 
	q=Math.floor(Math.random()*quests.length)
	htmlStr+="Q#"+qpp+": "+quests[q]+"<br>A: ";
	 
	rightAnswer[qpp]=answers[q][0]-1;
	 
	for(a=1;a<answers[q].length;a++){
	htmlStr+="<input name='ansr"+qpp+"' type='radio' value=''><label>"+answers[q][a]+"</label>  "
	}
	htmlStr+="<p>";
	}
	document.getElementById("myQuiz").innerHTML=htmlStr
	}
	function Nextlevel(){
	for (ra=0;ra<rightAnswer.length;ra++){
	radios=document.getElementsByName("ansr"+ra)
	if (radios[rightAnswer[ra]].checked){
	alert("The Answer#"+ra+" is correct");
	}else{
	alert("The Answer#"+ra+" is incorrect");
	}
	}
	showQuestion()
	}
	</script>
	</head>
	<body>
	<h1>Quiz:</h1>
	<form id="myQuiz" action=""> 
	</form>
	<form action="">
 <input type="button" style="background-color: yellow" onclick="Nextlevel()" name="button1" value="Quiz completato!" />
	<script>
	showQuestion()
	</script>
	</body>
	</html>
 

criric

Super Moderatore
Membro dello Staff
SUPER MOD
MOD
21 Ago 2010
5.607
54
48
TN
Potresti provare creando un array che comprende domande e risposte e ordinarlo casualmente
Codice:
<html>
    <head>
        <meta http-equiv="Content-Language" content="en-us">
        <title>TITOLO</title>
        <script type="text/Javascript">
            function randomFunct() {  
                return 0.5 - Math.round(Math.random());  
            }  
            var rightAnswer=new Array();
            var questPerPage=2;
            var answers=Array();
            answers[0]=Array("quest0",1,"answer1","answer2","answer3","answer4")
            answers[1]=Array("quest1",2,"answer5","answer6","answer7","answer8")
            answers[2]=Array("quest2",3,"answer9","answer10","answer11","answer12")
            answers[3]=Array("quest3",4,"answer13","answer14","answer15","answer16")
            answers[4]=Array("quest4",4,"answer17","answer18","answer19","answer20")
            answers[5]=Array("quest5",3,"answer21","answer22","answer23","answer24")
            answers[6]=Array("quest6",2,"answer25","answer26","answer27","answer28")
            answers[7]=Array("quest7",1,"answer29","answer30","answer31","answer32")
            
            answers.sort(randomFunct); 
            
            function showQuestion(){
                htmlStr="";
                for(qpp=0;qpp<questPerPage;qpp++){
	 
                    q=qpp;
                    htmlStr+= answers[q][0]+"<br>";
	 
                    rightAnswer[qpp]=answers[q][0]-1;
	 
                    for(a=2;a<answers[q].length;a++){
                        htmlStr+="<input name='ansr"+qpp+"' type='radio' value=''><label>"+answers[q][a]+"</label>  "
                    }
                    htmlStr+="<p>";
                }
                document.getElementById("myQuiz").innerHTML=htmlStr
            }
            function Nextlevel(){
                for (ra=0;ra<rightAnswer.length;ra++){
                    radios=document.getElementsByName("ansr"+ra)
                    if (radios[rightAnswer[ra]].checked){
                        alert("The Answer#"+ra+" is correct");
                    }else{
                        alert("The Answer#"+ra+" is incorrect");
                    }
                }
                showQuestion()
            }
        </script>
    </head>
    <body>
        <h1>Quiz:</h1>
        <form id="myQuiz" action=""> 
        </form>
        <form action="">
            <input type="button" style="background-color: yellow" onclick="Nextlevel()" name="button1" value="Quiz completato!" />
        </form>
        <script>
            showQuestion()
        </script>
    </body>
</html>

E' solo un idea non testata e da completare
Lascio a te l'onore
 
Discussioni simili
Autore Titolo Forum Risposte Data
L Quiz javascript funzionante da migliorare Javascript 0
T aiuto per trasformare un quiz fatto in JS in un quiz in JQUERY jQuery 0
G Quiz Javascript Javascript 4
AntoCastro [PHP] Creare un sito di quiz a catena PHP 17
R [VENDO] - Sito Notizie Virali e Quiz Compravendita siti e domini 3
V Creare quiz a "completamento" PHP 2
N Creare un quiz con php PHP 10
S Quiz a punteggio e risultati su un file PHP 0
G Aiuto progettazione DB quiz Database 0
A Caricare quiz in un sito PHP 1
B Errore quiz PHP 0
P Domande per quiz su software open source Presenta il tuo Sito 2
amphioxus creare un quiz con Javascript Javascript 0
M Semplice Quiz in Flash Flash 0
P Quiz sul vostro sito GRATIS! Altri Annunci 1
P Quiz e Test per studio e divertimento Presenta il tuo Sito 0
B come creare i quiz???? Discussioni Varie 1
F Somma di più tabelle da script Javascript 0
L Script per convertire numeri in parole Javascript 2
H Eliminazione script. Photoshop 0
S Script Google Translate scomparso HTML e CSS 3
P lanciare script asp (o php) da jquery Javascript 1
G Script notifiche dekstop aiuto Javascript 0
G [PHP] Creare script di prenotazione con controllo disponibilità. PHP 7
P Passare solo alcuni parametri a script per CSV PHP 0
M Collegamento tra form html e script php PHP 4
F Script java elenco alfabetico non funziona Javascript 3
F Script non funzionante. Devo elencare in ordine alfabetico un elenco di nominativi, ma lo script non Javascript 2
P Script upload immagini jQuery 0
M Premature end of script headers PHP 1
Cosina script data aggiornamento pagina Javascript 1
R Distribuire uno Script "Facebook Auto Post" PHP 0
F Creazione script Tv Presentati al Forum 1
N Script elenco file HTML HTML e CSS 5
felino PHP e script generazione file excel PHP 2
MarcoGrazia Se non sai se riceverai da GET o da POST, puoi verificarlo e far scegliere allo script. Snippet PHP 0
Beppe2 Ritardare esecuzione script Javascript 2
R Primo script in PHP / CSS PHP 4
felino Script PHP per leggere un file JSON. autenticazione? PHP 4
T [a pagamento] programmatore PHP che mi aiuti a migrare gli script da vecchia versione PHP a nuova Offerte e Richieste di Lavoro e/o Collaborazione 1
T script jquery non funziona più dopo il passaggio a MVC jQuery 5
F Aiuto java script Javascript 2
Max 1 [PHP] Script che funziona in locale e non online PHP 16
K Help: problema con uno script di booking in php! PHP 0
O [PHP] inviare dati da form e script ajax PHP 0
P [PHP] Aggiungere un mio script a prestashop? PHP 10
D [Javascript] inserire uno script in un file php Javascript 6
Gabriele15497514 php testo errato durante la lettura del file txt quando lo script viene eseguito contemporaneamente PHP 3
R [Javascript] Aiuto su questo script Javascript 2
M [HTML] Stesso script su 2 blog CMS (Content Management System) 4

Discussioni simili