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!

是不是渣渣级别了?
5722 次点击
所在节点    JavaScript
35 条回复
luikore
2013-10-04 11:43:00 +08:00
9 minutes, 0 seconds

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

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

讨论一下扩展名那个题:

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

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

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

个人感觉没事想点“歪门邪道”还是挺有趣的
pepsin
2013-10-04 19:34:10 +08:00
擦,好多函数名压根忘了,我居然还是个前端。。。
sivacohan
2013-10-04 21:05:46 +08:00
我会说我最后一道题查了手册还搞了9分钟嘛……
最后用了parseInt……
efi
2013-10-04 21:33:09 +08:00
7分半。文件后缀的字符串操作完全不记得api是啥,是rindex还是rstrchr来着,暴力循环做。最后一题递归的时候循环变量忘记var结果查了一会。
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;
}

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

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

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

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

© 2021 V2EX