Looping & Recursion

Video

JavaScript Notes

JavaScript
    // Looping vs Recursion
    let fetch = require('node-fetch');
    let url = "http://jsonplaceholder.typicode.com/comments?postId=42"; 
    let log = console.log;
    let count = 0; 
    const SIZE = 120;
    
    // Looping
    log('Starting the loop');
    for (let i =0; i < 10; i++){
        log('inside loop', i);
    }
    log ('Ended the loop');
    
    // Recursion
    let recky = function( ) {
        log('inside recky', count);
        count++; 
        if(count<10){
            recky( );
        }
    }
    log('Starting the recursion');
     
    recky( );
    log('Ending the recursion')
    
    // Recursion for countdowns 
    let countdown = function(size) {
        log('x'.repeat(size));
        size = Math.floor( size * 0.95); 
        if (size > 2){
            countdown(size);
        }
    }
    countdown(SIZE);
    
    // Asynchronous methods
    // AJAX, Databases Promises, Timers…
    
    log('about to fetch');
    for(let c=0; c<4; c++){
        let d = fetch(url)	// fetch is an asynchronous function – runs on a 2nd thread
        .then(response=>json.response( ))
        .then(data=>{ log('data', JSON.stringify(data)) })
        log(c, d);	// d is a promise object/variable, since fetch takes time to fully execute
    }
    log('after fetch??');