Learn Basic JavaScript by building a role playing game - Step 51

Tell us what’s happening:

Hey guys, this step says there’s a repetition in the code. While I understand and can execute the instructions to eliminate the alleged repetition, I can’t find the culprit in the code itself. Can you explain what the repetition is that I’m fixing? Is it a repeating function? If so, which one?

Thanks in advance! Instructions I’m referencing:

“You have repetition in the goTown and goStore functions. When you have repetition in your code, this is a sign that you need another function. Functions can take parameters, which are values that are given to the function each time it is run.”

My code so far

function goTown() {
  button1.innerText = "Go to store";
  button2.innerText = "Go to cave";
  button3.innerText = "Fight dragon";
  button1.onclick = goStore;
  button2.onclick = goCave;
  button3.onclick = fightDragon;
  text.innerText = "You are in the town square. You see a sign that says \"Store\".";

function goStore() {
  button1.innerText = "Buy 10 health (10 gold)";
  button2.innerText = "Buy weapon (30 gold)";
  button3.innerText = "Go to town square";
  button1.onclick = buyHealth;
  button2.onclick = buyWeapon;
  button3.onclick = goTown;
  text.innerText = "You enter the store.";

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge Information:

Learn basic JavaScript by building a role playing game - Step 51

The repetition is that the goTown and goStore functions basically do the exact same thing, just using different values. They both set the innerText on the three buttons, and then they both set the click handler on the three buttons, and then finally they set the innerText on text. I agree it may not seem obvious that this is the repetition the instructions are referring to. But whenever you have functions doing the same thing, just with different values, it’s a good bet you can create one generic function and then pass in the values.

You will spend the next many steps doing just this.


I see! It was a matter of simplifying the code. Thank you for the explanation. :blush:

1 Like

Where is the rest of your code?
as far as I know they ask us to Use const to create a variable called locations and assign it an empty array in this step