1
Name:
Anonymous
2012-02-29 2:35
function map(proc, lst) {
var res = [];
for (var obj in lst)
res.push(proc(obj));
return res;
}
function filter(pred, lst) {
var res = [];
for (var obj in lst)
if (pred(obj))
res.push(obj);
return res;
}
ls = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
ls = map(function(x) {
return x * x;
}, ls);
print(ls);
ls = filter(function (x) {
return x % 2 == 0;
}, ls);
print(ls);
What I expect:
0,1,4,9,16,25,36,49,64,81
0,4,16,36,64
What I get:
0,1,4,9,16,25,36,49,64,81
1,3,5,7,9
WHY?
2
Name:
Anonymous
2012-02-29 2:55
Never mind, I figured it out. for (var x in y) considered harmful, unscientific and ultimately destructive.
3
Name:
Anonymous
2012-02-29 3:31
so obj was just the index of the slot in the array or what?
4
Name:
Anonymous
2012-02-29 4:14
>>3
Yeah. Furthermore, if you were to use it on a DOM structure, I would give you an index for each leaf in the tree. It's almost never safe to use.
5
Name:
Anonymous
2012-02-29 5:23
>>4
javascript has so many strange caveats. I guess it's ok once you have enough knowledge to avoid them, but for a newcomer you can get hung up on this type of stuff for a while. Javascript got me so hard with the forced stringification of keys. I can't remember what the exact usage was though.
6
Name:
Anonymous
2012-02-29 5:37
>>5
Watch Crockford on Javascript, should only take 8 hours or so. If you don't have time just watch the "Function the Ultimate" one.
7
Name:
Anonymous
2012-02-29 12:01
you should never expect javascript to do what you want. it was designed for pop-up ads and nothing else
9
Name:
Anonymous
2012-02-29 12:50
want to fill in the form on
http://www.unblockyoutube.us/
normally i'd do document.FORMNAME.FIELDNAME.value='TEXT GOES HERE';
But the form on that page has no name or fields (I tried "url_form and "a" respectively without success).
Now, I also tried document.getElementsByTagName('form')[0].value='TEXT';
But that didnt work either.
Any pointers?
document.getElementsByClassName("form")[0].value = "pepe"
gives you a blank page with text
10
Name:
Anonymous
2012-02-29 16:29
JavaScript is broken by design.
11
Name:
Anonymous
2012-02-29 17:12
It's time to code apps and check dubs, and I'm not all out of dubs.
12
Name:
Anonymous
2012-02-29 17:20
>>9
document.getElementsByTagName('input')
13
Name:
Anonymous
2012-02-29 21:24
for (var obj in lst)
if (pred(lst[obj]))
res.push(lst[obj]);
FTFY. JS (maddeningly) assigns the index in for loops, not the value. (You'll need to fix map too, 1 * 1 is not 0.)
Also, WTF are you not using Array.map and Array.filter?