Role-Playing Game- Step 105

Question: " 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."

My code:

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

  }
}

Ive tried the += operator both where I posted above and after the last string before I call to my inventory and neither really worked :confused: any insight is super appreciated!

1 Like

Hello.
Form another innerText line and use the addition assignment operator to assign the given string and concatenate the inventory variable.

1 Like

Hmmm so I tried that and still nothing :confused: This is what I put in:

 let currentWeapon = inventory.shift();
    text.innerText = "You sold a " + currentWeapon + "." ;
    text.innerText = " In your inventory you have: " += inventory;

I tried placing the += both at the beginning of the second innerText line (removing the ; from the first line) and then also where you see it above… am I missing something?

Hello!

You are almost there: The addition assignment is at the wrong place.

You want to add-assign " In your inventory you have : " + inventory to “text.innerText”. Not “inventory” to the string :wink:.

1 Like

If Im not mistaken Ive already done that, this is what Im thinking you mean

text.innerText = "You sold a " + currentWeapon + "." + " In your inventory you have: " += inventory;

If you could maybe explain this in another way Id super appreciate!

I suggest we correct this code, that you had earlier posted.

Now make the following changes:

  1. The addition assignment operator should come after this text.innerText. You are assigning the string and the inventory variable to the text, so it should come first.

  2. Then, in the place where you used an additional assignment operator, use a concatenation operator instead, which is simply the plus sign. This will add the value of your inventory variable to your string.

  3. Make sure not to change the string in any way, do not delete or add anything, including spaces.

Im so sorry but Im still super lost hahah… so the addition operator should come after the FIRST innerText or the second? The way Im taking this is as follows:

let currentWeapon = inventory.shift();
    text.innerText = "You sold a " + currentWeapon + "."  + " In your inventory you have: " + inventory;

Which Ive already tried so there must be something Im not picking up on with your explanation. Is the second innerText needed still? Since it sounds to me as everything should be placed on the first innerText.

Thanks!

you need two lines, one assigning to innerText and the second using the compound assignment

This should be applied to the second one. You have simply interchanged the operators. To make it easier, interchange the operators. Remember we are using the code below.

AH-HA!!! Finally! Hahahahaha thank you so much for all the patience :slight_smile: You guys are awesome!

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