So I have a bit of code, that once it finishes one question it stops is there anyway to once you pick a question and finish the question and get it right it goes on to the next
Sorry, but I did not understand your question clearly. Could you tell me more about it?
If you mean that when you try to type answer option in number and it never returns true then your compared the number
and the "correct" string
.
You should try to compare the same data type to get the desired result.
First of all, welcome to the world of programming, young programmer :)
Before giving a solution to your question, I would give you the following advice:
Tidy up your code. You might think you need a lot of time and effort to do this, but the truth is - you end up saving a lot of time debugging.
Professional programmers actually have most of their time spent of planning and debugging, but not coding itself. So it's important to make debugging easy. The first step to do this is to write tidy code.
Your question nicely demonstrates this: you don't know what's wrong happening and you can't see it. (It's also a pain for anyone else to read and help)
It's actually two simple problems:
sc()
functionvar x
, you need to add a return to leave the function.x = 0
instead of x == 0
x = 0
is not the same as x == 0
)So the following is the code involved:
if (x >= 6) {
alert("please pick a valid question")
sc()
} else if (x <= 5 && x > 0) {
} else if (x == 0) { // x = 0 is assignment, not comparison
alert("please pick a valid question")
sc()
return; // You have to 'return' here
// otherwise the code following the else would continue to execute after this inner-sc() returns
} else {
alert("Please pick a valid question"), sc()
}
Also, there were newlines in the strings of your quiz array.
I'm not sure if it's just your copy & pasting to Stack Overflow which added these newlines, but if not, strings should not contain newlines. If you need to add newlines, use the newline character \n
(back-slash n)