Szybki trening JavaScript

Iteratory

Itertatory 01

                
                    let numbers = [10,11,12];

                    console.log(typeof numbers[Symbol.iterator]);
                
            

💡 Co pojawi się w konsoli ?

Itertatory 01

                
                    let numbers = [10,11,12];

                    console.log(typeof numbers[Symbol.iterator]);
                
            

💡 Co pojawi się w konsoli ?

                    
                        function
                    

Itertatory 02

                
                    let numbers = [10,11,12];
                    let it = numbers[Symbol.iterator]();
                    
                    console.log(it.next());
                
            

💡 Co pojawi się w konsoli ?

Itertatory 02

                
                    let numbers = [10,11,12];
                    let it = numbers[Symbol.iterator]();
                    
                    console.log(it.next());
                
            

💡 Co pojawi się w konsoli ?

                    
                        {value: 10, done: false}
                    
                

Itertatory 03

                
                    let numbers = [10,11,12];
                    let it = numbers[Symbol.iterator]();
                    
                    console.log(it.next());
                    console.log(it.next());
                    console.log(it.next());
                
            

💡 Co pojawi się w konsoli ?

Itertatory 03

                
                    let numbers = [10,11,12];
                    let it = numbers[Symbol.iterator]();
                    
                    console.log(it.next());
                    console.log(it.next());
                    console.log(it.next());
                
            

💡 Co pojawi się w konsoli ?

                    
                        {value: 10, done: false}, {value: 11, done: false}, {value: 12, done: false},
                    
                

Itertatory 04

                
                    let numbers = [10,11,12];
                    let it = numbers[Symbol.iterator]();
                    it.next()
                    it.next()
                    it.next()
                    console.log(it.next());
                
            

💡 Co pojawi się w konsoli ?

Itertatory 04

                
                    let numbers = [10,11,12];
                    let it = numbers[Symbol.iterator]();
                    it.next()
                    it.next()
                    it.next()
                    console.log(it.next());
                
            

💡 Co pojawi się w konsoli ?

                    
                        {value: undefined, done: true}
                    
                
                
                    let numberCreator = {
                        [Symbol.iterator]() {
                            let nextN = 10;
                            return {
                                next() {
                                    return {
                                        value: nextN++,
                                        done:false
                                    };
                                }
                         }
                    }};
                    
                    let it = numberCreator[Symbol.iterator]();
                    console.log(it.next().value);
                    console.log(it.next().value);
                
            

Itertatory 05

💡 Co pojawi się w konsoli ?

                
                    let numberCreator = {
                        [Symbol.iterator]() {
                            let nextN = 10;
                            return {
                                next() {
                                    return {
                                        value: nextN++,
                                        done:false
                                    };
                                }
                         }
                    }};
                    
                    let it = numberCreator[Symbol.iterator]();
                    console.log(it.next().value);
                    console.log(it.next().value);
                
            

Itertatory 05

💡 Co pojawi się w konsoli ?

                
                    10
                    11
                
            
                
                    let numberCreator = {
                        [Symbol.iterator]() {
                            let nextN = 10;
                            return {
                                next() {
                                    return {
                                        value: nextN++,
                                        done:false
                                    };
                                }
                         }
                    }};
                    
                    let it = numberCreator[Symbol.iterator]();
                    
                    
                    for (let v of numberCreator) {
                        if (v > 100) break;
                        console.log(v);
                    
                    }
                
            

Itertatory 07

💡 Co pojawi się w konsoli ?

                
                    let numberCreator = {
                        [Symbol.iterator]() {
                            let nextN = 10;
                            return {
                                next() {
                                    return {
                                        value: nextN++,
                                        done:false
                                    };
                                }
                         }
                    }};
                    
                    let it = numberCreator[Symbol.iterator]();
                    
                    
                    for (let v of numberCreator) {
                        if (v > 100) break;
                        console.log(v);
                    
                    }
                
            

Itertatory 07

