0

I want to write several variables into an array for one call to the "addLetter" method, "push" and "concat" does not work

function Team(name) {
    this.name = name;
    this.letters = [];
}
Team.prototype.addLetter = function (letter) {
    this.letters.push(letter).join('\n');
};
Team.prototype.toString = function () {
    return "Name of team - " + this.name +  '\n' +"ltters : " + this.letters;
};
var a = 's';
b='g';
v='d';
var team1 =  new Team('letters');
team1.addLetter(a,b,v);
console.log(team1.toString());

6
  • Well, you never defined addMember, so that's causing an error. Commented May 27, 2018 at 20:32
  • Where's addMember declaration? Commented May 27, 2018 at 20:32
  • sorr I did not change the name Commented May 27, 2018 at 20:37
  • Also your toString-function tries to log members instead of letters. Commented May 27, 2018 at 20:38
  • Array.prototype.push returns an integer rather than an array. Commented May 27, 2018 at 20:40

1 Answer 1

1

If you're using ES6:

Team.prototype.addLetter = function() {
    this.letters.push(...arguments);
};

With older syntax:

Team.prototype.addLetter = function() {
    this.letters.push.apply(this.letters.push, arguments);
};

JavaScript allows any number of arguments to be passed to any function. All of these arguments are accessible via the arguments variable within the function, which is an array-like object, containing all arguments in the order they were passed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.