Skip to content

Instantly share code, notes, and snippets.

@abhishekdev
Last active December 24, 2015 21:39
Show Gist options
  • Select an option

  • Save abhishekdev/6866955 to your computer and use it in GitHub Desktop.

Select an option

Save abhishekdev/6866955 to your computer and use it in GitHub Desktop.

Revisions

  1. abhishekdev renamed this gist Oct 7, 2013. 1 changed file with 0 additions and 0 deletions.
  2. abhishekdev created this gist Oct 7, 2013.
    62 changes: 62 additions & 0 deletions js_under_pressure_solutions.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    /**
    * Solutions to questions at {@link http://toys.usvsth3m.com/javascript-under-pressure/ }
    */

    // Ans 1
    function doubleInteger(i){
    return i*2;
    }


    // Ans 2
    function isNumberEven(i){
    return i%2 == 0;
    }


    // Ans 3
    function getFileExtension(i){
    var rawExt = i.match(/\.+[a-zA-Z0-9]+/),
    ext = false;

    if(rawExt && rawExt.length){
    ext = rawExt[0].substring(1);
    }

    return ext;
    }


    // Ans 4
    function longestString(i){
    for(var x = i.length - 1; x>=0; --x){
    if(typeof i[x] != 'string'){
    i[x] = "";
    }
    }

    i.sort(function(a,b){
    return b.length - a.length;
    });

    return i[0];
    }


    // Ans 5
    function arraySum(i){
    var sum=0,
    x = i.length - 1,
    val;

    for(; x>=0; --x){
    val = i[x];

    switch(typeof val){
    case "number" : sum += val; break;
    case "object" : sum += arraySum(val); break;
    }
    }

    return sum;
    }