Results 1 to 2 of 2

Thread: Score Not Incrementing and errors

  1. #1
    Join Date
    Aug 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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>

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The error in logic here is that you are incrementing score regardless of whether or not it already was incremented for a correct answer to a given question and not decrementing score if a correct answer becomes an incorrect one. There could be other conceptual ways of doing this, and there are certainly other coding methods that could be used. My approach was to keep track of when a point was awarded for a question (changes/additions highlighted):

    Code:
    <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');
                    !$this.data('awarded') && ++score;
    		$this.data('awarded', true);
                    break;
                } else {
                    $this.css('background-color', 'red');
                    if($this.data('awarded')){
                       $this.data('awarded', false);
                       --score;
                    }
                }
    
            }
        }
    
    
        /* if (score == 2) {
            alert('Hi Ur Score is 2');
        } */
    
    
        $('#score').text('score: ' + score);
    
    }
    
    $('input').on('keyup', checkResults);
    </script>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. not working after score=3
    By kadi in forum JavaScript
    Replies: 0
    Last Post: 03-22-2014, 09:39 AM
  2. Should Primary Keys be self-incrementing?
    By kuau in forum MySQL and other databases
    Replies: 4
    Last Post: 07-04-2011, 07:49 PM
  3. Table not incrementing anymore @ 99 Records
    By SChaput in forum MySQL and other databases
    Replies: 1
    Last Post: 02-02-2010, 02:02 AM
  4. Incrementing Hours To Time Format
    By TimeTracker in forum JavaScript
    Replies: 7
    Last Post: 07-08-2008, 12:15 AM
  5. Send Score?
    By cursed in forum Flash
    Replies: 5
    Last Post: 10-16-2006, 05:31 AM

Bookmarks

Posting Permissions

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