I need to link my array which holds the questions into a prompt box one question at a time. i.e 1.What colour is grass? A)Blue B)Green C)Red [User Input] // what ever the user types is then written to an array. I only can do this which is along the wrong lines would appreciate your help <html> <body> <script type="text/javascript"> var Questions = ['What colour was Arsenals First Kit?','Arsenals Stadium Name?','Arsenals Top Scorer?','Arsenal Team Colours?','Arsenals Manager?','Who did they lose to in the Championsleague Final?','What nationalilty is Robin van Persie?','Arsenals Captain?','Where did Arsenal Finish Last Season?','Where are Arsenal based?']; var Answers = [['Red','White','Redcurrent'],['Wembley','Emirates Stadium','Highbury'],['Wright','Henry','Bergkamp'],['Red and White','Yellow','Black'],['Wenger','Taylor','Rioch'],['Real Madrid','Barcelona','Liverpool'],['German','Dutch','French'],['Clichy','Vermaelen','Fabregas'],['Second',' Third','Fourth'],['Nottingham','Birmingham','London']]; var Correct = new Array(9); var Correct = ['Redcurrant','Henry','Red and White','Wenger','Barcelona','Dutch','Fabregas','Third','London']; for (var i = 0; i < Questions.length; i++) { document.write(Questions + "<br>"); for (var j = 0; j < Answers.length; j++) { document.write(Answers[j]+"<br>"); } document.write("<br>"); } </script> </body> </html>
Maybe something like this? <html> <body> <script type="text/javascript"> var Questions = ['What colour was Arsenals First Kit?', 'Arsenals Stadium Name?', 'Arsenals Top Scorer?', 'Arsenal Team Colours?', 'Arsenals Manager?', 'Who did they lose to in the Championsleague Final?', 'What nationalilty is Robin van Persie?', 'Arsenals Captain?', 'Where did Arsenal Finish Last Season?', 'Where are Arsenal based?']; var Answers = [['Red','White','Redcurrent'], ['Wembley','Emirates Stadium','Highbury'], ['Wright','Henry','Bergkamp'], ['Red and White','Yellow','Black'], ['Wenger','Taylor','Rioch'], ['Real Madrid','Barcelona','Liverpool'], ['German','Dutch','French'], ['Clichy','Vermaelen','Fabregas'], ['Second',' Third','Fourth'], ['Nottingham','Birmingham','London']]; var meow = new Array ('Redcurrent','Wembley','Henry','Red and White','Wenger','Barcelona','Dutch','Fabregas','Third','London'); var lol = ""; for (var i = 0; i < Questions.length; i++) { document.write(Questions[i] + "<br>"); for (var j = 0; j < Answers[i].length; j++) lol += Answers[i][j]+"\n"; yeh = prompt(Questions[i] + "\n" + lol); if (yeh == meow[i]) alert("correct!"); else alert ("not correct!"); lol = ""; } </script> </body> </html> HTML: You're missing one correct element for Correct, I just randomly chose an answer for 'Arsenals Stadium Name?'
Does the user answers get stored in an array? Bit of a beginner at this as I need to users answers array to be compared to the correct answers array to output a score
No, it doesn't store the answers in an array, my js just send an alert message saying correct or not correct on each of their answers. Your javascript is pretty neat, so I figured you can do that yourself. I just gave you an idea of how you might do it.. In any case, if you're just going to output the score, you don't need to go into so much trouble such as storing each answers and compare it to the correct ones.. Just count for correct answers. =/
I wouldn't use a prompt. Use radio buttons, much more user friendly: var questions = ['What colour was Arsenals First Kit?', 'Arsenals Stadium Name?', 'Arsenals Top Scorer?', 'Arsenal Team Colours?', 'Arsenals Manager?', 'Who did they lose to in the Championsleague Final?', 'What nationalilty is Robin van Persie?', 'Arsenals Captain?', 'Where did Arsenal Finish Last Season?', 'Where are Arsenal based?']; var answers = [['Red','White','Redcurrent@'], ['Wembley@','Emirates Stadium','Highbury'], ['Wright','Henry@','Bergkamp'], ['Red and White@','Yellow','Black'], ['Wenger@','Taylor','Rioch'], ['Real Madrid','Barcelona@','Liverpool'], ['German','Dutch@','French'], ['Clichy','Vermaelen','Fabregas@'], ['Second',' Third@','Fourth'], ['Nottingham','Birmingham','London@']]; var userAnswers = new Array(); var r; for(i=0;i<questions.length;i++) { document.body.innerHTML += ("<b>" + questions[i] + "</b><br>"); for(i2=0;i2<answers[i].length;i2++) { r=document.createElement("input"); r.type="radio"; r.name="Q" + i; r.value=answers[i][i2]; document.body.appendChild(r); document.body.innerHTML+=(answers[i][i2].replace("@","") + "<br>"); } } var b = document.createElement("input"); b.type = "button"; b.value = "Check my answers"; b.onclick = function() { var rads = document.getElementsByTagName("input"); var correct = 0; for(i=0; i<rads.length; i++) { if(rads[i].type=="radio" && rads[i].checked) { userAnswers.push(rads[i].value); if(rads[i].value.search("@")>-1) correct += 1; } } window.alert(userAnswers); window.alert(correct + " out of" + (questions.length)); } document.body.appendChild(b); Code (markup): The answers that they chose are stored in the array userAnswers. Correct answers are marked with a @.
Cheers cam thats alot better I tried to make it show the percentage any ideas why it aint working. <html> <body> <script type="text/javascript"> var questions = ['What colour was Arsenals First Kit?', 'Arsenals Stadium Name?', 'Arsenals Top Scorer?', 'Arsenal Team Colours?', 'Arsenals Manager?', 'Who did they lose to in the Championsleague Final?', 'What nationalilty is Robin van Persie?', 'Arsenals Captain?', 'Where did Arsenal Finish Last Season?', 'Where are Arsenal based?']; var answers = [['Red','White','Redcurrent@'], ['Wembley','Emirates Stadium@','Highbury'], ['Wright','Henry@','Bergkamp'], ['Red and White@','Yellow','Black'], ['Wenger@','Taylor','Rioch'], ['Real Madrid','Barcelona@','Liverpool'], ['German','Dutch@','French'], ['Clichy','Vermaelen','Fabregas@'], ['Second',' Third@','Fourth'], ['Nottingham','Birmingham','London@']]; var userAnswers = new Array(); var r; for(i=0;i<questions.length;i++) { document.body.innerHTML += ("<b>" + questions + "</b><br>"); for(i2=0;i2<answers.length;i2++) { r=document.createElement("input"); r.type="radio"; r.name="Q" + i; r.value=answers[i2]; document.body.appendChild(r); document.body.innerHTML+=(answers[i2].replace("@","") + "<br>"); } } var b = document.createElement("input"); b.type = "button"; b.value = "Check my answers"; b.onclick = function() { var rads = document.getElementsByTagName("input"); var correct = 0; for(i=0; i<rads.length; i++) { if(rads.type=="radio" && rads.checked) { userAnswers.push(rads.value); if(rads.value.search("@")>-1) correct += 1; } } window.alert("you got " (correct /(questions.length)) * 100 +"% correct"); window.alert("you got " + correct + " out of" + (questions.length) ); } document.body.appendChild(b); </script> </body> </html>
you need to use the + operater to join strings with js. Also I prefer to surround any equations completely with brackets () when using them as part of a string so: window.alert("you got " [B]+[/B] ((correct /(questions.length)) * 100) + "% correct"); Code (markup): aleternatively, probably less messy, assign it to a variable: var p = (correct /(questions.length)) * 100 window.alert("you got " + p + "% correct"); Code (markup):