Role-Playing Game step 105

Step 105

Now use the += operator to add the string " In your inventory you have: " and the contents of inventory to the text.innerText. Make sure to include the space at the beginning and end of the " In your inventory you have: " string.

Im here, and here´s my code:

function sellWeapon() {
  if (inventory.length > 1) {
    gold += 15;
    goldText.innerText = gold;
let currentWeapon = inventory.shift();
    text.innerText = "You sold a " + currentWeapon + "." ;
    text.innerText = " In your inventory you have: " += inventory;
  }
}

I looked other forum posts and copied their code but it doesnt work, and it doesn´t say why (It doesnt show the message with the error, only the button to reset the lesson).
Thank you for the help :slightly_smiling_face:

1 Like

Hello,
a += b is like saying a = a + b, so basically the += operator is used to avoid repeating the same element on the right side of the assignment this means you can also do this
a += b + c which is a = a + b + c
In you case you are asked to add " In your inventory you have: " & inventory to text.innerText, you should replace the += before the inventory variable & add += right after text.innerText instead of =

1 Like

You are trying to assign inventory to a string. Strings are immutable, and you can not assign values to primitives.

You can combine two primitive values (using a math operator or concatenation) into a new value and save that new value.

let value = 1 + 2;

and not

let value = 1 += 2;

If you need to use the left-hand side value on the right-hand side, you do this:

let value = 40;
value = value + 2
// value > 42

As explained, instead of putting the value on the right-hand side, you can use the addition assignment += operator

let value = 40;
value += 2
// value > 42

And you can also combine the two:

let value = 40;
value += 1 + 1
// value > 42
1 Like

Thank you! I forgot that if you do a new text.innerText line it would replace the one before, so i didnt know where to put the +=

1 Like

You need + only before inventory array, and you missing + for your assignment operator = . Also place your last quotation mark after your concatenate operator.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.