💾*closedRange()

JSiterationcustom generators ⟩ *closeRange()

💾 replit:closed range (generator)

👉 compare: ClosedRange (the hard way), *list()

// ⭐️ (closed) range of integers
function* closedRange(start, end) {
    for (let i = start; i <= end; i++) { yield i }
}

💈範例:


for (const i of closedRange(1, 3)) console.log(i);  // 1 2 3
[...closedRange(-2, 2)],                            // [ -2, -1, 0, 1, 2 ]

Last updated