Click here to Skip to main content
14,576,820 members
Rate this:
Please Sign up or sign in to vote.
See more:
I created a javascript file for a quiz using (for )loop but it doesn't seems to work. I have worked with it a lot times.but no matter what I do it doesn't go well

What I have tried:

var problems= [
prompt:"what is your name?\n (a)deepa\n
 (b)rita\n (c)Gita",
 answer:" a"
prompt:"what is your age?\n (a)10\n
 (b)17\n (c)18",
 answer: "c"

prompt:"what is your scool?\n (a)univrsal\n
 (b)venky\n (c)kmc",
 answer: "a"


var score = 0;

for(var i=0;i<problems.length;i++ ){
 var response=  window.prompt(problems[i].prompt);
if(response==problems[i].answer ){
marks++; alert("right!!");

else{ "wrong!!!"};


alert("you got"+score+"/"+problems.length);
Updated 30-Jun-20 5:00am
Richard MacCutchan 30-Jun-20 11:10am
Your prompt; strings need to be on a single line. Javascript does not like them being broken.
Richard MacCutchan 30-Jun-20 11:20am
Note also the difference between "score" and "marks".

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

"It's not working" is one of the most useless problem descriptions we get: it tells us absolutely nothing about the problem. We don't know if you get an error message, or the wrong data, or even that that code compiles successfully!
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
So tell us what happens when you run that code, what you expected to happen, how you checked what happened. Help us to help you!

And in this case, there isn't a whole lot we can do - we have no idea what you expect that to do, much less what it actually does that it shouldn't.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your browser, but a quick Google for the name of your browser and "javascript debugging" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100