I want it to go on to the next element in the array in a javascript quiz

前端 未结 2 1563
余生分开走
余生分开走 2021-01-23 08:27

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

相关标签:
2条回答
  • 2021-01-23 09:07

    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.

    0 讨论(0)
  • 2021-01-23 09:09

    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:

    1. You have a infinite-looping function call on your sc() function
      In the else-if case of var x, you need to add a return to leave the function.
    2. You almost always trigger this infinite-loop by mistyping x = 0 instead of x == 0
      (Assuming you know that 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)

    0 讨论(0)
提交回复
热议问题