Practices and examples

Practice 1

"Fibonacci Sequence" redirects here. For the chamber ensemble, see Fibonacci Sequence (ensemble) A tiling with squares whose side lengths are successive Fibonacci numbers: 1, 1, 2, 3, 5, 8, 13, and 21.

for n > 1.

The beginning of the sequence is thus

function fibonacci(maxNumber) {
    const fib = [];
    let fibRes = [];

    fib[0] = 0;
    fib[1] = 1; 
    for (let i = 2; i <= maxNumber; i++) {
        fib[i] = fib[i - 2] + fib[i - 1];
        fibRes.push(fib[i]);
    }
    console.log(fibRes);
}

Practice 2

Create a function that will receive two arrays and will return an array with elements that are in the first array but not in the second.

function elementSelector(arr1, arr2) {
    let myArray = [];
    for (let index = 0; index < arr2.length; index++) {
        if (!arr1.includes(arr2[index])) {
            myArray.push(arr1[index]);
        }
    }
    console.log(myArray);
}

Practice 3

Create a function that will receive two arrays of numbers as arguments and return an array composed of all the numbers that are either in the first array or second array but not in both.

function mergeArrays(arr1, arr2) {
    let myArray = [];
    for (let index = 0; index < arr2.length; index++) {
        if (!arr1.includes(arr2[index])) {
            myArray.push(arr2[index]);
        }
    }

    for (let index = 0; index < arr1.length; index++) {
        if (!arr2.includes(arr1[index])) {
            myArray.push(arr1[index]);
        }
    }
    console.log(myArray);
}

Practice 4

Imagine you have an array of objects:

const myArray = [
{
    'id': 1,
    'name': 'John'
},
{
    'id': 2,
    'name': 'Bob'
}];

How to turn this array into an object where ids are keys? E.g.

const myObject =
{
    '1':
    {
        'id': 1,
        'name': 'John'
    },
    '2':
    {
        'id': 2,
        'name': 'Bob'
    }
}

Result

function fromArrayToObject(arr) {
    let myObject = {};
    myObject = Object.assign({}, arr);
    console.log(myObject);
}

and backwards:

function fromObjectToArray(obj) {
    let myArray = Object.values(obj);
    console.log(myArray);
}

Practice 5

Calculate the average of the numbers in an array of numbers

function calculateAverage(arr) {
    let total = 0;
    let n = arr.length;
    for (let i = 0; i < n; i++) {
        total += arr[i];
    }
    average = (total / n);
    return (Math.round(average));
}

Practice 6

Create a function that will receive n as argument and return an array of n random numbers from 1 to n.

// Create a function that will receive n as argument and return an array of n
// random numbers from 1 to n. The numbers should be unique inside the array.
function randomize(num) {

    let myArray = [];

    for (let i = 1; i <= num; i++) {
        if (!myArray.includes(i)) {
            myArray.push(i);
        }
    }
    return myArray;
}

Practice 7

Reverse a string

// Reverse a string
function reverseString(word) {
    let myWord = "";
    for(let i = word.length -1; i >= 0; i--) {
        myWord += word[i];
    }
    return myWord;
}

Practice 8

Reverse an array using build-in method and non-build in.

// Reverse an array
function reverseArray(arr) {
    // return arr.reverse(); // Build-in method
    let myArray = []; 
    for (let i = arr.length - 1; i >= 0; i--) {
        myArray.push(arr[i]);
    }
    return myArray;
}

Practice 9

Create a function that receives an array of numbers as argument and returns an array containing only the positive numbers

// Create a function that receives an array of numbers as argument and returns an
// array containing only the positive numbers
function getPositiveNumbers(numArr) {
    let myArray = [];
    // First way
    // for (let i = 0; i < numArr.length; i++) {
    //     if (numArr[i] > 0) {
    //         myArray.push(numArr[i]);
    //     }
    // }

    // Second way
    myArray = numArr.filter(x => x > 0);
    return myArray;
}

Last updated