Hi everyone. My question is how compact/concise I can make a function. The working code as intended is
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;
}
}
my question is, could I write it as
text.innerText ="You sold a " + currentWeapon + "." + " In your inventory you have: " + inventory;
or would that be frowned upon or not in best practice? Ive tested it myself and it still works just fine as far as I can tell.
Thanks
no, they way you wrote it works too.
I think in the context of these first lessons, breaking it up like this is to help you practice working with the +=
operator.
In the next lesson, you are going to learn about an even better way to write this which is to use template literals.
Why is this better?
Because sometimes it is tricky to remember the correct spacing with concatenting strings like this
But with template literals you could actual rewrite that whole thing like so
text.innerText = `You sold a ${currentWeapon}. In your inventory you have: ${inventory}`;
You will learn about that in the next project and use template literals throughout the rest of the course.
Hope that helps
1 Like
I assumed it might be something covered later but figured Id ask. Thank you!
system
Closed
4
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.