为了账号安全,请及时绑定邮箱和手机立即绑定

如果带有 ageDifference 的语句未正确计算最接近的年龄

如果带有 ageDifference 的语句未正确计算最接近的年龄

蝴蝶刀刀 2022-07-01 15:51:22
当我试图找到年龄与 Sonya Sotomayor 最接近的人时,出了点问题。任何人都可以检测到我的错误吗?var notablePeople = {  "Elvis Presley": new Date(1935, 0, 8),  "Sonya Sotomayor": new Date(1954, 5, 25),  "Franklin D. Roosevelt": new Date(1882, 0, 30),  "Ben Carson": new Date(1951, 8, 18),  "Roger Staubach": new Date(1942, 1, 5),  "Steve Jobs": new Date(1955, 1, 24),  "Albert Einstein": new Date(1879, 2, 14),  "Isaac Asimov": new Date(1919, 9, 4),  "Jada Pinkett Smith": new Date(1971, 8, 18),  "Grace Hopper": new Date(1906, 11, 9),  "Jared Nicholas": new Date(1995, 5, 16)};// Find who is closest in age to Sonya Sotomayorvar sonyaAge = notablePeople["Sonya Sotomayor"].getTime();var ageDifference = Infinity;var personClosest = "";for (person in notablePeople) {  // See if this person's age difference is closer  if (person != "Sonya Sotomayor" && Math.abs(notablePeople[person].getTime() - sonyaAge) < ageDifference) {    ageDifference = Math.abs(notablePeople[person].getTime() - sonyaAge);    ageDifference = ageDifference / 1000 / 60 / 60 / 24 / 365;    personClosest = person;  }}console.log("\nClosest age difference is " + person + " with " + ageDifference + " years difference.");输出:/*This is not correct Closest age difference is Jared Nicholas with 19.473858447488585 years difference.*/  
查看完整描述

3 回答

?
弑天下

TA贡献1818条经验 获得超8个赞

var notablePeople = {

    "Elvis Presley": new Date(1935, 0, 8),

    "Sonya Sotomayor": new Date(1954, 5, 25),

    "Franklin D. Roosevelt": new Date(1882, 0, 30),

    "Ben Carson": new Date(1951, 8, 18),

    "Roger Staubach": new Date(1942, 1, 5),

    "Steve Jobs": new Date(1955, 1, 24),

    "Albert Einstein": new Date(1879, 2, 14),

    "Isaac Asimov": new Date(1919, 9, 4),

    "Jada Pinkett Smith": new Date(1971, 8, 18),

    "Grace Hopper": new Date(1906, 11, 9),

    "Jared Nicholas": new Date(1995, 5, 16)

};



// Find who is closest in age to Sonya Sotomayor

var sonyaAge = notablePeople["Sonya Sotomayor"].getTime();

var ageDifference = Infinity;

var personClosest = "";


for (person in notablePeople) {

    // See if this person's age difference is closer

    console.log(person,Math.abs(notablePeople[person].getTime() - sonyaAge) / 1000 / 60 / 60 / 24 / 365);

    if (person != "Sonya Sotomayor" && (Math.abs(notablePeople[person].getTime() - sonyaAge) < ageDifference)) {

        ageDifference = Math.abs(notablePeople[person].getTime() - sonyaAge);

        personClosest = person;

    }

}

ageDifference = ageDifference / 1000 / 60 / 60 / 24 / 365;


console.log("\nClosest age difference is " + personClosest + " with " + ageDifference + " years difference.");

//您以错误的格式比较年龄,并且您使用的是 person 而不是 personClosest


查看完整回答
反对 回复 2022-07-01
?
慕妹3242003

TA贡献1824条经验 获得超6个赞

我会用它reduce来解决这个问题。它的目的是获取一个数组并将其减少为单个值。在这种情况下,我选择将其简化为包含姓名和年龄差异的对象。


