हमें एक ऐरे फ़ंक्शन लिखना है (वे फ़ंक्शन जो Array.prototype ऑब्जेक्ट पर रहते हैं)। फ़ंक्शन को एक स्टार्ट इंडेक्स और एक एंड इंडेक्स लेना चाहिए और ऐरे में स्टार्ट इंडेक्स से एंड इंडेक्स तक के सभी एलिमेंट्स का योग करना चाहिए (स्टार्ट और एंड दोनों सहित)।
उदाहरण
const arr = [1, 2, 3, 4, 5, 6, 7]; const sumRange = function(start = 0, end = 1){ const res = []; if(start > end){ return res; }; for(let i = start; i <= end; i++){ res.push(this[i]); }; return res; }; Array.prototype.sumRange = sumRange; console.log(arr.sumRange(0, 4));आउटपुट
कंसोल में आउटपुट होगा -
[ 1, 2, 3, 4, 5 ]