Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Jul 2013
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Score Not Incrementing and errors

    My Code Is about +1 score for every correct answer. Only a score of one is achieving for first question1 and when i type the correct answer for question2 score is not incrementing. My final score should be 2 since i have only two questions but whats happening in my code is that if i delete first answer 'yes' score is going to 4 and so on. Dont know where the error in my code is...

    Code:
    <div id="score" style="font: bolder 20px courier">score: 0</div>
    <input type="text" id="question" />
    <input type="text" id="question1" />
    
    <script>
    var answers = {
        'question': 'yes',
         'question1': 'no',
    
    };
    
    var score = 0;
    
    function checkResults() {
    
        var $this = $(this),
            val = $this.val().toLowerCase();
    
        for (var k in answers) {
            if (answers.hasOwnProperty(k)) {
    
                if (k == $this.attr('id') && answers[k] === val) {
                    $this.css('background-color', 'green');
                    score += 1;
    
                    break;
                } else {
                    $this.css('background-color', 'red');
                }
    
            }
        }
    
    
        if (score == 2) {
            alert('Hi Ur Score is 2');
        }
    
    
        $('#score').text('score: ' + score);
    
    }
    
    $('input').on('keyup', checkResults);
    </script>
    Last edited by kadi; 08-25-2014 at 03:24 PM.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,091
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question

    Are you trying to code this with JQuery?
    If yes, you seem to have left out loading the library.

    If not, you have left out some other significant code.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,313
    Thanks
    23
    Thanked 612 Times in 611 Posts
    Worked for me. But you have to click off your alert to get the score to change.
    Either remove the alert [BEST] or put the
    $('#score').text('score: ' + score);
    before it.

    And I added <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> to the script.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #4
    New Coder
    Join Date
    Jul 2013
    Posts
    25
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sunfighter View Post
    Worked for me. But you have to click off your alert to get the score to change.
    Either remove the alert [BEST] or put the before it.

    And I added <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> to the script.
    i check with what u said but it working for score=1 as i said, if u write no in next text box, ur score wil not go to 2. and also if u delete yes ur score will go to 4. i dont know how u said that it worked for u

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,313
    Thanks
    23
    Thanked 612 Times in 611 Posts
    i dont know how u said that it worked for u
    I just opened my mouth and the words just came out. What browser are you using? It works in FF.

    It's not the way I would do it and this line:
    'question1': 'no',
    Should not end in that comma, but it works in FF and chrome.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,979
    Thanks
    79
    Thanked 4,429 Times in 4,394 Posts
    The whole thing makes no sense. If a user types in the right answer to one or both boxes, the score gets bumped by one.

    But if he then RETURNS to that same box, changes it (to make it wrong) and then changes it BACK to the right answer, again the score is bumped by one!

    If the user repeated this 10 times, the score would be 10!

    Here's a VERY VERY simple working version that doesn't use jQuery (why would you pull in the entire jQuery library for something this simple?):
    Code:
    <html>
    <body>
    <div style="font: bolder 20px courier">score: <span id="score">0</span></div>
    <input type="text" id="question" />
    <input type="text" id="question1" />
    
    <script>
    var answers = {
        'question': 'yes',
        'question1': 'no',
    };
    
    
    function checkResults() 
    {
        var score = 0;
        for (var k in answers) 
        {
            var inp = document.getElementById(k);
            if ( inp.value == answers[k] )
            {
                ++score;
                inp.style.backgroundColor = "lightgreen";
             } else if ( inp.value != "" ) {
                inp.style.backgroundColor = "pink";
            }
     
        }
        document.getElementById("score").innerHTML = score;
    
    }
    for (var k in answers) 
    {
        document.getElementById(k).onchange = checkResults;
    }
    </script>
    </body>
    </html>
    p.s.: Yes, you could use onkeyup in place of onchange. But I don't like that, it turns the box pink even though you are in the process of typing the right answer.
    Last edited by Old Pedant; 08-26-2014 at 12:13 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •