Last active
March 4, 2023 18:22
-
-
Save omril1/a465fb4eee71f7beb8523608ae1efc0d to your computer and use it in GitHub Desktop.
even-iteration-abstraction.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EvensIterator { | |
constructor(realArray) { | |
this.realArray = realArray; | |
} | |
* [Symbol.iterator]() { | |
for (const n of this.realArray) { | |
if (n % 2 === 0) yield n; | |
} | |
} | |
} | |
const input = [1, 2, 3, 4, 5, 6, 78, 456, 5687, 345, 1423]; | |
// abstraction thing | |
const evens = new EvensIterator(input); | |
for (const n of evens) { | |
console.log(n); | |
} | |
// VS actually doing it | |
for (const n of input) { | |
if (n % 2 === 0) { | |
console.log(n); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment