0

I've created a multiple choice test for an rpg website.

The test has 14 questions.

Half of these questions and answers add numerical value inside 1 of 8 properties of an object called primaryResults.

The second half does exactly the same, but stores the data in an object called secondaryResults.

Now I need a way to compare all the properties of both of these objects in order to append the proper results to the user.

For example

If PrimaryResults.def > than the rest of the properties in primary.

and

If secondaryResults.def > than the rest of the properties of secondary.

Then append The div that corresponds to def/def to display.

Is there a more efficient way than

If primaryResults.def > primaryResults.ber && PrimaryResults.mnk && primaryResults.rng

Etc etc Edit: Code Added

$(".intro").appendTo('#display_box');
var question = $(".question");
var questionPosition = -1;
var results =$(".results");

var secondaryResults = {
    def:0,
    ber:0,
    mnk:0,
    rng:0,
    cle:0,
    thf:0,
    mge:0,
    dru:0,
};

var primaryResults = {
    def:0,
    ber:0,
    mnk:0,
    rng:0,
    cle:0,
    thf:0,
    mge:0,
    dru:0,
};



function clearBox(){
    $("#display_box").children().fadeOut(500).appendTo('#question_holding');
};



function cycle(){

    question.eq(questionPosition).fadeIn(500).appendTo("#display_box");
    $("#display_box").animate({scrollTop:0}, 500);


}





    $('#leftarrow').on('click', function(){
    questionPosition--;
    if (questionPosition <= -1) {questionPosition = 13};
    clearBox();
    cycle();





});





$('#rightarrow').on('click', function(){
    questionPosition++;
    if (questionPosition > 13) { questionPosition = 0};
    clearBox();
    cycle();

    if($('input[name^="answer"]:checked').length > 13 ) {
        $("#submit").css('display', 'block');
    }




});

$('#submit').on('click', function() {
$('input[name^= "answer"]:checked').each(function(){
        if ($(this).val() == "monkL"){
            secondaryResults.mnk += 1.02;

        }

        if ($(this).val() == "berserkerL"){
            secondaryResults.ber += .99;

        }

        if ($(this).val() == "defenderL"){
            secondaryResults.def += 1.01;

        }

        if ($(this).val() == "thiefL"){
            secondaryResults.thf += 1;

        }

        if ($(this).val() == "mageL"){
            secondaryResults.mge += .98;

        }

        if ($(this).val() == "clericL"){
            secondaryResults.cle += 1.03;

        }

        if ($(this).val() == "rangeL"){
            secondaryResults.rng += .97;

        }

        if ($(this).val() == "druidL"){
            secondaryResults.dru += 1.05;

        }



        })



    });


$('#submit').on('click', function() {
$('input[name^= "answer"]:checked').each(function(){
        if ($(this).val() == "monkM"){
            secondaryResults.mnk += 1.31;

        }

        if ($(this).val() == "berserkerM"){
            secondaryResults.ber += 1.29;

        }

        if ($(this).val() == "defenderM"){
            secondaryResults.def += 1.3;

        }

        if ($(this).val() == "thiefM"){
            secondaryResults.thf += 1.28;

        }

        if ($(this).val() == "mageM"){
            secondaryResults.mge += 1.27;

        }

        if ($(this).val() == "cleric"){
            secondaryResults.cle += 1.32;

        }

        if ($(this).val() == "rangeM"){
            secondaryResults.rng += 1.33;

        }

        if ($(this).val() == "druidM"){
            secondaryResults.dru += 1.26;

        }



        })



    });

$('#submit').on('click', function() {
$('input[name^= "answer"]:checked').each(function(){
        if ($(this).val() == "monkH"){
            secondaryResults.mnk += 1.5;

        }

        if ($(this).val() == "berserkerH"){
            secondaryResults.ber += 1.51;

        }

        if ($(this).val() == "defenderH"){
            secondaryResults.def += 1.52 ;

        }

        if ($(this).val() == "thiefH"){
            secondaryResults.thf += 1.49;

        }

        if ($(this).val() == "mageH"){
            secondaryResults.mge += 1.48;

        }

        if ($(this).val() == "clericH"){
            secondaryResults.cle += 1.47;

        }

        if ($(this).val() == "rangeH"){
            secondaryResults.rng += 1.53;

        }

        if ($(this).val() == "druidH"){
            secondaryResults.dru += 1.51;

        }



        })



    });

