0

I'm trying to define teams with 4 different attributes -- speed, header, power, and accuracy. Then, I want to find the average of theses 4 attributes, but for some reason when I try to do so, or perform any mathematical operation between attributes, the program returns undefined. Any ideas?

team1 = new team ("Manchester United");
team2 = new team ("Arsenal");
team3 = new team ("Chelsea");
team4 = new team ("New York Rangers");

function soccerGame (team1, team2, team3, team4)  {

var team = function(teamName) {
    this.teamName = teamName;
    this.speed = Math.floor(Math.random() * 10) + 1 ; 
    this.header = Math.floor(Math.random() * 10) + 1 ;
    this.power = Math.floor(Math.random() * 10) + 1 ;
    this.accuracy = Math.floor(Math.random() * 10) + 1 ;
    console.log(this.accuracy + this.power + this.header + this.speed) / 4;
};
}
0

2 Answers 2

1

Your only issue is that you have your constructor wrapped in soccerGame, making it a local variable. Local variables can only be accessed from within the scope of the function. You have two options: put your team declarations within the soccerGame function, or remove the function and implement what you were planning to do with that somewhere else. You can't pass in teams to it if you define your teams within the function, so I would recommend the second option.

Demo

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

Comments

0
var team = function(teamName) {
    this.teamName = teamName;
    this.speed = Math.floor(Math.random() * 10) + 1 ; 
    this.header = Math.floor(Math.random() * 10) + 1 ;
    this.power = Math.floor(Math.random() * 10) + 1 ;
    this.accuracy = Math.floor(Math.random() * 10) + 1 ;
    console.log(this.accuracy + this.power + this.header + this.speed) / 4;
};


team1 = new team ("Manchester United");
team2 = new team ("Arsenal");
team3 = new team ("Chelsea");
team4 = new team ("New York Rangers");

Your team function and arragement of code does matter.

Working Demo

3 Comments

Order only matters because a function expression is being used. If the OP used a function declaration, it wouldn't matter (but would still be better if the function declaration was first).
I mean the var team = function(team) should place outside the soccerGame function. That is my meaning of arragement of code.
Ok. You may have guessed I'm not a fan of using function expressions where function declarations can be used. ;-)

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.