Skip to content

Instantly share code, notes, and snippets.

@helderberto
Forked from aegorenkov/iterators.js
Last active July 19, 2024 14:51
Show Gist options
  • Select an option

  • Save helderberto/15ae2b1b87c3f9b3cf4bd2358bccb03d to your computer and use it in GitHub Desktop.

Select an option

Save helderberto/15ae2b1b87c3f9b3cf4bd2358bccb03d to your computer and use it in GitHub Desktop.

Revisions

  1. helderberto revised this gist Jul 19, 2024. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions iterators.js
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ console.log('Hello, world!');
    function sumFunc(arr) {
    // YOUR CODE HERE
    let accumulator = 0;
    for (let i = 0; i < arr.length; i++) {
    for (let i = 0; i < arr.length; i++) {
    accumulator += arr[i];
    }
    return accumulator;
    @@ -24,7 +24,6 @@ function returnIterator(arr) {
    index++;
    return value;
    }

    }

    // Uncomment the lines below to test your work
    @@ -41,7 +40,7 @@ console.log(myIterator()); // -> should log 'd'

    function nextIterator(arr) {
    // YOUR CODE HERE
    let index = 0;
    let index = 0;
    const iterator = {
    next: function () {
    const value = arr[index];
    @@ -73,7 +72,7 @@ function sumArray(arr) {
    accumulator += next;
    next = iterator.next();
    }
    return accumulator
    return accumulator
    }

    // Uncomment the lines below to test your work
  2. @aegorenkov aegorenkov created this gist Jul 26, 2018.
    178 changes: 178 additions & 0 deletions iterators.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,178 @@
    // Type JavaScript here and click "Run Code" or press Ctrl + s
    console.log('Hello, world!');

    // CHALLENGE 1

    function sumFunc(arr) {
    // YOUR CODE HERE
    let accumulator = 0;
    for (let i = 0; i < arr.length; i++) {
    accumulator += arr[i];
    }
    return accumulator;
    }

    // Uncomment the lines below to test your work
    const array = [1, 2, 3, 4];
    console.log('Challenge 1A', sumFunc(array)); // -> should log 10

    function returnIterator(arr) {
    // YOUR CODE HERE
    let index = 0;
    return function() {
    const value = arr[index];
    index++;
    return value;
    }

    }

    // Uncomment the lines below to test your work
    const array2 = ['a', 'b', 'c', 'd'];
    const myIterator = returnIterator(array2);
    console.log('Challenge 1B', myIterator()); // -> should log 'a'
    console.log(myIterator()); // -> should log 'b'
    console.log(myIterator()); // -> should log 'c'
    console.log(myIterator()); // -> should log 'd'



    // CHALLENGE 2

    function nextIterator(arr) {
    // YOUR CODE HERE
    let index = 0;
    const iterator = {
    next: function () {
    const value = arr[index];
    index++;
    return value;
    }
    }
    return iterator;
    }

    // Uncomment the lines below to test your work
    const array3 = [1, 2, 3];
    const iteratorWithNext = nextIterator(array3);
    console.log('Challenge 2', iteratorWithNext.next()); // -> should log 1
    console.log(iteratorWithNext.next()); // -> should log 2
    console.log(iteratorWithNext.next()); // -> should log 3



    // CHALLENGE 3

    function sumArray(arr) {
    // YOUR CODE HERE
    // use your nextIterator function
    let accumulator = 0;
    let iterator = nextIterator(arr);
    let next = iterator.next();
    while(next) {
    accumulator += next;
    next = iterator.next();
    }
    return accumulator
    }

    // Uncomment the lines below to test your work
    const array4 = [1, 2, 3, 4];
    console.log('Challenge 3', sumArray(array4)); // -> should log 10



    // CHALLENGE 4

    function setIterator(set) {
    // YOUR CODE HERE
    let setIterator = set.values();
    const iterator = {
    next: function () {
    var next = setIterator.next();
    return next['value'];
    }
    }
    return iterator;
    }

    // Uncomment the lines below to test your work
    const mySet = new Set('hey');
    const iterateSet = setIterator(mySet);
    console.log('Challenge 4', iterateSet.next()); // -> should log 'h'
    console.log(iterateSet.next()); // -> should log 'e'
    console.log(iterateSet.next()); // -> should log 'y'



    // CHALLENGE 5

    function indexIterator(arr) {
    // YOUR CODE HERE
    let index = 0;
    const iterator = {
    next: function () {
    const value = arr[index];
    index++;
    return [index - 1, value];
    }
    }
    return iterator;
    }

    // Uncomment the lines below to test your work
    const array5 = ['a', 'b', 'c', 'd'];
    const iteratorWithIndex = indexIterator(array5);
    console.log('Challenge 5', iteratorWithIndex.next()); // -> should log [0, 'a']
    console.log(iteratorWithIndex.next()); // -> should log [1, 'b']
    console.log(iteratorWithIndex.next()); // -> should log [2, 'c']



    // CHALLENGE 6

    function Words(string) {
    this.str = string;
    }

    Words.prototype[Symbol.iterator] = function() {
    // YOUR CODE HERE
    let index = 0;
    const splitStr = this.str.split(/\s/);
    return {
    next: function () {
    if (index < splitStr.length) {
    const value = splitStr[index];
    index ++;
    return {value: value, done: false};
    } else {
    return { done: true };
    }
    }
    }
    }

    // Uncomment the lines below to test your work
    const helloWorld = new Words('Hello World');
    for (let word of helloWorld) { console.log('Challenge 6', word); } // -> should log 'Hello' and 'World'

    // CHALLENGE 7

    function valueAndPrevIndex(array){
    let index = 0;
    return {
    sentence: function() {
    index++;
    let indexName = index;
    if (index - 1 === 0) {
    indexName = "first";
    }
    return "" + array[index - 1] + " was found after index " + indexName;
    }
    }
    }

    const returnedSentence = valueAndPrevIndex([4,5,6])
    console.log(returnedSentence.sentence());
    console.log(returnedSentence.sentence());
    console.log(returnedSentence.sentence());
    109 changes: 109 additions & 0 deletions promises.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,109 @@
    // Challenge 1

    function sayHello() {
    function printHello() {
    console.log('Challenge 1, 5', 'Hello');
    }
    setTimeout(printHello, 1000);
    }

    // Uncomment the line below when ready
    sayHello(); // should log "Hello" after 1000ms


    // Challenge 2
    let promise = new Promise(function (resolve, reject) {
    // ADD CODE HERE
    setTimeout(resolve, 1000);
    });

    // Should print out "Resolved!"
    // ADD CODE HERE
    function displayResolve() {
    console.log('Challenge 2', 'Resolved!');
    }
    promise.then(displayResolve);


    // Challenge 3

    promise = new Promise(function(resolve, reject) {
    // ADD CODE HERE
    reject();
    })

    // Should print out "Reject!"
    // ADD CODE HERE
    function displayRejected() {
    console.log('Challenge 3', 'Rejected!');
    }
    promise.catch(displayRejected);


    // Challenge 4

    promise = new Promise(function (resolve, reject) {
    // ADD CODE HERE
    resolve();
    });

    // Uncomment the lines below when ready
    promise.then(function () {
    console.log('Challenge 4', 'Promise has been resolved!')
    });
    console.log('Challenge 4', "I'm not the promise!");


    // Challenge 5
    function delay(){
    return new Promise(function (resolve, reject) {
    setTimeout(resolve, 1000);
    });
    }

    // Uncomment the code below to test
    // This code should log "Hello" after 1000ms
    delay().then(sayHello);


    // Challenge 6
    //
    // ADD CODE BELOW
    const secondPromise = new Promise(function (resolve, reject) {
    resolve('Second!');
    });
    const firstPromise = new Promise(function (resolve, reject) {
    resolve(secondPromise);
    })

    firstPromise.then((value) => console.log('Challenge 6', value));


    // Challenge 7
    const fakePeople = [
    { name: 'Rudolph', hasPets: false, currentTemp: 98.6 },
    { name: 'Zebulon', hasPets: true, currentTemp: 22.6 },
    { name: 'Harold', hasPets: true, currentTemp: 98.3 },
    ]

    const fakeAPICall = (i) => {
    const returnTime = Math.floor(Math.random() * 1000);
    return new Promise((resolve, reject) => {
    if (i >= 0 && i < fakePeople.length) {
    setTimeout(() => resolve(fakePeople[i]), returnTime);
    } else {
    reject({ message: "index out of range" });
    }
    });
    };

    function getAllData() {
    // CODE GOES HERE
    const apiPromises = [fakeAPICall(0), fakeAPICall(1), fakeAPICall(2)]
    return Promise.all(apiPromises)
    .then(function(values) {
    return values;
    });
    }

    getAllData().then((values) => console.log('Challenge 7', values));