Learn Basic JavaScript by Building a Role Playing Game - Step 171

Tell us what’s happening:

Hello, I am confused on what I am doing wrong in my code. I also checked other posts and noticed my if statement is completely different from other posts, could this be affecting it?

Your code so far

if (numbers.includes(guess)) {
text.innerText += " Right! You win 20 gold!";
gold += 20;
goldText.innerText = gold;

}

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

 if (numbers.includes(guess)) {
    text.innerText += " Right! You win 20 gold!";
    gold += 20;
    goldText.innerText = gold;
    

  }

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36

Challenge Information:

Learn Basic JavaScript by Building a Role Playing Game - Step 171

Hi @st3llz

Please remove the space after the quote mark.

Happy coding

Thanks so much! It worked, I thought it had something to do with my IF statement for some reason…

Do you have any insight why others code uses this for the IF statement

if (numbers.indexOf(guess) !== -1) {}

where as mine is :

if (numbers.includes(guess))

The condition is checking if the guess is not in the numbers array.

The .indexOf method is looking for guess in the numbers array. If it finds it, it returns the position of the first instance.

For example, say your guess is 2 and the numbers array contains it.
The method will return the index where the number is.

numbers = [1, 4, 5, 6, 2, 4, 7, 9];

Using zero-based indexing the index number will be 4
If guess is not in the array the index number will be -1

-1 means not in the array.

The strict inequality operator is comparing two numbers.

If the result is anything except -1 then the condition is true.

Inequality operators do the opposite of equality operators.

For the inequality operator, pretend you’re in the mirror verse. Whatever the comparison is in the mirror verse, then on the other side of the mirror the result will be the opposite.

Happy coding

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