Devacron.com

Basic algorithm scripting solutions [Freecodecamp]

freecodecamp basic algorithm scripting solutions

FreeCodeCamp.com is one of the gems on the internet in my opinion. You can start your career as a developer absolutely free with the courses you can find there. The project is open source and this means you can contribute if you are an experienced developer. Today I finished the ‘Basic Algorithm Scripting’ section and even if I work as a developer for years it took me more than I thought. Well this is an alert that by using often tools like lodash.js / underscore.js will make my brain lazy 😛 Below are my solutions, you can write your thoughts in comments section.

Reverse a String

function reverseString(str) {
    return str.split('').reverse().join('');
}

Factorialize a Number

function factorialize(num) {
    if (num <= 1) {
        return 1;
    } else {
        return num * factorialize(num - 1);
    }
}

Check for Palindromes

function palindrome(str) {
    var c = str.toLowerCase();
    c = c.replace(/[^A-Za-z0-9]/g, '');
    var revc = c.split('').reverse().join('');
    return c == revc;
}

Find the Longest Word in a String

function findLongestWord(str) {
    var longest = '';
    str.split(' ').forEach(function(acc) {
        if (acc.length > longest.length) {
            longest = acc;
        }
    });
    return longest.length;
}

Title Case a Sentence

function titleCase(str) {
    return str.split(' ').map(function(acc) {
        return acc.toLowerCase().charAt(0).toUpperCase() + acc.toLowerCase().slice(1);
    }).join(' ');
}

Return Largest Numbers in Arrays

function largestOfFour(arr) {
    return arr.map(function(arrayN) {
        return arrayN.reduce(function(fNum, nNum) {
            return fNum < nNum ? nNum : fNum;
        });
    });
}

Confirm the Ending

function confirmEnding(str, target) {
    return str.substr(-target.length) === target;
}

Repeat a string

function repeatStringNumTimes(str, num) {
    for (var k = '', i = 0; i < num; i++) {
        k += str;
    }
    return k;
}

Truncate a string

function truncateString(str, num) {
    if (num > 3 && num < str.length) {
        return str.slice(0, num - 3) + '...';
    } else if (num >= str.length) {
        return str;
    } else {
        return str.slice(0, num) + '...';
    }
}

Chunky Monkey

function chunkArrayInGroups(arr, size) {
    var tempArr = [],
        i = 0;
    while (i < arr.length) {
        tempArr.push(arr.slice(i, i += size));
    }
    return tempArr;
}

Slasher Flick

function slasher(arr, howMany) {
    arr.splice(0, howMany);
    return arr;
}

Mutations

function mutation(arr) {
    var tempVal = true;
    for (var i = 0; i < arr[1].length; i++) {
        if (arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) == -1) {
            tempVal = false;
        }
    }
    return tempVal;
}

Falsy Bouncer

function bouncer(arr) {
    return arr.filter(function(arrElem) {
        return !!arrElem;
    });
}

Seek and Destroy

function destroyer(arr) {
    var eleArgs = [];

    for (var i = 1; i < arguments.length; i++) {
        eleArgs.push(arguments[i]);
    }

    return arr.filter(function(arrNum) {
        return eleArgs.indexOf(arrNum) === -1;
    });
}

Where do I belong

function getIndexToIns(arr, num) {
    arr.push(num);
    arr.sort(function compare(a, b) {
        return a - b;
    });
    return arr.indexOf(num);
}

Caesars Cipher

function rot13(str) {
    var encoded = '';
    for (var i = 0; i < str.length; i++) {
        if (str[i].charCodeAt(0) >= 65 && str[i].charCodeAt(0) < 78) {
            encoded += String.fromCharCode(str[i].charCodeAt(0) + 13);
        } else if (str[i].charCodeAt(0) >= 78 && str[i].charCodeAt(0) <= 90) {
            encoded += String.fromCharCode(str[i].charCodeAt(0) - 13);
        } else {
            encoded += str[i];
        }
    }
    return encoded;
}

Or you can get them from this repository

Exit mobile version