קורס JavaScript ES6/7/8 שיעור תרגול פירוק והרכבה


זהו נושא דיון מלווה לערך המקורי שב־https://www.tocode.co.il/bundles/es6/lessons/destructuring-lab

שלום,
מצרפת את הפתרונות שלי - אשמח להערות:
תרגילים 1 ו - 2:

  function printTimes(options) {
        // TODO - fill code (1 line) here
        const {text , times=5} = options;
        for (let i=0; i < times; i++) {
            console.log(`${String(i + 1).padStart(2, '0')} ${text}`);
        }
    }
    // print 'hello world' ten times:
    printTimes({ text: 'hello world', times: 10 });
    // print 'hello world' five times:
    printTimes({ text: 'hello world'});

תרגיל 3:

function printTimesFromWeirdDevelopersDownstairs(weirdOptions) {
        // TODO fill code (1 line) here
        const {text = weirdOptions.title , times} = weirdOptions;
        printTimes({ text, times });
    }

תרגיל 4:

    function fib(n) {
        if ((n == 0) || (n == 1)) {
            return 1;
        }
        return fib(n - 1) + fib(n - 2);
    }

    function timed(fn) {
        return (...arg)=>{
            const s0 = Date.now();
            fn(...arg);
            const s1 = Date.now();
            return `Took ${s1 - s0}ms`;
        }
    }

    const timedFib = timed(fib);
    console.log(timedFib(40));

תרגיל 5: arr1 לא משנה את ההתייחסות ו-arr2 מתייחס למערך חדש.

    let arr1 = copyArr1 = [10, 20, 30, 40];
    let arr2 = copyArr2 = [10, 20, 30, 40];

    arr1.push(50);
    arr2 = [...arr2, 50];
    
    console.log(arr1);
    console.log(arr2);

    console.log(arr1===copyArr1);
    console.log(arr2===copyArr2);
לייק 1

נראה מעולה לגבי תרגיל 4 נשמע לי יותר הגיוני שהפונקציה הפנימית תחזיר את התוצאה של fn במקום את הטקסט של כמה זמן זה לקח

לייק 1