💡 Co pojawi się w konsoli ?

                
                    0
                    1
                    2
                    3
                    ...
                    100
                
            
                
                    let numberCreator = {
                        [Symbol.iterator]() {
                            let nextN = 10;
                            return {
                                next() {
                                    if (next > 100)
                                    let value = nextN > 100?undefined:nextN++;
                                    let done = !value;
                                    return {
                                        value,
                                        done
                                    };
                                }
                         }
                    }};
                    
                    for (let v of numberCreator) {
                        console.log(v);
                    
                    }
                
            

Itertatory 08

💡 Co pojawi się w konsoli ?

                
                    let numberCreator = {
                        [Symbol.iterator]() {
                            let nextN = 10;
                            return {
                                next() {
                                    if (next > 100)
                                    let value = nextN > 100?undefined:nextN++;
                                    let done = !value;
                                    return {
                                        value,
                                        done
                                    };
                                }
                         }
                    }};
                    
                    for (let v of numberCreator) {
                        console.log(v);
                    
                    }
                
            

Itertatory 08

💡 Co pojawi się w konsoli ?

                
                    0
                    1
                    2
                    3
                    ...
                    100
                
            
                
                    let numbers = [10,11,12];

                    function process(n1,n2,n3) {
                        console.log(n3);
                    }
                    
                    process(...numbers);
                
            

Itertatory 09

💡 Co pojawi się w konsoli ?

                
                    let numbers = [10,11,12];

                    function process(n1,n2,n3) {
                        console.log(n3);
                    }
                    
                    process(...numbers);
                
            

Itertatory 09

💡 Co pojawi się w konsoli ?

                
                    12
                
            

Generator

                
                    function *process() {
                        yield 10;
                        yield 11;
                        yield 12;
                    }
                    
                    let it = process();
                    console.log(it.next());
                
            

Generator 1

💡 Co pojawi się w konsoli ?

                
                    function *process() {
                        yield 10;
                        yield 11;
                        yield 12;
                    }
                    
                    let it = process();
                    console.log(it.next());
                
            

Generator 1

💡 Co pojawi się w konsoli ?

                
                    {value: 10, done: false}
                
            
                
                    function *process() {
                        yield 10;
                        yield 11;
                        yield 12;
                    }
                    
                    let it = process();
                    it.next()
                    it.next()
                    it.next()
                    console.log(it.next());
                
            

Generator 2

💡 Co pojawi się w konsoli ?

                
                    function *process() {
                        yield 10;
                        yield 11;
                        yield 12;
                    }
                    
                    let it = process();
                    it.next()
                    it.next()
                    it.next()
                    console.log(it.next());
                
            

Generator 2

💡 Co pojawi się w konsoli ?

                
                    {value: undefined, done: true}
                
            
                
                    function *process() {
                        let next = 10000;
                        while(true)
                            yield(next++);
                    }
                    
                    let it = process();
                    it.next()
                    console.log(it.next().value);
                
            

Generator 3

💡 Co pojawi się w konsoli ?

                
                    function *process() {
                        let next = 10000;
                        while(true)
                            yield(next++);
                    }
                    
                    let it = process();
                    it.next()
                    console.log(it.next().value);
                
            

Generator 3

💡 Co pojawi się w konsoli ?

                
                    10001
                
            
                
                    function *process() {
                        let next = 10000;
                        while(true)
                            yield(next++);
                    }
                    
                    for (let n of process()) {
                    
                        if (n > 10100) break;
                            console.log(n);
                    
                    }
                
            

Generator 5

💡 Co pojawi się w konsoli ?

                
                    function *process() {
                        let next = 10000;
                        while(true)
                            yield(next++);
                    }
                    
                    for (let n of process()) {
                    
                        if (n > 10100) break;
                            console.log(n);
                    
                    }
                
            

Generator 5

💡 Co pojawi się w konsoli ?

                
                    10001
                    10002
                    10003
                    ...
                    10100
                
            
                
                    function *process() {
                        yield;
                    }
                    
                    let it = process();
                    console.log(it.next());
                
            

Generator 6

💡 Co pojawi się w konsoli ?

                
                    function *process() {
                        yield;
                    }
                    
                    let it = process();
                    console.log(it.next());
                
            

Generator 6