$('#submit').on('click', function() {
$('input[name^= "answer"]:checked').each(function(){
        if ($(this).val() == "monkPL"){
            secondaryResults.mnk += .96;

        }

        if ($(this).val() == "berserkerPL"){
            secondaryResults.ber += .97;

        }

        if ($(this).val() == "defenderPL"){
            secondaryResults.def += .98;

        }

        if ($(this).val() == "thiefPL"){
            secondaryResults.thf += .99;

        }

        if ($(this).val() == "magePL"){
            secondaryResults.mge += 1;

        }

        if ($(this).val() == "clericPL"){
            secondaryResults.cle += 1.01;

        }

        if ($(this).val() == "rangePL"){
            secondaryResults.rng += 1.02;

        }

        if ($(this).val() == "druidPL"){
            secondaryResults.dru += 1.03;

        }



        })



    });



$('#submit').on('click', function() {
$('input[name^= "answer"]:checked').each(function(){
        if ($(this).val() == "monkP"){
            primaryResults.mnk += 1.3;

        }

        if ($(this).val() == "berserkerPM"){
            primaryResults.ber += 1.26;

        }

        if ($(this).val() == "defenderPM"){
            primaryResults.def += 1.27;

        }

        if ($(this).val() == "thiefPM"){
            primaryResults.thf += 1.28;

        }

        if ($(this).val() == "magePM"){
            primaryResults.mge += 1.29;

        }

        if ($(this).val() == "clericPM"){
            primaryResults.cle += 1.31;

        }

        if ($(this).val() == "rangePM"){
            primaryResults.rng += 1.32;

        }

        if ($(this).val() == "druidPM"){
            primaryResults.dru += 1.33;

        }


        })


    });

$('#submit').on('click', function() {
$('input[name^= "answer"]:checked').each(function(){
        if ($(this).val() == "monkPH"){
            primaryResults.mnk += 1.46;

        }

        if ($(this).val() == "berserkerPH"){
            primaryResults.ber += 1.47;

        }

        if ($(this).val() == "defenderPH"){
            primaryResults.def += 1.48 ;

        }

        if ($(this).val() == "thiefPH"){
            secondaryResults.thf += 1.49;

        }

        if ($(this).val() == "magePH"){
            primaryResults.mge += 1.5;

        }

        if ($(this).val() == "clericPH"){
            primaryResults.cle += 1.51;

        }

        if ($(this).val() == "rangePH"){
            primaryResults.rng += 1.52;

        }

        if ($(this).val() == "druidPH"){
            primaryResults.dru += 1.536172;

        }



        $("#submit").css('display','none');
        })


    });



$('#submit').on('click',function(){

if (Object.keys(primary).every(function(key) {
return key == 'def' || primary.def > primary[key];
})) {
$(results.eq(0)).appendTo$("#display_box");
}
});

}); 




}); 
5
  • I bet ya all the programmers in here prefer to see the code ;) Commented Jun 3, 2016 at 22:21
  • Yes, there's a better way. Loop over Object.keys(primary). Commented Jun 3, 2016 at 22:26
  • Object.keys(primary).every(function(){}) Commented Jun 3, 2016 at 22:27
  • @yezzz added code = ) Commented Jun 3, 2016 at 22:32
  • We don't need all that code, especially with so many unnecessary blank lines. Just show the part that's relevant to the question. Commented Jun 4, 2016 at 0:07

1 Answer 1

1

You can use the Array.prototype.every function to test every property in the object.

var primary = {
  A: 100,
  B: 5,
  C: 6,
  D: 15,
  E: 30
};

if (Object.keys(primary).every(function(key) {
  return key == 'A' || primary.A > primary[key];
})) {
  console.log("Success!");
}

Object.keys(primary) returns an array containing all the property names in primary, i.e. ["A", "B", "C", ...].

every() then calls the function on every element of that array; it returns true if all the calls return true, otherwise it returns false.

The function then checks whether primary.A is greater than primary[key], for each given key. Testing key == 'A' keeps it from trying to compare primary.A with itself, so it effectively ignores that property in the object.

Sign up to request clarification or add additional context in comments.

10 Comments

I am a bit shaky on how your method works. (Im a pretty new coder) . Can you explain the logic to me? and did I do this correctly if (object.keys(primaryResults)).every(function(key)) { return key == 'def' || primaryResults.def > primaryResults[key]; };
Uppercase O in Object.
I've added some more explanation to the answer.
Thanks Barmar, ive only been at this about 4 months, your answer was easy to understand... One last thing. I seem to be having a weird syntax issue. I even tried copy/pasteing your code right into my file. Its giving me uncaught syntax token } , telling me my closing onload tag is unexpected Updated the script in the post
You're missing the code to run when the test is successful. if (some_condition) { stuff to do }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.