I am new to the forum and also relatively knew to JavaScript.
Looking for an expert who may be able to spare a couple of minutes of there time to help me with a fix for creating a short 3 question quiz that is called with a body onload function.
I know i am asking alot but im kind of hoping someone has something similar perhaps already created?
The quiz is to be designed to give the user two attempts at answering each question and also keep a running score and then display final score on page.
Any takers? This would be much appreciated as i no where near skilled enough
Thanks i had a look and made this attempt. I have it working so the question is being asked but i would like to give the user a second attempt and keep a running total of the users score. Then finally display the score to my page
Any help/advice at all would be massively appreciated.
PHP Code:
function askQuestion()
{
var answer1 = prompt("What is the capital of England?","");
if (answer1 == 'London')
{
alert("Correct!");
}
else
{
alert("Sorry, Try Again!");
}
}
guys thanks for all the help! This is what i have so far - all i need is for the user to get 2 attempts at each question, quick fix anyone?
PHP Code:
var total = 3;
var score = 0;
var correct = 1;
//array list with questions & answers
var questions = [
['What is the capital of England?', 'London'],
['What is the capital of Scotland?', 'Edinburgh'],
['What do you call a baby cat?', 'Kitten']
];
function askQuestion(question) //calling the function
{
var answer = prompt(question[0], ""); //using prompt to pull question from array and assing users answer to var
if (answer == question[1]) //if statement checking if answer = answer pulled from array list
{
alert("Correct! " + "You have scored " + correct + " out of " + total);
score++;
correct++
}
else
{
alert("Sorry. The correct answer is " + question[1]);
}
}
for (var i = 0; i < questions.length; i++) // counter which will end loop after 3 questions answered.
{
askQuestion(questions[i]); // indexing the array for the next question to be pulled
}
document.writeln("Well done! Your final score is " + score + " out of " + total);
Bookmarks