5个弱智 Javascript 问题限时挑战 You can't Javascript under pressure - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a Javascript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
Javascript 权威指南第 5 版
Closure: The Definitive Guide
est
V2EX    Javascript

5个弱智 Javascript 问题限时挑战 You can't Javascript under pressure

  •  
  •   est 2013-10-04 11:21:02 +08:00 6280 次点击
    这是一个创建于 4391 天前的主题,其中的信息可能已经有所发展或是发生改变。
    You can't Javascript under pressure

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


    注意页面打开自动播放声音


    出自 http://www.reddit.com/r/programming/comments/1nnokk/you_cant_Javascript_under_pressure/

    我的成绩 20 minutes, 29 seconds for all 5 levels. Well done!

    是不是渣渣级别了?
    35 条回复    1970-01-01 08:00:00 +08:00
    luikore
        1
    luikore  
       2013-10-04 11:43:00 +08:00
    9 minutes, 0 seconds

    最后一题判断 array type 用了点 hack...
    luikore
        2
    luikore  
       2013-10-04 11:46:15 +08:00
    如果是 coffee 就可以快很多, 后面的问题可以一个 list comprehension 写出来
    darcy
        3
    darcy  
       2013-10-04 11:48:34 +08:00
    17 minutes

    话说这才提醒我快1年没写过Javascript了。
    zhujinliang
        4
    zhujinliang  
       2013-10-04 12:01:50 +08:00
    6 minutes, 1 seconds for all 5 levels. Well done!
    主要时间浪费在读题以及解决实际跑的过程中遇到的意外,两三道题中题目本身未涉及类型判断问题,一开始我就没做判断,结果测试时给了意外的类型,还得修改代码
    angelface
        5
    angelface  
       2013-10-04 12:21:50 +08:00
    我很喜欢这个编辑器
    justfly
        6
    justfly  
       2013-10-04 13:18:26 +08:00
    8分56秒
    判断数组 可耻的用了 google
    dongsheng
        7
    dongsheng  
       2013-10-04 14:26:52 +08:00
    17,忘记Javascript的正则怎么写了 -_-
    windylcx
        8
    windylcx  
       2013-10-04 14:41:31 +08:00
    8 分多..打开控制台调试了多次.面壁去.
    windylcx
        9
    windylcx  
       2013-10-04 14:42:34 +08:00
    @dongsheng 用lastIndexOf 可以不用正则的
    est
        10
    est  
    OP
       2013-10-04 14:43:57 +08:00
    @windylcx 最后一个题可以用正则。哈哈
    mengzhuo
        11
    mengzhuo  
       2013-10-04 17:02:46 +08:00
    最后一题不是用递归?
    ```
    if (typeof(i[s]) =='object'){
    sum += arraySum(i[s]);
    }
    ```
    6 minutes, 43 seconds for all 5 levels. Well done!
    mengzhuo
        12
    mengzhuo  
       2013-10-04 17:04:07 +08:00
    体会是:
    果然Python写多了……
    JS功力不如当年了……
    思路也渐渐和Python靠拢了
    luin
        13
    luin  
       2013-10-04 17:41:52 +08:00
    3分多,扩展名那道题忘了 js 里咋用正则了,直接用了 indexOf,看 9 楼发现其实自己写错了,不过没被发现= =
    qiukun
        14
    qiukun  
       2013-10-04 17:54:49 +08:00
    typeof [1, 2, 4] === 'object'; // use Array.isArray or Object.prototype.toString.call to differentiate regular objects from arrays
    binux
        15
    binux  
       2013-10-04 18:35:12 +08:00
    7 minutes, 28 seconds
    大部分时间在读题。。判断是否是字符串很可耻地 if (e.split) 。。。
    zhujinliang
        16
    zhujinliang  
       2013-10-04 19:23:49 +08:00
    @luin @windylcx

    讨论一下扩展名那个题:

    return i.split('.').slice(1).pop()||'';

    用点分割以后,取出数组的最后一个就是扩展名,跟我们组一个php程序员学的。

    但是通常情况是存在“没有扩展名”的情况的,比如他这个题。
    简单的办法,判断数组成员数,如果是1则直接返回''。
    装b的办法,先把第一个去掉,如果没扩展名,此时数据就是空的了,pop返回undefined,再替换为''就是了。

    个人感觉没事想点“歪门邪道”还是挺有趣的
    pepsin
        17
    pepsin  
       2013-10-04 19:34:10 +08:00
    擦,好多函数名压根忘了,我居然还是个前端。。。
    sivacohan
        18
    sivacohan  
    PRO
       2013-10-04 21:05:46 +08:00
    我会说我最后一道题查了手册还搞了9分钟嘛……
    最后用了parseInt……
    efi
        19
    efi  
       2013-10-04 21:33:09 +08:00
    7分半。文件后缀的字符串操作完全不记得api是啥,是rindex还是rstrchr来着,暴力循环做。最后一题递归的时候循环变量忘记var结果查了一会。
    nixzhu
        20
    nixzhu  
       2013-10-04 21:36:58 +08:00
    第五题花比较久的时间,重做了一遍,顺便抄录下来。这好像就是写C的人会写出的风格,没有花哨的东西。最后一题取巧了一点:

    第一题:
    function doubleInteger(i) {
    // i will be an integer. Double it and return it.
    return i+=i;
    }
    第二题:
    function isNumberEven(i) {
    // i will be an integer. Return true if it's even, and false if it isn't.
    return i%2 == 0;
    }
    第三题:
    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
    var l = i.split('.');
    if (l && l.length > 1) {
    return l[l.length-1];
    } else {
    return false;
    }
    }
    第四题:
    function longestString(i) {
    // i will be an array.
    // return the longest string in the array
    var str ='';
    for (var x=0; x < i.length; x++) {
    if (typeof i[x] === 'string' && i[x].length > str.length) {
    str = i[x];
    }
    }
    return str;
    }
    第五题:
    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 x=0; x < i.length; x++) {
    if (!(typeof i[x] === 'string')) {
    if (i[x] instanceof Array) {
    sum += arraySum(i[x]);
    } else {
    sum += i[x];
    }
    }
    }
    return sum;
    }
    windylcx
        21
    windylcx  
       2013-10-04 21:56:25 +08:00
    @luin 具体哪里 求指导
    Hyperion
        22
    Hyperion  
       2013-10-04 21:56:50 +08:00
    8 minutes, 29 seconds. 我可耻的用了这种东西, 好羞耻. 没有语法提示, 实在是想不起来正则之外那几个搜索字符串的函数怎么拼了... 45s猥琐解决...

    test = i.match(/.*\.(\w+)/);
    return test?(test[1]?test[1]:false):false;

    最后一题, 除了递归还有啥解决办法吗?
    Hyperion
        23
    Hyperion  
       2013-10-04 21:59:39 +08:00
    @Hyperion 嗯.. 正则, 回复里漏打一个$, 请不要吐槽... 实际是写了的...
    mengzhuo
        24
    mengzhuo  
       2013-10-04 22:30:19 +08:00
    @zhujinliang

    s.split('.').reverse()[0]

    这样不行?
    breeswish
        25
    breeswish  
       2013-10-04 22:30:47 +08:00
    扩展名我直接lastIndexOf
    判断是否整数直接上了parseInt(x) === x
    ……


    @Hyperion 最后一题还可以这样写,是无递归的~

    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.

    sum = 0;

    arr = JSON.parse('[' + JSON.stringify(i).replace(/[\[|\]]/g, '') + ']');
    arr.forEach(function(v)
    {
    if (parseInt(v) === v)
    sum += v;
    });

    return sum;

    }
    breeswish
        26
    breeswish  
       2013-10-04 22:32:21 +08:00
    @Hyperion 不过我承认这样的代码,从native code来说还是有递归 ^_^
    zhujinliang
        27
    zhujinliang  
       2013-10-04 22:48:20 +08:00
    @mengzhuo .reverse()[0] 仅相当于 .pop(),无法正确处理类似 'aabbcc' 这样没有扩展名的情况
    tangzx
        28
    tangzx  
       2013-10-05 12:20:04 +08:00
    Eich, Douglas Crockford, Ada, 还有一个被黑的是谁?
    kavinyao
        29
    kavinyao  
       2013-10-05 17:24:00 +08:00
    14 minutes, 23 seconds for all 5 levels. Well done!
    kavinyao
        30
    kavinyao  
       2013-10-05 17:31:48 +08:00
    @nixzhu 第五题有两个问题:1. !(typeof i[x] === 'string') 不够健壮,object检测不出就不说了,浮点数呢? 2. 无法处理递归引用,例如var a =[1];a[1] = a; 你感受下……
    s
        31
    s  
       2013-10-05 18:31:25 +08:00 via Android
    我蛋疼地用手机撸了半个小时!
    晚上回家贴答案。
    otakustay
        32
    otakustay  
       2013-10-05 19:39:04 +08:00
    6分钟,最后一题玩reduce玩脱,好好写for循环估计4分钟能搞定
    s
        33
    s  
       2013-10-05 20:38:35 +08:00 via Android
    灰大,reduce可以的。
    s
        34
    s  
       2013-10-06 02:09:46 +08:00
    第一题
    function doubleInteger(i) {
    return i*2;
    }

    第二题
    function isNumberEven(i) {
    return !(i%2);
    }

    第三题
    function getFileExtension(i) {
    i = /\.(\w+)$/i.exec(i);
    return i && i[1] || false;
    }

    第四题
    function longestString(i) {
    return i.sort(function(a, b){
    if(typeof a!=='string') return 1;
    if(typeof b!=='string') return -1;
    return b.length-a.length;
    })[0];
    }

    第五题
    function arraySum(i) {
    return i.reduce(function(sum, next){
    next = next.map ? arraySum(next) :
    next.toFixed ? next : 0;
    return sum+next;
    }, 0);
    }
    lyric
        35
    lyric  
       2014-02-16 15:46:32 +08:00
    5 minutes, 46 seconds
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2258 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 16:04 PVG 00:04 LAX 09:04 JFK 12:04
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86