我的任務是撰寫函式,該函式從陣列中回傳偶數的平均值。如果陣列中沒有偶數,則回傳 null。以下是陣列和預期結果: [1,2,3,4,5,6,7] - 應該回傳“4”,[1,1,1,1] - 應該回傳“null”。我應該在哪里設定 null 的條件?我嘗試了不同的選項,但沒有任何效果,但也許我的代碼是錯誤的。
function getEvenAverage(array) {
const even = array.filter(function (element) {
return element % 2 === 0
});
const sum = even.reduce(function (sum, element) {
return (sum element);
});
return sum / even.length;
}
const result1 = getEvenAverage([1,2,3,4,5,6,7])
console.log(result1);
const result2 = getEvenAverage([1,1,1,1])
console.log(result2);
uj5u.com熱心網友回復:
您也可以在 1 個回圈中實作此目的:
邏輯:
- 創建一個物件(比如
map)來保存值 - 在陣列上回圈。
- 檢查當前專案是否是偶數,計算總數和長度。將其設定為
map - 最后,檢查長度和回傳值
function getEvenAverage(array) {
const map = array.reduce((acc, item) => {
if (item % 2 === 0) {
return { total: acc.total item, length: acc.length 1 }
}
return acc
}, { total: 0, length: 0 });
return map.length ? map.total / map.length : null
}
const result1 = getEvenAverage([1, 2, 3, 4, 5, 6, 7])
console.log(result1);
const result2 = getEvenAverage([1, 1, 1, 1])
console.log(result2);
uj5u.com熱心網友回復:
我認為您可以按長度檢查。
function getEvenAverage(array) {
const even = array.filter(function (element) {
return element % 2 === 0
});
const sum = even.length > 0 ? even.reduce(function (sum, element) {
return (sum element);
}) : null;
return sum / even.length;
}
const result1 = getEvenAverage([1,2,3,4,5,6,7])
console.log(result1);
const result2 = getEvenAverage([1,1,1,1])
console.log(result2);
uj5u.com熱心網友回復:
檢查此代碼
function getEvenAverage (arr) {
let temp = [];
let sum = 0;
let count = 0;
arr.map(item => {
if(item % 2 === 0) {
temp = [...temp, item]
}
})
temp.map(item => {
sum = item;
count =1;
})
if(temp.length ===0) {
return null
}
return sum/count;
}
uj5u.com熱心網友回復:
您需要進行以下更改:
- 添加
null為Array.prototype.reduce的初始值 - 檢查是否
sum === null回傳sum,否則計算平均值
您可以在下面找到已更改應用的代碼
function getEvenAverage(array) {
const even = array.filter(function (element) {
return element % 2 === 0
});
const sum = even.reduce(function (sum, element) {
return (sum element);
}, null);
return sum === null ? sum : sum / even.length;
}
const result1 = getEvenAverage([1,2,3,4,5,6,7])
console.log(result1);
const result2 = getEvenAverage([1,1,1,1])
console.log(result2);
uj5u.com熱心網友回復:
最初作為對此答案的簡單編輯發布。在下面作為單獨的答案重新發布(作為另一個答案,Rajesh回滾并指出這將單獨發布)。
添加了評論,因為這現在作為單獨的答案發布。
// method to get average of even numbers in array
const getEvenAverage = (arr) => (
arr // use optional-chainging "?." to handle bad "arr" values
?.reduce( // iterate over the array
(acc, itm, idx) => { // access "acc" (accumulator), "itm" and "idx"
if (itm % 2 === 0) { // if itm is even number
acc.tot = itm; // add it to "acc" total (ie, "tot" prop)
acc.len ; // increment the "len" prop of "acc"
};
if ( // if processing last "itm"
idx === arr.length - 1 &&
acc.len > 0 // and have previously processed even number "itm"
) { // update "result" prop of "acc"
acc.result = acc.tot / acc.len;
};
return acc; // return the updated "acc" for each iteration
}, // initialize "acc" to have three props
{ tot: 0, len: 0, result: null }
) // simply send the "result" back to the caller
?.result
);
console.log(
'call fn with array: 1,2...7\naverage-of-even-numbers: ',
getEvenAverage([1, 2, 3, 4, 5, 6, 7])
);
console.log(
'call fn with array: 1,1,1,1\naverage-of-even-numbers: ',
getEvenAverage([1, 1, 1, 1])
);
console.log(
'call fn with empty-array: []\naverage-of-even-numbers: ',
getEvenAverage([])
);
console.log(
'call fn with undefined: \naverage-of-even-numbers: ',
getEvenAverage()
);
.as-console-wrapper { max-height: 100% !important; top: 0 }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/480903.html
標籤:javascript 数组 功能
