【问题标题】:Comparing two objects woth themselves and each other将两个对象与自身和彼此进行比较
【发布时间】:2016-06-04 14:18:54
【问题描述】:

我是一个非常新的程序员,我不习惯比一些 if else 语句或一些轻量级 DOM 遍历更复杂的事情。

在 stackoverflow 的帮助下,我为正在构建的 RPG 网站创建了一个测试。

14 个问题测试用户作为龙与地下城的玩家“关注”的设置,并为 8 种性格类型中的 1 种分配分数。

8 种类型中的每一种,都是两个对象的属性。主要结果和次要结果。 8 个属性中的每一个都具有相同的名称。 Defender (def) , Berserker(ber), Monk(mnk) 等(你会在代码中看到)

关键是能够得到像 Def​​/Def 或 Ber/Def 或 Mnk/Def 这样的结果 有 8x8 组合或 64 个结果。

然后一个带有 a 类结果的 div 会根据测试的最终分数将实际的 HTML 结果附加到用户的屏幕上。

我的问题:

我需要一行代码来遍历 BOTH 对象的每个属性并找到最高值,我试图将其放入 IF 语句中,从而将正确的结果附加给用户。

6/5 更新尝试 ---

使用 Azamantes 的代码建议,我发现无论每个变量的实际值是什么,我仍然可以得到 DEF/DEF 的结果。

我已经包含了 console.log 告诉我的值与显示内容的屏幕截图。

由于某种原因,当代码循环结束时

如果 ph =def 和 sh = def 它返回为真。

在图片中我圈出了一个可能的问题,我不知道我不是专家。

这可能与它在顶部显示 0 表示 Ber 并在单击时获得 ber 分数有什么关系吗? (见图)

或者它与对象本身的值有什么关系吗?值是否在翻译中丢失了?

$(window).load(function() {
$(".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,
};







let pH = 'def', sH = 'def';

Object.keys(primaryResults).map(key => {
if(primaryResults[key] > primaryResults[pH]) {
    pH = key;
}
if(secondaryResults[key] > secondaryResults[pH]) {
    sH = key;
}
});

const highestPrimary = primaryResults[pH];
const highestSecondary = secondaryResults[sH];

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

    console.log(primaryResults);
    console.log(secondaryResults);

    if (pH == 'def' && sH == 'def') {
        clearBox();
        results.eq(0).fadeIn(500).appendTo('#display_box');
    };

    if (pH == 'def' && sH == 'ber') {
        clearBox();
        results.eq(1).fadeIn(500).appendTo('#display_box');
    };

    if (pH == 'def' && sH == 'mnk') {
        clearBox();
        results.eq(2).fadeIn(500).appendTo('#display_box');
    };

        if (pH == 'def' && sH == 'rng') {
        clearBox();
        results.eq(3).fadeIn(500).appendTo('#display_box');
    };

    if (pH == 'def' && sH == 'thf') {
        clearBox();
        results.eq(4).fadeIn(500).appendTo('#display_box');
    };

    if (pH == 'def' && sH == 'cle') {
        clearBox();
        results.eq(5).fadeIn(500).appendTo('#display_box');
    };

    if (pH == 'def' && sH == 'dru') {
        clearBox();
        results.eq(6).fadeIn(500).appendTo('#display_box');
    };

    if (pH == 'def' && sH == 'mge') {
        clearBox();
        results.eq(7).fadeIn(500).appendTo('#display_box');
    };






}) 







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"){
            primaryResults.mnk += .96;

        }

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

        }

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

        }

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

        }

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

        }

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

        }

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

        }

        if ($(this).val() == "druidPL"){
            primaryResults.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');
        })


    });




















}); 

Screen cap of results

【问题讨论】:

  • “最高价值”是什么意思?两个对象具有相同的值并且该值高于所有其他值?或者其中一个对象具有比另一个对象更高的值(在同一个键中),并且同时高于自身的任何其他值(无论另一个对象在给定键中是否具有相同的“最高值”) ?
  • 对不起,我的意思是我想要每个对象中具有最高值的属性。因此,Primary 出现 Def,Secondary 出现 Mnk,然后结果附加正确的 div,在这种情况下为 def/mnk
  • 所以你想要primary 的最高值和secondary 的最高值,并且这些值不必来自两个对象中的相同键?我的意思是它可以是secondary.def(中学最高),例如primary.ber(小学最高)?
  • 昨晚我不是帮你写了这段代码吗?您希望我们写多少您的应用程序?
  • 编程不是从书本或教程中获得答案。这是一个创造性的过程,您可以在其中思考该过程,然后将其转化为代码。在某事中找到最高值应该很容易,只需遍历所有项目,将每个项目与目前最高的项目进行比较,如果当前值更高则替换它。所以你只需要知道如何循环一个对象以及如何比较变量。

标签: javascript jquery object properties


【解决方案1】:

如果我理解你,这就是你所需要的:

let primaryResults = {
    def: 0, ber: 5, mnk: 99999,
    rng: 0, cle: 1, thf: 1,
    mge: 0, dru: 1,
};
let secondaryResults = {
    def: 1, ber: 0, mnk: 0,
    rng: 1, cle: 33333333, thf: 0,
    mge: 1, dru: 0,
};

let pH = 'def', sH = 'def';
let highestPrimary, highestSecondary;

Object.keys(primaryResults).map(key => {
    if(primaryResults[key] > primaryResults[pH]) {
        pH = key;
    }
    if(secondaryResults[key] > secondaryResults[sH]) {
        sH = key;
    }
});

highestPrimary = primaryResults[pH];
highestSecondary = secondaryResults[sH];

pH - 输入最高值的primaryResults
sH - 输入最高值的resultsResults
highPrimary - primaryResults 中的最大值
highestSecondary - secondaryResults 中的最大值

【讨论】:

  • 谢谢@Azamantes 请问为什么 ph = 'def' 我可能会误会不会将 Primary 的最高值设置为 def 吗?因此结果总是以 def 的形式出现?
  • 我只是将它设置为任何恰好是“def”的随机键,因为我们正在根据主要 [pH] 检查值。如果您没有将 [pH] 设置为任何值,那么它将返回 undefined,它既不高于也不低于任何数字。
  • 我已经用您的解决方案更新了代码。我尝试询问是否highestprimary = def 和highestSecondary = def 然后将第一项附加到.result 索引中没有发生任何事情,没有结果,没有错误消息,我100%确定最高值应该是主要和次要结果中的def
  • highestPrimaryhighestSecondary 永远不会相等 def 因为它们是键,而不是值。 pHsH 是键。如果您将primaryResults.def 设置为某个最大值并将secondaryResults.def 设置为某个最大值,则pH 和sH 将同时为def
  • 什么是key?它是最高价值的占位符吗?
猜你喜欢
  • 2013-01-05
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
  • 2022-01-05
  • 2021-06-09
  • 2023-04-04
  • 2012-01-21
  • 2022-09-30
相关资源
最近更新 更多