获得“意外的令牌错误”

时间:2016-03-10 07:24:42

标签: javascript object

我应该制作一个程序,预测March Madness游戏的获胜者,并且我不断收到“意外的令牌错误”,我无法找到语法错​​误的位置。我是新手,所以任何帮助表示赞赏谢谢! (另外如果你能告诉我如何更容易找到我的错误,而不会在将来寻求帮助,这对我有很大的帮助!)

/*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!");

};

3 个答案:

答案 0 :(得分:1)

为函数指定一些函数名称并将其命名为

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

答案 1 :(得分:1)

&#13;
&#13;
/*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);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

如果您不想为自己的功能命名,或者不想在其他地方使用它,可以使用immediately invoked function表达式:

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


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