var notablePeople = {

  "Elvis Presley": new Date(1935, 0, 8),

  "Sonya Sotomayor": new Date(1954, 5, 25),

  "Franklin D. Roosevelt": new Date(1882, 0, 30),

  "Ben Carson": new Date(1951, 8, 18),

  "Roger Staubach": new Date(1942, 1, 5),

  "Steve Jobs": new Date(1955, 1, 24),

  "Albert Einstein": new Date(1879, 2, 14),

  "Isaac Asimov": new Date(1919, 9, 4),

  "Jada Pinkett Smith": new Date(1971, 8, 18),

  "Grace Hopper": new Date(1906, 11, 9),

  "Jared Nicholas": new Date(1995, 5, 16)

};



// Find who is closest in age to Sonya Sotomayor

var sonya = "Sonya Sotomayor";

var sonyaAge = notablePeople[sonya].valueOf();

var ageDifference = Infinity;

var personClosest = '';

// Object.keys returns an array of property names (the people)

var personAge = Object.keys(notablePeople)

  // filter out Sonya

  .filter(key => key !== sonya)

  // go through the rest

  .reduce((agg, cur) => {

    // agg is the "aggregate value"; the object containing the name and age difference

    // from the last loop.

    // cur is the name of the person whose information we're looking at now

    // calculate the difference between them. The difference is positive.

    const diff = Math.abs(notablePeople[cur].valueOf() - sonyaAge);

    // return an object with the difference (if it's less than any of the previous)

    // and the name of the current person (again, if the difference is less than any of the previous)

    return { 

      ageDifference: diff < agg.ageDifference ? diff : agg.ageDifference, 

      person: diff < agg.ageDifference ? cur : agg.person

    }

  }, 

  // The initial value before any people have been examined

  { 

      ageDifference, 

      person: '' 

  }

);


// get the age difference in years (approx)

personAge.ageDifference = personAge.ageDifference / 1000 / 60 / 60 / 24 / 365;


console.log(`Closest age difference is ${personAge.person} with ${personAge.ageDifference} years difference.`);


查看完整回答
反对 回复 2022-07-01
?
Qyouu

TA贡献1786条经验 获得超11个赞

我通过基本的大小写(简化它)解决了这个问题,删除了长除法并使用普通数字来检查例程。这个问题很快就被发现了。您如何测试新的 ageDiff 无效。


底壳是一种非常有用的技术。这可能不是真正“技术上”是基本外壳,但概念是相同的;尽可能降低复杂性,您通常可以更轻松地找到正在发生的事情。


看看下面。它呈现正确的结果(Staubach)。替换回你的逻辑(我自己,我会省略长除法,直到你已经知道这个人是谁,如果你不确定你有合适的人,没有理由运行这个逻辑),你应该能够前进。


const notablePeople = {

  "Elvis Presley": 50,

  "Sonya Sotomayor": 30,

  "Franklin D. Roosevelt": 10,

  "Roger Staubach": 20,

  "Ben Carson": 45,

};



console.log ( getPerson () );


function getPerson () {

    let ageDiff = Infinity;

    let finalPerson = null;

    const sonyaAge = notablePeople["Sonya Sotomayor"];

    for (person in notablePeople) {

        if (person !== "Sonya Sotomayor") {

            const testAgeDiff = Math.abs(notablePeople[person] - sonyaAge);

            if ( testAgeDiff < ageDiff ) {

                ageDiff = testAgeDiff;

                finalPerson = person;

            }

        }

    }


    return finalPerson;

}

我在这里提出的另一个提示;我故意确保我期望的目标不是列表中的最后一个条目。看到开发人员编写这样的逻辑并不少见,他们运行测试并得到预期的结果,所以他们认为没问题。如果目标是已检查集合中的最后一个条目,那么您真的不知道您是否得到了正确的答案,或者您是否只是返回集合中的最后一个条目。我移动了数字,总是得到我期望的结果。


请注意,这不会执行您的代码应该执行的检查相同年龄、无效年龄等操作。但我认为通过这里的程序,你会看到逻辑错误并得到想法。


最后一点;这也可以使用 Array.filter 来完成,并且您可以使其整体更简洁。可能值得研究一下。在这里,我想尽可能地贴近您的原始想法,以便您可以看到逻辑炸弹。


查看完整回答
反对 回复 2022-07-01

添加回答

代码语言

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号