3

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);
}

1
  • 1
    Try this.team1Points = 0 etc. Commented Nov 16, 2017 at 0:57

3 Answers 3

1

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.");
Sign up to request clarification or add additional context in comments.

7 Comments

Wow, sorry if you noticed that horrible sequence of edits. I'm a bit rusty on SO usage.
I forgot to add in the team1Points and team2Points variables but it doesnt really answer my question. I tried to do console.log(testMatch.update()) and it doesnt really solve the question. I am trying to get team1Points to = 3 as they have scored more goals. Maybe I am calling the update() function wrong im unsure
The update function doesn't return anything, so logging its result won't do. You'll want to log the variables themselves if you want to check their values.
@Jay I edited the answer with an example of how you'd create a match and log the results.
Ah, gotcha. I forgot to add this. in the conditions as well.
|
1

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);
}

Comments

1
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());
}

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.