日期:2014-05-16  浏览次数:20323 次

今晚发现一个比较有趣的js过关游戏,分享下

本人js是今年下半年开始真正入手啊,感觉这js世界丰富程度真的超出我想象

晚上偶然间看到别人在群里问做什么题目的,跟踪后发现是个js过关游戏,而且全是英文,兴趣就来了偷笑

网站地址为:http://toys.usvsth3m.com/javascript-under-pressure/

javascript-under-pressure

总共有五关,都是些比较基础的题目,对本人水平测试玩玩够了~

全部答题完成花了15分钟 难过,把每关的代码贴下,有兴趣的可以看看,有觉得写得不好的可以攻击哈~求赐教!

对大牛来说很基础,有兴趣的童鞋们可以玩下哈


第一关

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

第二关

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

 第三关

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, otherwisefalse
   var j = i.lastIndexOf('.');
   if(j===-1)
       return false;
   return i.substring(j+1);
}

第四关

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

第五关

function arraySum(i) {
   // i will be an array, containing integers, strings and/or arrays likeitself.
   // Sum all the integers you find, anywhere in the nest of arrays.
   var result = 0;
   for(var j = 0;j<i.length;j++)
    {
       if(typeof(i[j]) == "number")
           result +=i[j];
       else if(Object.prototype.toString.call(i[j]) === '[object Array]')
           result +=arraySum(i[j]);
    }
   return result;
}