Reputation: 35
I am trying to make an object that includes the teams points (as shown below where it says "this.update"). When I run the program it does not seem to give the points to the teams and it does not seem to even evaluate both teams' goals.
I want the team1Points and team2Points properties to be derived from an IF statement like the one above or another solution would help, akin to the 1 point to both teams for a draw, 3 points for a win, 0 for a loss.
teamsArray = ["Blue Team", "Red Team"];
function match(team1Name, team2Name, team1Goals, team2Goals) {
this.team1Name = team1Name;
this.team1Goals = team1Goals;
this.team2Name = team2Name;
this.team2Goals = team2Goals;
this.update = function() {
if (team1Goals > team2Goals) {
team1Points = 3;
team2Points = 0;
} else if (team2Goals > team1Goals) {
team2Points = 3;
team1Points = 0;
} else if (team1Goals == team2Goals) {
team1Points = 1;
team2Points = 1;
}
};
this.update();
}
testMatch();
function testMatch() {
var match1 = new match(teamsArray[0], teamsArray[1], 2, 0);
console.log(match1);
}
Upvotes: 3
Views: 69
Reputation: 6768
Your method creates global variables instead of properties, and you never even called it!
In order to avoid issues like this, I suggest switching to the modern JavaScript syntax. Begin your scripts with the instruction 'use strict';
to enable strict mode and define variables using let
instead of var
. If you do that, the browser won't let you define global variables from within a function.
As for the solution to your code:
function match(team1Name, team2Name, team1Goals, team2Goals) {
this.team1Name = team1Name;
this.team1Goals = team1Goals;
this.team2Name = team2Name;
this.team2Goals = team2Goals;
this.team1Points = this.team2Points = 0;
this.update = function() {
if (this.team1Goals > this.team2Goals) {
this.team1Points = 3;
this.team2Points = 0;
} else if (this.team2Goals > this.team1Goals) {
this.team2Points = 3;
this.team1Points = 0;
} else if (this.team1Goals == this.team2Goals) {
this.team1Points = 1;
this.team2Points = 1;
}
};
}
And don't forget to call .update()
somewhere.
m = new match("Alpha", "Beta", 0, 2);
m.update();
console.log("Team " + m.team1Name + " has " + m.team1Points + " points.");
console.log("Team " + m.team2Name + " has " + m.team2Points + " points.");
Upvotes: 1
Reputation: 21
teamsArray = ["Blue Team", "Red Team"];
function match(team1Name, team2Name, team1Goals, team2Goals) {
this.team1Name = team1Name;
this.team1Goals = team1Goals;
this.team2Name = team2Name;
this.team2Goals = team2Goals;
this.update = function() {
if (team1Goals > team2Goals) {
team1Points = 3;
team2Points = 0;
return "team1 win"
} else if (team2Goals > team1Goals) {
team2Points = 3;
team1Points = 0;
return "team2 win"
} else if (team1Goals == team2Goals) {
team1Points = 1;
team2Points = 1;
return "draw"
}
};
}
testMatch();
function testMatch() {
var match1 = new match(teamsArray[0], teamsArray[1], 2, 0);
console.log(match1.update());
}
Upvotes: 1
Reputation: 276
teamsArray = ["Blue Team", "Red Team"];
function match(team1Name, team2Name, team1Goals, team2Goals) {
this.team1Name = team1Name;
this.team1Goals = team1Goals;
this.team2Name = team2Name;
this.team2Goals = team2Goals;
this.update = function() {
if (team1Goals > team2Goals) {
this.team1Points = 3;
this.team2Points = 0;
} else if (team2Goals > team1Goals) {
this.team2Points = 3;
this.team1Points = 0;
} else if (team1Goals == team2Goals) {
this.team1Points = 1;
this.team2Points = 1;
}
};
this.update();
}
testMatch();
function testMatch() {
var match1 = new match(teamsArray[0], teamsArray[1], 2, 0);
console.log(match1);
}
Upvotes: 1