💡 Co pojawi się w konsoli ?

                
                    {value: undefined, done: false}
                
            
                
                    function *process() {
                        let result = yield;
                        console.log(`result is ${result}`);
                    }
                    
                    let it = process();
                    it.next();
                    it.next(200);
                
            

Generator 8

💡 Co pojawi się w konsoli ?

                
                    function *process() {
                        let result = yield;
                        console.log(`result is ${result}`);
                    }
                    
                    let it = process();
                    it.next();
                    it.next(200);
                
            

Generator 8

💡 Co pojawi się w konsoli ?

                
                    result is 200
                
            
                
                    function *process() {
                        let result = yield;
                        console.log(`result is ${result}`);
                    }
                    
                    let it = process();
                    it.next();
                    console.log(it.next(200));
                
            

Generator 9

💡 Co pojawi się w konsoli ?

                
                    function *process() {
                        let result = yield;
                        console.log(`result is ${result}`);
                    }
                    
                    let it = process();
                    it.next();
                    console.log(it.next(200));
                
            

Generator 9

💡 Co pojawi się w konsoli ?

                
                    result is 200
                    {value: undefined, done: true}
                
            
                
                    function *process() {

                        let newArray = [yield,yield,yield];
                        console.log(newArray[2])
                    }
                    
                    let it = process();
                    it.next();
                    it.next(8);
                    it.next(16);
                    it.next(24);
                
            

Generator 10

💡 Co pojawi się w konsoli ?

                
                    function *process() {

                        let newArray = [yield,yield,yield];
                        console.log(newArray[2])
                    }
                    
                    let it = process();
                    it.next();
                    it.next(8);
                    it.next(16);
                    it.next(24);
                
            

Generator 10

💡 Co pojawi się w konsoli ?

                
                    24
                    {value: undefined, done: true}
                
            
                
                    function *process() {

                        let val =  2 * (yield 20);
                        console.log(val);
                    }
                    
                    let it = process();
                    it.next();
                    it.next(1);
                
            

Generator 11

💡 Co pojawi się w konsoli ?

                
                    function *process() {

                        let val =  2 * (yield 20);
                        console.log(val);
                    }
                    
                    let it = process();
                    it.next();
                    it.next(1);
                
            

Generator 11

💡 Co pojawi się w konsoli ?

                
                    2, {value: undefined, done: true}
                
            
                
                    function *process() {

                        yield 100;
                        yield [10,20,30]
                    }
                    
                    let it = process();
                    console.log(it.next().value);
                    console.log(it.next().value);
                    console.log(it.next().value);
                    console.log(it.next().value);
                
            

Generator 11

💡 Co pojawi się w konsoli ?

                
                    function *process() {

                        yield 100;
                        yield [10,20,30]
                    }
                    
                    let it = process();
                    console.log(it.next().value);
                    console.log(it.next().value);
                    console.log(it.next().value);
                    console.log(it.next().value);
                
            

Generator 11

💡 Co pojawi się w konsoli ?

                
                    100
                    [10, 20, 30]
                    undefined
                    undefined
                
            
                
                    function *process() {

                        yield 100;
                        yield* [10,20,30]
                    }
                    
                    let it = process();
                    console.log(it.next().value);
                    console.log(it.next().value);
                    console.log(it.next().value);
                    console.log(it.next().value);
                
            

Generator 12

💡 Co pojawi się w konsoli ?

                
                    function *process() {

                        yield 100;
                        yield* [10,20,30]
                    }
                    
                    let it = process();
                    console.log(it.next().value);
                    console.log(it.next().value);
                    console.log(it.next().value);
                    console.log(it.next().value);
                
            

Generator 12

💡 Co pojawi się w konsoli ?

                
                    100
                    10
                    20
                    30
                
            
                
                    function *process() {

                        yield 100;
                        yield* [10,20,30]
                    }
                    
                    let it = process();
                    console.log(it.next().value);
                    console.log(it.next().value);
                    console.log(it.next().value);
                    console.log(it.next().value);
                
            

Generator 13

💡 Co pojawi się w konsoli ?

                
                    100
                    10
                    20
                    30