猿问

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

当我试图找到年龄与 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 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

  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.

*/  


蝴蝶刀刀
浏览 151回答 3
3回答

弑天下

var notablePeople = {&nbsp; &nbsp; "Elvis Presley": new Date(1935, 0, 8),&nbsp; &nbsp; "Sonya Sotomayor": new Date(1954, 5, 25),&nbsp; &nbsp; "Franklin D. Roosevelt": new Date(1882, 0, 30),&nbsp; &nbsp; "Ben Carson": new Date(1951, 8, 18),&nbsp; &nbsp; "Roger Staubach": new Date(1942, 1, 5),&nbsp; &nbsp; "Steve Jobs": new Date(1955, 1, 24),&nbsp; &nbsp; "Albert Einstein": new Date(1879, 2, 14),&nbsp; &nbsp; "Isaac Asimov": new Date(1919, 9, 4),&nbsp; &nbsp; "Jada Pinkett Smith": new Date(1971, 8, 18),&nbsp; &nbsp; "Grace Hopper": new Date(1906, 11, 9),&nbsp; &nbsp; "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) {&nbsp; &nbsp; // See if this person's age difference is closer&nbsp; &nbsp; console.log(person,Math.abs(notablePeople[person].getTime() - sonyaAge) / 1000 / 60 / 60 / 24 / 365);&nbsp; &nbsp; if (person != "Sonya Sotomayor" && (Math.abs(notablePeople[person].getTime() - sonyaAge) < ageDifference)) {&nbsp; &nbsp; &nbsp; &nbsp; ageDifference = Math.abs(notablePeople[person].getTime() - sonyaAge);&nbsp; &nbsp; &nbsp; &nbsp; personClosest = person;&nbsp; &nbsp; }}ageDifference = ageDifference / 1000 / 60 / 60 / 24 / 365;console.log("\nClosest age difference is " + personClosest + " with " + ageDifference + " years difference.");//您以错误的格式比较年龄,并且您使用的是 person 而不是 personClosest

慕妹3242003

我会用它reduce来解决这个问题。它的目的是获取一个数组并将其减少为单个值。在这种情况下,我选择将其简化为包含姓名和年龄差异的对象。var notablePeople = {&nbsp; "Elvis Presley": new Date(1935, 0, 8),&nbsp; "Sonya Sotomayor": new Date(1954, 5, 25),&nbsp; "Franklin D. Roosevelt": new Date(1882, 0, 30),&nbsp; "Ben Carson": new Date(1951, 8, 18),&nbsp; "Roger Staubach": new Date(1942, 1, 5),&nbsp; "Steve Jobs": new Date(1955, 1, 24),&nbsp; "Albert Einstein": new Date(1879, 2, 14),&nbsp; "Isaac Asimov": new Date(1919, 9, 4),&nbsp; "Jada Pinkett Smith": new Date(1971, 8, 18),&nbsp; "Grace Hopper": new Date(1906, 11, 9),&nbsp; "Jared Nicholas": new Date(1995, 5, 16)};// Find who is closest in age to Sonya Sotomayorvar 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)&nbsp; // filter out Sonya&nbsp; .filter(key => key !== sonya)&nbsp; // go through the rest&nbsp; .reduce((agg, cur) => {&nbsp; &nbsp; // agg is the "aggregate value"; the object containing the name and age difference&nbsp; &nbsp; // from the last loop.&nbsp; &nbsp; // cur is the name of the person whose information we're looking at now&nbsp; &nbsp; // calculate the difference between them. The difference is positive.&nbsp; &nbsp; const diff = Math.abs(notablePeople[cur].valueOf() - sonyaAge);&nbsp; &nbsp; // return an object with the difference (if it's less than any of the previous)&nbsp; &nbsp; // and the name of the current person (again, if the difference is less than any of the previous)&nbsp; &nbsp; return {&nbsp;&nbsp; &nbsp; &nbsp; ageDifference: diff < agg.ageDifference ? diff : agg.ageDifference,&nbsp;&nbsp; &nbsp; &nbsp; person: diff < agg.ageDifference ? cur : agg.person&nbsp; &nbsp; }&nbsp; },&nbsp;&nbsp; // The initial value before any people have been examined&nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; ageDifference,&nbsp;&nbsp; &nbsp; &nbsp; person: ''&nbsp;&nbsp; });// 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.`);

Qyouu

我通过基本的大小写(简化它)解决了这个问题,删除了长除法并使用普通数字来检查例程。这个问题很快就被发现了。您如何测试新的 ageDiff 无效。底壳是一种非常有用的技术。这可能不是真正“技术上”是基本外壳,但概念是相同的;尽可能降低复杂性,您通常可以更轻松地找到正在发生的事情。看看下面。它呈现正确的结果(Staubach)。替换回你的逻辑(我自己,我会省略长除法,直到你已经知道这个人是谁,如果你不确定你有合适的人,没有理由运行这个逻辑),你应该能够前进。const notablePeople = {&nbsp; "Elvis Presley": 50,&nbsp; "Sonya Sotomayor": 30,&nbsp; "Franklin D. Roosevelt": 10,&nbsp; "Roger Staubach": 20,&nbsp; "Ben Carson": 45,};console.log ( getPerson () );function getPerson () {&nbsp; &nbsp; let ageDiff = Infinity;&nbsp; &nbsp; let finalPerson = null;&nbsp; &nbsp; const sonyaAge = notablePeople["Sonya Sotomayor"];&nbsp; &nbsp; for (person in notablePeople) {&nbsp; &nbsp; &nbsp; &nbsp; if (person !== "Sonya Sotomayor") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const testAgeDiff = Math.abs(notablePeople[person] - sonyaAge);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( testAgeDiff < ageDiff ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ageDiff = testAgeDiff;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finalPerson = person;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return finalPerson;}我在这里提出的另一个提示;我故意确保我期望的目标不是列表中的最后一个条目。看到开发人员编写这样的逻辑并不少见,他们运行测试并得到预期的结果,所以他们认为没问题。如果目标是已检查集合中的最后一个条目,那么您真的不知道您是否得到了正确的答案,或者您是否只是返回集合中的最后一个条目。我移动了数字,总是得到我期望的结果。请注意,这不会执行您的代码应该执行的检查相同年龄、无效年龄等操作。但我认为通过这里的程序,你会看到逻辑错误并得到想法。最后一点;这也可以使用 Array.filter 来完成,并且您可以使其整体更简洁。可能值得研究一下。在这里,我想尽可能地贴近您的原始想法,以便您可以看到逻辑炸弹。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答