0

I've been having problems with logic and i need help addressing this issue. I come up with these types of problems all the times and i cant seem to set the variables at the right places to get the right logic to occur. basically what i want in the code is that if someone inputs the wrong (addition) answer and clicks go you should push to an array the questions and the answer. so i could get a list of the questions and answers in the array after the user finishes answering a bunch of questions. if i just push the question every time on the click i will get repeats in the array. so i need a condition, something like if the last one was wrong and it is not in the array only put it in the array once. the user could keep on attempting to answer until he gets it right.

I tried to set a wrong = 0 in global scope and in the if else statement increase the counter++ and check to see if the counter is less than 2 if it is less than 2 that means the counter could be 1 which which also means that a wrong answer was attempted so that's when I push the info to the array than i reset it to 0 but thats not good it looks like wrong will always be 1

  $(document).ready( function(){

        var wrong = 0
        wrongQs = []
        clicked = false

         number1 = ~~(Math.random() * 10);
         number2 = ~~(Math.random() * 10);
        $(".numbers").children().remove()
        $(".numbers").html("<div>" + number1 +"</div><div>" + number2 + "</div>")

        $("button.go").on('click' , function(e){
            clicked = true
            console.log($(".input").val()," " ,(number1 + number2))
            //alert(+$(".input").val())
            if(+$(".input").val() == (number1 + number2)){
                 number1 = ~~(Math.random() * 10);
                 number2 = ~~(Math.random() * 10);
                $(".numbers").children().remove()
                $(".numbers").html("<div>" + number1 +"</div><div>" + number2 + "</div>")
            }else{
                wrong++
                if( wrong < 2){
                    wrongQs.push([number1, number2, number1+ number2])
                    console.log("WrongQs Array " , wrongQs)
                    wrong = 0
                }

            }
        })
    });

html:

    <div class="numbers"></div>
<input class = "input"> <button class="go">Go</button>

and how can i become a better logical programmer? I've been at this for almost 2 yrs

jack blank
  • 5,073
  • 7
  • 41
  • 73
  • This is going to be pretty broad. Off hand though, It would be better to get your data at the end either when the user leaves the final question or when they submit the whole test. I have implemented something very similar and could share the final result with you if you are interested. – Wesley Smith Jul 07 '15 at 05:17
  • if you implemented something similar in js I would be happy to look at it. maybe it will be helpful. thankyou. I just think that there is a simple answer to this question about setting the right conditions to push data to an array – jack blank Jul 07 '15 at 05:27
  • Sure there are lots of simple ways to set checks to only push the data into the array under certain situations. But, as you say, your issue is really a logical one not a programmatic one. As such, there are a lot of variables to consider. For example, should the user be allowed to got back and review their answers prior to submitting the test, just to name one. The design of the test will dictate what logic needs to be implemented. Give me a few to clean up an example of a finished product so you can see what I mean. – Wesley Smith Jul 07 '15 at 05:41
  • Take a look at http://dodsoftware.com/test/ Anser the few questions. Check out the console after you click the submit button at the end. You'll see an array showing all of the question data much the way you seem to need. Feel free to use any of it you like and Im available if you have any questions. – Wesley Smith Jul 07 '15 at 06:49
  • Thanks. I just took a quick look at it. I will go over it more tomorrow. it looks like the array of interest is questionInfo and when you click the submit button. you get the array that was produced. If that`s the case than this will be helpful. I don't want to send an ajax request. The user will be able to answer unlimited random questions then click a button and see the data(that they got wrong so far). hopefully this will give me some ideas tomorrow when i try this again. Than You so much for your help – jack blank Jul 07 '15 at 07:09
  • Yep, you got it `questionInfo` is generated whenever the submit button is clicked. It does this by looping through each question element comparing the user's input to answer stored in the element's data attribute. Hope it helps :) – Wesley Smith Jul 07 '15 at 07:19

0 Answers0