Martin Carlin

You Can’t JavaScript Under Pressure

in javascript

You Can’t JavaScript Under Pressure is a coding game created by us vs th3m which was popular a couple of weeks ago but I thought I’d post my answers incase anyone was stuck and wanted to find the solution.

function doubleInteger(i) {

    // i will be an integer. Double it and return it.

    // answer
    return i * 2;
}

function isNumberEven(i) {

    // i will be an integer. Return true if it's even, and false if it isn't.

    // answer
    return  (i % 2) == 0

}

function getFileExtension(i) {

    // i will be a string, but it may not have a file extension.
    // return the file extension (with no period)
    // if it has one, otherwise false

    // answer (assuming filenames don't have dots in them)
    if (i.indexOf('.') != -1) {
        var extension = i.split('.').pop();
        return extension;
    } else {
        return false;
    } 
}

function longestString(i) {

    // i will be an array.
    // return the longest string in the array

    // answer
    var length = 0;
    var longest;

    for (var count = 0; count < i.length; count++) {
        if (i[count].length > length && typeof i[count] == 'string') {
            length = i[count].length;
            longest = i[count];
        }      
    } 
    return longest;
}

function arraySum(i) {

    // i will be an array, containing integers, strings
    // and/or arrays like itself.
    // Sum all the integers you find, anywhere in the nest of arrays.

    var total = 0;

    // answer
    for (var count = 0; count < i.length; count++) {
        if (typeof i[count] === 'number') {
            total += i[count];
        } else if (Array.isArray(i[count])) {
            total += arraySum(i[count]);
        }

    }
    return total;
}
comments powered by Disqus