我該如何解決這個問題。實際上這是一個關于如何使用除錯器的練習,但我無法解決問題。
修復cutestCat
功能。cuteness
應該回傳評分最高的貓。
*/
function cutestCat(cats) {
let cutest;
let i = 0;
debugger
while (i < cats.length) {
const cat = cats[i];
if (cat > cutest) {
cutest = cat.cuteness;
}
i ;
}
return cutest;
}
const cats = [
{ name: 'Fluffy', cuteness: 9 },
{ name: 'Princess', cuteness: 6 },
{ name: 'Tiger', cuteness: 7 },
{ name: 'Indie', cuteness: 5 },
]
console.log(cutestCat(cats)); // { name: 'Fluffy', cuteness: 9 }
uj5u.com熱心網友回復:
修復任何一個...
function cutestCat(cats) {
let cutest=-MAX_SAFE_INTEGER, cutestCat;
let i = 0;
debugger
while (i < cats.length) {
const cat = cats[i];
if (cat.cuteness > cutest) { // note the new comparison
cutest = cat.cuteness;
cutestCat = cat; // note the recording of the object
}
i ;
}
return cutestCat; // note returning the object
}
或者,更漂亮...
function cutestCat(cats) {
let sorted = cats.slice().sort((a, b) => b.cuteness-a.cuteness);
return sorted.length ? sorted[0] : null;
}
uj5u.com熱心網友回復:
有兩種簡單的方法可以解決這個問題。減少
function cutestCat(array) {
return array.reduce((acc, rec) => {
console.log("acc", acc);
return acc.cuteness < rec.cuteness ? rec : acc;
});
}
回圈
function cutestCat(array) {
if (!array.length) {
return "some default value";
}
let result = array[0];
for (let i = 1; i < array.length; i = 1) {
const current = array[i];
if (current.cuteness > result.cuteness) {
result = current;
}
}
return result;
}
在您的示例中,您需要通過可愛欄位進行比較,而不是比較物件。所有物件都有不同的參考,因此您的條件將不起作用。
uj5u.com熱心網友回復:
您可以使用array.forEach
回圈遍歷它們并檢查最高的可愛度,如下所示:
function cutestCat(cats) {
let cutest = cats[0]; // to avoid null reference
cats.forEach(cat => {
if (cat.cuteness > cutest.cuteness) {
cutest = cat;
}
});
return cutest;
}
uj5u.com熱心網友回復:
function cutestCat(cats) {
let cutest;
let i = 0;
while (i < cats.length) {
const cat = cats[i];
// if there is no cutest cat to start with, it means this is the
// first cat, set it as the cutest cat and skip the cuteness
// comparison
if(!cutest) {
cutest = cat;
continue;
}
// if there is already a cat that is considered cutest,
// compare it with current cat and update cutest if
// the current cat is more cuter than the one that is already
// considered cutest
if (cat.cuteness > cutest.cuteness) {
cutest = cat;
}
i ;
}
return cutest;
}
const cats = [
{ name: 'Fluffy', cuteness: 9 },
{ name: 'Princess', cuteness: 6 },
{ name: 'Tiger', cuteness: 7 },
{ name: 'Indie', cuteness: 5 },
]
console.log(cutestCat(cats)); // { name: 'Fluffy', cuteness: 9 }
uj5u.com熱心網友回復:
你有兩個問題:
- 你需要比較貓的可愛而不是貓,即:
if (cat.cuteness > cutest.cuteness){
}
- 此比較要求
cutest
變數包含具有cuteness
屬性的貓,但開始undefined
時會給您一個錯誤。一個可能的解決方法是從它作為陣列中的第一只貓開始,然后初始化i
為0
.
這是整個片段:
function cutestCat(cats) {
let cutest = cats[0];
let i = 1;
while (i < cats.length) {
const cat = cats[i];
if (cat.cuteness > cutest.cuteness) {
cutest = cat;
}
i ;
}
return cutest;
}
const cats = [
{ name: 'Fluffy', cuteness: 9 },
{ name: 'Princess', cuteness: 6 },
{ name: 'Tiger', cuteness: 7 },
{ name: 'Indie', cuteness: 5 },
]
console.log(cutestCat(cats));
uj5u.com熱心網友回復:
問題在于回圈if
。while
您正在將 cat(object) 變數與 cutest(numeric) 變數進行比較。
因此,與其存盤cuteness
在cutest
變數中,不如存盤物件本身。然后你可以比較這兩個物體在if
條件下的可愛程度。
那是:
function cutestCat(cats) {
let cutest = cats[0];
let i = 1;
debugger
while (i < cats.length) {
const cat = cats[i];
if (cat.cuteness > cutest.cuteness) {
cutest = cat;
}
i ;
}
return cutest;
}
const cats = [
{ name: 'Fluffy', cuteness: 9 },
{ name: 'Princess', cuteness: 6 },
{ name: 'Tiger', cuteness: 7 },
{ name: 'Indie', cuteness: 5 },
];
console.log(cutestCat(cats)); // { name: 'Fluffy', cuteness: 9 }
您可能還想撰寫一些條件來檢查傳遞的陣列(即cats
)是否為空。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/507996.html
標籤:javascript 目的