DavidGilly
DavidGilly

Reputation: 47

Getting an "Unexpected Token Error"

I am supposed to make a program that predicts the winners of March Madness games and I keep getting an "Unexpected token error" and I cannot find where the error in the syntax is. I'm a novice so any help is appreciated thanks! (Also if you could tell me how to more easily find my errors without asking for help in the future that would help me greatly!)

/*Asks users for the Offensive and defensive efficiencies of each team,
and asks for their seed in the tournament*/

var team1 = {
offEff: prompt("Offensive Efficency for Team 1", "ex 1.28"),
defEff: prompt("Defensive Efficency for Team 1", "ex .72"),
seed: prompt("Seed for Team 1", "ex 2")
};
var team2 = {
offEff: prompt("Offensive Efficency for Team 2", "ex 1.28"),
defEff: prompt("Defensive Efficency for Team 2", "ex .72"),
seed: prompt("Seed for Team 2", "ex 2")
};

/*This function adds point values to each team based on comparisons
in each category, and whoever's point value is highest is printed
to the console.*/

function (team1, team2)
{
var team1p = 0;
var team2p = 0;

if (team1.seed < team2.seed)
    team1p+=3;
else
    team2p+=3;

if(team1.offEff > team2.offEff)
    team1p+=1.5;
else
    team2p+=1.5;

if(team1.defEff < team2.defEff)
    team1p+=1.5;
else
    team2p+=1.5;

if (team1p >= team2p) 
    console.log("Team 1 will win!");
else 
    console.log("Team 2 will win!");

};

Upvotes: 0

Views: 43

Answers (3)

Adam
Adam

Reputation: 5233

If you don't want to name your function, or don't want to use it elsewhere, you can use the immediately invoked function expression:

var team1 = {
    //..
};
var team2 = {
    //..
};


(function (team1, team2) {
    //
    // ...
    //
}(team1, team2));

Upvotes: 0

Thanga
Thanga

Reputation: 8091

/*Asks users for the Offensive and defensive efficiencies of each team,
and asks for their seed in the tournament*/

var team1 = {
offEff: prompt("Offensive Efficency for Team 1", "ex 1.28"),
defEff: prompt("Defensive Efficency for Team 1", "ex .72"),
seed: prompt("Seed for Team 1", "ex 2")
};
var team2 = {
offEff: prompt("Offensive Efficency for Team 2", "ex 1.28"),
defEff: prompt("Defensive Efficency for Team 2", "ex .72"),
seed: prompt("Seed for Team 2", "ex 2")
};

/*This function adds point values to each team based on comparisons
in each category, and whoever's point value is highest is printed
to the console.*/

function WhichTeamWon(team1, team2)
{
var team1p = 0;
var team2p = 0;

if (team1.seed < team2.seed)
    team1p+=3;
else
    team2p+=3;

if(team1.offEff > team2.offEff)
    team1p+=1.5;
else
    team2p+=1.5;

if(team1.defEff < team2.defEff)
    team1p+=1.5;
else
    team2p+=1.5;

if (team1p >= team2p) 
    console.log("Team 1 will win!");
else 
    console.log("Team 2 will win!");

};

WhichTeamWon(team1, team2);

Upvotes: 1

Pavan Teja
Pavan Teja

Reputation: 3202

give some function name to the function and call it

 var team1data = {
    offEff: prompt("Offensive Efficency for Team 1", "ex 1.28"),
    defEff: prompt("Defensive Efficency for Team 1", "ex .72"),
    seed: prompt("Seed for Team 1", "ex 2")
    };
    var team2data = {
    offEff: prompt("Offensive Efficency for Team 2", "ex 1.28"),
    defEff: prompt("Defensive Efficency for Team 2", "ex .72"),
    seed: prompt("Seed for Team 2", "ex 2")
    };



    function funcname(team1, team2)
    {
    var team1p = 0;
    var team2p = 0;

    if (team1.seed < team2.seed)
        team1p+=3;
    else
        team2p+=3;

    if(team1.offEff > team2.offEff)
        team1p+=1.5;
    else
        team2p+=1.5;

    if(team1.defEff < team2.defEff)
        team1p+=1.5;
    else
        team2p+=1.5;

    if (team1p >= team2p) 
        console.log("Team 1 will win!");
    else 
        console.log("Team 2 will win!");

    };
    funcname(team1data ,team2data );

Upvotes: 1

Related Questions