समस्या
हमें एक JavaScript फ़ंक्शन लिखना है जो पूर्णांकों की एक सरणी लेता है। हमारे फ़ंक्शन को उन सभी पूर्णांकों का योग वापस करना चाहिए जिनका इंडेक्स सम है, जिसे अंतिम इंडेक्स पर पूर्णांक से गुणा किया गया हो।
const arr = [4, 1, 6, 8, 3, 9];
अपेक्षित आउटपुट -
const output = 117;
उदाहरण
कोड निम्न है -
const arr = [4, 1, 6, 8, 3, 9];
const evenLast = (arr = []) => {
if (arr.length === 0) {
return 0;
} else {
const sub = arr.filter((_, index) => index % 2 === 0);
const sum = sub.reduce((a, b) => a + b);
const posEl = arr[arr.length - 1];
const res = sum * posEl;
return res;
}
};
console.log(evenLast(arr));
आउटपुट
कंसोल आउटपुट निम्न है -
117