Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context of Array() in this function

  it("should know properties that are functions act like methods", function() {
    var meglomaniac = { 
      mastermind : "Brain", 
      henchman: "Pinky",
      battleCry: function(noOfBrains) {
        return "They are " + this.henchman + " and the" +
          Array(noOfBrains + 1).join(" " + this.mastermind);
      }
    };

    var battleCry = meglomaniac.battleCry(4);
    expect('They are Pinky and the Brain Brain Brain Brain').toMatch(battleCry);
  });

What is the definition of Array in this code (line 7)? I looked it up and it looks like it's an Array.of() command which generates an empty array of length n which in this case would be 5? So why does it end up with only 4 brain inputs assuming that is the correct assumption? Or is this array() doing something else?

like image 894
NewAtLearningThis Avatar asked May 16 '26 18:05

NewAtLearningThis


1 Answers

battleCry(4) means that Array(noOfBrains + 1) will indeed have a length of 5:

[empty, empty empty, empty, empty]

But when you .join those 5 elements, you only insert something into the spaces between them, and there are only 4 spaces:

[empty, empty empty, empty, empty]
//     ^     ^      ^      ^

So, you end up with 4 occurrences of this.mastermind in the resulting string.

This code is quite confusing. I'd strongly prefer something like .repeat instead:

var meglomaniac = { 
  mastermind : " Brain", 
  henchman: "Pinky",
  battleCry: function(noOfBrains) {
    return `They are ${this.henchman} and the${this.mastermind.repeat(noOfBrains)}`
  }
};

var battleCry = meglomaniac.battleCry(4);
console.log(battleCry === 'They are Pinky and the Brain Brain Brain Brain');

Array is just the array constructor. It's not really anything special, it just creates an array which has a length of the parameter (when passed a number).

like image 83
CertainPerformance Avatar answered May 18 '26 08:05

CertainPerformance



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!