3 回答

TA貢獻1818條經(jīng)驗 獲得超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

TA貢獻1824條經(jīng)驗 獲得超6個贊
我會用它reduce來解決這個問題。它的目的是獲取一個數(shù)組并將其減少為單個值。在這種情況下,我選擇將其簡化為包含姓名和年齡差異的對象。
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.`);

TA貢獻1786條經(jīng)驗 獲得超11個贊
我通過基本的大小寫(簡化它)解決了這個問題,刪除了長除法并使用普通數(shù)字來檢查例程。這個問題很快就被發(fā)現(xiàn)了。您如何測試新的 ageDiff 無效。
底殼是一種非常有用的技術。這可能不是真正“技術上”是基本外殼,但概念是相同的;盡可能降低復雜性,您通??梢愿p松地找到正在發(fā)生的事情。
看看下面。它呈現(xiàn)正確的結(jié)果(Staubach)。替換回你的邏輯(我自己,我會省略長除法,直到你已經(jīng)知道這個人是誰,如果你不確定你有合適的人,沒有理由運行這個邏輯),你應該能夠前進。
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;
}
我在這里提出的另一個提示;我故意確保我期望的目標不是列表中的最后一個條目??吹介_發(fā)人員編寫這樣的邏輯并不少見,他們運行測試并得到預期的結(jié)果,所以他們認為沒問題。如果目標是已檢查集合中的最后一個條目,那么您真的不知道您是否得到了正確的答案,或者您是否只是返回集合中的最后一個條目。我移動了數(shù)字,總是得到我期望的結(jié)果。
請注意,這不會執(zhí)行您的代碼應該執(zhí)行的檢查相同年齡、無效年齡等操作。但我認為通過這里的程序,你會看到邏輯錯誤并得到想法。
最后一點;這也可以使用 Array.filter 來完成,并且您可以使其整體更簡潔。可能值得研究一下。在這里,我想盡可能地貼近您的原始想法,以便您可以看到邏輯炸彈。
添加回答
舉報