Skip to content

Instantly share code, notes, and snippets.

@makenova
Created April 27, 2014 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makenova/11338953 to your computer and use it in GitHub Desktop.
Save makenova/11338953 to your computer and use it in GitHub Desktop.
javascript under pressure solutions

These are my solutis that I came up with while doing the JS Challenge at usvsth3m. They were reached 'under pressure' as the title implies, hence there are no optimizations and it looks rough.
The problem description is stated before the solution.

Code as fast as you can! You need to double the integer and return it. To test your code, click Go or hit Ctrl-Enter/⌘-Enter.

function doubleInteger(i) {
    // i will be an integer. Double it and return it.
    return i*2;
}

Little bit trickier now. The clock's started ticking again. Return true or false depending on whether the number is even. You can use console.log() and alert(); the results will show up here.

function isNumberEven(i) {
    // i will be an integer. Return true if it's even, and false if it isn't.
    return i % 2 === 0;    
}

Here we go! Given a filename in a string (like 'test.jpg'), return the file extension (like 'jpg'), OR false if it doesn't have one.

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
    if ( i.split('.').length > 1 ){
        return i.split('.')[1];
    } else {
        return false;
    }
}

Level 4 of 5! You'll get an array. Return the longest string inside it.

function longestString(i) {
    // i will be an array.
    // return the longest string in the array
    var result = '';
    for (var itt=0; itt<i.length; itt++){
        if(i[itt].length > result.length && typeof i[itt]==='string'){
            result = i[itt];
        }
    }
    return result;
}

Final challenge! Sum all the integers in a nested array, no matter how many levels deep.

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 sum = 0;
    for(var itt=0; itt<i.length; itt++){
        if (Array.isArray(i[itt])){
            sum += arraySum(i[itt]);
        } else if (Number.isInteger(i[itt])){
            sum += i[itt];
        }
    }
    return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment