Friday, 15 November 2013

JavaScript Under Pressure

Just thought I'd share this little exercise that I got sent.

It's a fun little exercise that gives you five JavaScript exercises to do on a time limit.

http://toys.usvsth3m.com/javascript-under-pressure/

If you don't want to see the answers, then leave now, otherwise I'll give a summary of how I completed them!

One

return i*2;

Two

if(i%2)
{
    return false;
}
else
{
    return true;
}

Three

var fileExt = i.split('.');
return fileExt[1] != null ? fileExt[1] : false;

Four

var lengthOfString = 0;
var arrayVal = "";
   
for (var x = 0; x < i.length; x++){
  if(i[x].length > lengthOfString && typeof(i[x]) == "string")
        {
            lengthOfString = i[x].length;
            arrayVal = i[x];
        }
    }
   
return arrayVal;

Five

var sumOfArray = 0;
   
for(var x = 0; x < i.length; x++ {
    if(Array.isArray(i[x])) {
        sumOfArray += arraySum(i[x]);
    } else if (typeof(i[x]) == "number") {
        sumOfArray += i[x];
    }

}


return sumOfArray;