我一直在代码学院研究Javascript,并对其中一个问题有疑问。

题:

编写两个函数:

one creates an object from arguments
the other modifies that object


我的答案:

//First, the object creator
function makeGamePlayer(name,totalScore,gamesPlayed) {
    //should return an object with three keys:
    // name
    // totalScore
    // gamesPlayed
    var myObject = {
     "name": name,
     "totalscore" : totalscore,
     "gamesPlayed" : gamesPlayed
    };
  return myObject;
}

//Now the object modifier
function addGameToPlayer(player,score) {
    //should increment gamesPlayed by one
    //and add score to totalScore
    //of the gamePlayer object passed in as player
    var score = player[totalscore];
    score =score+1;
    player[totalscore] = score;
}


不知道我的错误在哪里。需要一些改进此解决方案的指导。非常感谢...

最佳答案

在您的对象中,您永远不会分配分数

"totalscore" : totalscore,


应该

"totalscore" : totalScore


由于您传递的是totalScore

07-28 07:01