In this excerpt from javascript koans, what is the Array(foo+1) all about? -


the koan below. in lessons, i've never seen word "array" stuck code that. examples elsewhere can study?

it("should know properties functions act methods", function () {   var meglomaniac = {     mastermind : "brain",     henchman: "pinky",     battlecry: function (noofbrains)     {       return "they " + this.henchman + " , the" +       array(noofbrains + 1).join(" " + this.mastermind);     } };  var battlecry = meglomaniac.battlecry(4); expect(fill_me_in).tomatch(battlecry); }); 

it should know properties functions act methods. damaging karma.

expected 'fill value in' match 'they pinky , brain brain brain brain'.

so, array(noofbrains + 1) creates new array of length 5 (well, given noofbrains passed in 4), in every element undefined:

[undefined, undefined, undefined, undefined, undefined] 

then, join operation takes string (" brain"), , places copy of between each element of array. (a more common use of join array.join(", ") comma-separates array)

so have:

undefined + " brain" + undefined + " brain" + undefined + " brain"+ undefined + " brain" + undefined 

which becomes " brain brain brain brain", since undefined's ignored join.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -