/    Sign up×
Community /Pin to ProfileBookmark

Javascript Quiz using Arrays

Does anyone have simple code for a javascript quiz which uses arrays and outputs the answers out of 10 and as a percentage. Would be a big help to me.

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jul 06.2010 — http://javascript.internet.com/miscellaneous/basic-javascript-quiz.html

Obviously anyone will be able to check the answers if they view the source.
Copy linkTweet thisAlerts:
@walcott14authorJul 06.2010 — This is what im looking for but without the bit where it shows the score and it says you got 5 out of 10 aswell as the percentage
Copy linkTweet thisAlerts:
@walcott14authorJul 06.2010 — Needs to be in javascript
Copy linkTweet thisAlerts:
@WesterlyJul 06.2010 — It is.

AJAX = Async JavaScript and XML

Never mind. I knew I was wasting my time again.
Copy linkTweet thisAlerts:
@walcott14authorJul 06.2010 — my bad im a beginner
Copy linkTweet thisAlerts:
@Declan1991Jul 06.2010 — form.solutions.value = correctAnswers[b]/numQues*100 + "%"[/b];That should do the trick.
Copy linkTweet thisAlerts:
@walcott14authorJul 06.2010 — <HEAD>



<script language="JavaScript">




var numQues = 10;

var numChoi = 3;

var answers = new Array(10);

answers[0] = "Brazil";

answers[1] = "Uruguay";

answers[2] = "Jules Rimet Trophy";

answers[3] = "1966";

answers[4] = "Angola";

answers[5] = "Cafu";

answers[6] = "7";

answers[7] = "Jabulani";

answers[8] = "Zidane";

answers[9] = "Waddle";

// Do not change anything below here ...

function getScore(form) {

var score = 0;

var currElt;

var currSelection;

for (i=0; i<numQues; i++) {

currElt = i*numChoi;

for (j=0; j<numChoi; j++) {

currSelection = form.elements[currElt + j];

if (currSelection.checked) {

if (currSelection.value == answers[i]) {

score++;

break;

}

}

}

}

score = Math.round(score/numQues
*
100);

form.percentage.value = score + "&#37;";

var correctAnswers = "";

for (i=1; i<=numQues; i++) {

correctAnswers += i + ". " + answers[i-1] + "rn";

}

form.solutions.value = correctAnswers/numQues*100 + "%";



}

// End -->

</script>



</HEAD>



<BODY>



<h3>World Cup Quiz</h3>



<form name="quiz">

1. Who has won the World Cup the Most?

<ul style="margin-top: 1pt">

<li><input type="radio" name="q1" value="Brazil">Brazil</li>

<li><input type="radio" name="q1" value="Argentina">Argentina</li>

<li><input type="radio" name="q1" value="Italy">Italy</li>

</ul>



  • 2. First team to win the World Cup?

    <ul style="margin-top: 1pt">

    <li><input type="radio" name="q2" value="England">England</li>

    <li><input type="radio" name="q2" value="Italy">Italy</li>

    <li><input type="radio" name="q2" value="Uruguay">Uruguay</li>

    </ul>

  • 3. Original World Cup Trophy Name?

    <ul style="margin-top: 1pt">

    <li><input type="radio" name="q3" value="FIFA World Cup">FIFA World Cup</li>

    <li><input type="radio" name="q3" value="World Cup Trophy">World Cup Trophy</li>

    <li><input type="radio" name="q3" value="Jules Rimet Trophy">Jules Rimet Trophy</li>

    </ul>

    4.When did England win the World Cup?

    <ul style="margin-top: 1pt">

    <li><input type="radio" name="q4" value="1966">1966</li>

    <li><input type="radio" name="q4" value="1970">1970</li>

    <li><input type="radio" name="q4" value="1974">1974</li>

    </ul>

    5.Fewest goals conceded in the World Cup?

    <ul style="margin-top: 1pt">

    <li><input type="radio" name="q5" value="Angola">Angola</li>

    <li><input type="radio" name="q5" value="Ivory Coast">Ivory Coast</li>

    <li><input type="radio" name="q5" value="Canada">Canada</li>

    </ul>

    6.Who's played in the most World Cups?

    <ul style="margin-top: 1pt">

    <li><input type="radio" name="q6" value="Beckham">Beckham</li>

    <li><input type="radio" name="q6" value="Cafu">Cafu</li>

    <li><input type="radio" name="q6" value="Neville">Neville</li>

    </ul>

    7.In a penalty shootout how many goals have England missed?

    <ul style="margin-top: 1pt">

    <li><input type="radio" name="q7" value="5">5</li>

    <li><input type="radio" name="q7" value="6">6</li>

    <li><input type="radio" name="q7" value="7">7</li>

    </ul>

    8.Name of the World Cup Ball?

    <ul style="margin-top: 1pt">

    <li><input type="radio" name="q8" value="Nike T90">Nike T90</li>

    <li><input type="radio" name="q8" value="Jabulani">Jabulani</li>

    <li><input type="radio" name="q8" value="Mitre">Mitre</li>

    </ul>

    9.Who got sent off in the last World Cup Final?

    <ul style="margin-top: 1pt">

    <li><input type="radio" name="q9" value="Zidane">Zidane</li>

    <li><input type="radio" name="q9" value="Materazzi">Materazzi</li>

    <li><input type="radio" name="q9" value="Henry">Henry</li>

    </ul>

    10.Who missed the 1990 World Cup shootout for England?

    <ul style="margin-top: 1pt">

    <li><input type="radio" name="q10" value="Lineker">Lineker</li>

    <li><input type="radio" name="q10" value="Waddle">Waddle</li>

    <li><input type="radio" name="q10" value="Beardsley">Beardsley </li>

    </ul>


  • <input type="button" value="Get score" onClick="getScore(this.form)">

    <input type="reset" value="Clear answers">

    <p> Score = <strong><input class="bgclr" type="text" size="5" name="percentage" disabled></strong><br><br>


    </form>

    This is what I got all I need to do is add you got 5 out of ten how would I do this.
    ×

    Success!

    Help @walcott14 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.7,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...