Role playing step 172

Does anyone know what I am missing here please?

Instruction:

The indexOf() array method returns the first index at which a given element can be found in the array, or -1 if the element is not present.

After your for loop, add an if expression to check if the guess is in the numbers array. You can check if indexOf is not equal (!==) to -1.

Here is an example of the indexOf syntax:

const numbers = [1, 2, 3];
numbers.indexOf(2) // 1
  let numbers = [];
  while (numbers.length < 10) {
    numbers.push(Math.floor(Math.random() * 11));
  }
  text.innerText = "You picked " + guess + ". Here are the random numbers:\n";
  for (let i = 0; i < 10; i++) {
    text.innerText += numbers[i] + "\n";
  }
if (numbers.indexOf(guess) !== -1) {

}

Idk if is formatting in your post that you added the extra backticks (```), but your solution is correct and is passing from my side :thinking:

Can you please try again and be sure that your code is inside the correct function and with the correct formatting?

Please post a link to the Step. Thanks

Step 172

The indexOf() array method returns the first index at which a given element can be found in the array, or -1 if the element is not present.

After your for loop, add an if expression to check if the guess is in the numbers array. You can check if indexOf is not equal (!==) to -1.

Here is an example of the indexOf syntax:

const numbers = [1, 2, 3];
numbers.indexOf(2) // 1

That’s not a link, please post the link to the Step. You can copy the link by copying the URL from your browsers address bar.

  let numbers = [];
  while (numbers.length < 10) {
    numbers.push(Math.floor(Math.random() * 11));
  }
  text.innerText = "You picked " + guess + ". Here are the random numbers:\n";
  for (let i = 0; i < 10; i++) {
    text.innerText += numbers[i] + "\n";
  }
    if (numbers.indexOf(guess) !== -1) {
}
1 Like

I can’t because the help option is not showing up as available

You should be able to copy from your browsers address bar unless something is wrong with your browser

ah, ok:

1 Like

This is only part of the code in the editor. The if statement is correct but you many have accidentally made other changes. Id reset the step and only add the if statement that you have here.

ok, I will try that thank you.

If I select ‘reset’, it will definitely only reset the current step, right (and not the whole course)? I think I lost progress previously when I did this the wrong way, so just checking…

The reset button on a step only resets the step

thank you, it’s still not accepting it though. (Code copied below).

function pick(guess) {
  let numbers = [];
  while (numbers.length < 10) {
    numbers.push(Math.floor(Math.random() * 11));
  }
  text.innerText = "You picked " + guess + ". Here are the random numbers:\n";
  for (let i = 0; i < 10; i++) {
    text.innerText += numbers[i] + "\n";
  }
}
 if (numbers.indexOf(guess) !== -1) {
}

Your if statement is outside of the function. For the test to pass, if should be inside your function after the for loop.

3 Likes

thank you for your help

2 Likes

Mod Edit SOLUTION REMOVED

here is the solution

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

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