-1

I have been working on Javascript in codecademy and have a doubt on one of the questions.

Question:

Write two functions:

one creates an object from arguments
the other modifies that object

My answer:

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

Not sure where my error is. Need some guidance on improving this solution.. Many Thanks...

4
  • 1
    what's the question? if you don't know what the error is, we are supposed to guess it? Commented Aug 10, 2012 at 15:01
  • 2
    You say you have to add score to player.totalscore, but you're assigning score + 1 to player.totalscore instead. Also, be wary of brackets notation, as it requires strings: player["totalscore"], not player[totalscore]. Commented Aug 10, 2012 at 15:02
  • 2
    You should read the MDN JavaScript Guide alongside with doing the exercises. Especially have a look at Working with Objects. Commented Aug 10, 2012 at 15:10
  • Misspelt totalScore parameter. That is all. Commented Dec 20, 2024 at 12:07

4 Answers 4

3

in your object you are never assigning the score

"totalscore" : totalscore,

should be

"totalscore" : totalScore

since you are passing in totalScore

Sign up to request clarification or add additional context in comments.

Comments

2

You are not accessing the object correctly, either

var score = player.totalscore;

or

var score = player["totalscore"];

It expects a string, but you are passing an undefined variable.

You are also defining score twice within the function, use a different name for the internal variable.

Comments

1

The parameter to makeGamePlayer is named totalScore but you are using totalscore in myObject which is a different name - case matters.

You also have an issue in addGameToPlayer trying to use a variable named totalscore but that is not defined

Comments

0

Apart from the typo's and your code being rather silly and pointless IMO (sorry, but google Douglas Crockford JavaScript Object or something and read what a powerconstructor is), I gather you want to check if all parameters are passed to the function. If so:

function foo (bar, foobar)
{
    if (arguments.length < 2)
    {
        throw new Error('foo expects 2 arguments, only '+arguments.length+' were specified');
    }
    //or - default values:
    bar = bar || 'defaultBar';
    //check the type?
    if (typeof bar !== 'string' || typeof foobar !== 'number')
    {
        throw new Error ('types don\'t match expected types');
    }
}

And so on... But please, read and be more specific when asking a question

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.