Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
4.1 kB
9
Indexable
Never
/**
 * @description Print Visual Nuts FizzBuzz version
 * @param {Integer} maxNumber 
 * @param {Array} numberListParam 
 */
const visualNutsFizzBuzz = (maxNumber = 100, numberListParam = []) => {

    console.log('###################### EXERCISE 1 #######################');

    let numberList = [];

    // use received number list
    if (Array.isArray(numberListParam) && numberListParam.length > 0) {

        let elegibleNumbers;

        // make sure array ha no empty or invalid
        for (let i = 0; i < numberListParam.length; i++) {

            if (!numberListParam[i] || Number.isNaN(numberListParam[i])) {
                numberListParam[i] = i;
            } else if (Number.isSafeInteger(numberListParam[i])) {
                numberListParam[i] = parseInt(numberListParam[i]);
            }
        }

        // control max execution
        if (numberListParam.length > maxNumber) {
            // it will print just until maxNumber
            elegibleNumbers = numberListParam.splice(0, maxNumber);
        } else {
            elegibleNumbers = numberListParam;
        }

        numberList.unshift(...elegibleNumbers);

        // if the user send the first numbers but it is still small then complete the size
        if (numberList.length < maxNumber) {
            numberList.push(...[...new Array(maxNumber - numberList.length)].map((value, index) => value = index + 1))
        }

    } else {

        numberList = [...new Array(maxNumber)].map((value, index) => value = index + 1)
    }

    numberList.sort((a, b) => a - b);

    // console.log(numberList);
    numberList.forEach((number) => {
        let printMessage;

        if (number === 15) {
            printMessage = 'Visual Nuts';
        } else if (number % 3 === 0) {
            printMessage = 'Visual';
        } else if (number % 5 === 0) {
            printMessage = 'Nuts';
        }


        console.log(printMessage || number);
    })

}

const exercise2 = () => {

    console.log('###################### EXERCISE 2 #######################');

    const resourceList = [{
            country: "US",
            languages: ["en"]
        },
        {
            country: "BE",
            languages: ["nl", "fr", "de"]
        },
        {
            country: "NL",
            languages: ["nl", "fy"]
        },
        {
            country: "DE",
            languages: ["de"]
        },
        {
            country: "ES",
            languages: ["es"]
        }
    ];

    // avoid duplication
    const countryList = [...new Set(resourceList)];

    console.log('total of countries', countryList.length);

    const speaksDE = countryList.filter((item) => item.languages.includes('de'));

    console.log('speak German', speaksDE.map((item) => item.country));


    const languagesUtilization = [];

    resourceList.forEach((item) => {
        item.totalLanguages = item.languages.length;

        languagesUtilization.push(...item.languages);
    });

    console.log('Oficial Languages Total by country', resourceList.map((item) => {
        return {
            country: item.country,
            total: item.totalLanguages
        }
    }));

    console.log('Country with max official languages', function () {
        const max = resourceList.reduce((prev, current) => {
            return (prev.totalLanguages > current.totalLanguages) ? prev : current
        });

        return {
            countr: max.country,
            totalLanguages: max.totalLanguages
        }
    }());

    console.log('Language with max occurrence', function () {
        const max = languagesUtilization.sort((a, b) =>
            languagesUtilization.filter(v => v === a).length -
            languagesUtilization.filter(v => v === b).length
        ).pop();

        return {
            Language: max.toUpperCase()
        }
    }());
}


// possibilities
visualNutsFizzBuzz();
visualNutsFizzBuzz(10, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
visualNutsFizzBuzz(20, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
visualNutsFizzBuzz(20, [2, 2, 2, 2]);

exercise2();