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
    26
    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,264
    Thanks
    43
    Thanked 518 Times in 512 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
    Master Coder
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    5,487
    Thanks
    26
    Thanked 754 Times in 752 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.

    Because I love FLEXBOX and tired of waiting: http://davidwalsh.name/goodbye-vendor-prefixes

  • #4
    New Coder
    Join Date
    Jul 2013
    Posts
    26
    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
    Master Coder
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    5,487
    Thanks
    26
    Thanked 754 Times in 752 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.

    Because I love FLEXBOX and tired of waiting: http://davidwalsh.name/goodbye-vendor-prefixes

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    29,546
    Thanks
    89
    Thanked 4,934 Times in 4,895 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.
    Be yourself. No one else is as qualified.


  •  

    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
    •