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

2013-10-04 11:21:02 +08:00
 est
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!

是不是渣渣级别了?
5737 次点击
所在节点    JavaScript
35 条回复
windylcx
2013-10-04 21:56:25 +08:00
@luin 具体哪里 求指导
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
2013-10-04 21:59:39 +08:00
@Hyperion 嗯.. 正则, 回复里漏打一个$, 请不要吐槽... 实际是写了的...
mengzhuo
2013-10-04 22:30:19 +08:00
@zhujinliang

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

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

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/84511

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX