javascript小测试

   测试地址http://toys.usvsth3m.com/javascript-under-pressure/

 在群里看到测试网站做着玩,希望你能过关,不能,且看下面答案(为了过关,不惜不够严谨): 

  第一题:    

function doubleInteger(i) {
    
    // i will be an integer. Double it and return it.  //i是一个整数,返回倍数。

  //可以用console.log(i)检查i是个什么数值。
    i*=2
    return i;  
}

   第二题:

function isNumberEven(i) {
    
    // i will be an integer. Return true if it's even, and false if it isn't. // i是一个整数,如果是偶数返回true,否则返回false;
  if(i%2 == 0){
        return true;
    }else{
        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, otherwise false

  //i是一个字符串,可能没有扩展名,如果有返回扩展名,没有返回false;
var index = i.indexOf(".");
    if(index != -1){
        return i.slice(index+1);
    }else{
        return false;
    }
}

  第四题:

function longestString(i) {
    
    // i will be an array.
    // return the longest string in the array

  //i是一个数组,返回数组中长度最长的项。
 var j=0,n = i.length,a=0,b;
    for(;j<n;j+=1){
        if(typeof i[j] == "object") continue; //由于试了几次出现二维数组,索性直接跳过。
        if(a<i[j].length){
            a=i[j].length;
            b=i[j]
        }
    }
    return b;
}

  第五题:

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.

  //i是数组,整数,字符串或者其他的,返回所有数字的和。
  var j=0,n = i.length,a=0;
    for(;j<n;j+=1){
        if(typeof i[j] == "object") {
            a+=arguments.callee(i[j]);
            continue;
        }
        if(typeof i[j] == "string") continue;
        a+=i[j];
    }
    return a;
}

以上为答案,你也试试吧,能过关,判断的不够严谨。 

原文地址:https://www.cnblogs.com/floatboy/p/interview_2.html