How to grab array item string value?

Im making a game where you guess a random fruit. I have the fruits in an array but it seems that the user can only enter the array number value and not the string associated with it. How can I fix this so that the guess can equal the string value and not the array numeric position value?

also, my final alert is not working for some reason. Im working on fixing that, but im not sure why its not triggering.

Please help :slightly_smiling_face:

Why not set the randomFruit to the entry in the array rather than the index of the entry in the array?

let randomFruit= randomNum ;
1 Like

Yes that what I would like to do but im unsure how to go about it.

Because from my perspective, Im not intentionally asking for the index im asking for the entry.

Well, how do you get an entry out of an array?

1 Like

I see that I can grab the entry of an array using the square brackets . So, if I want the first value of an array, I can use myArray[0].

but in my case, how would I randomize this? Im unclear as to how I could implement this into my code.

You have a random number here in between 0 and number of fruits - 1, right?

1 Like

I kinda get what you’re getting at… Im just unsure how to implement it.

I want to do :

let randomNum = Math.floor(Math.random()*fruitArray[ ]);

but that returns me Nan. :thinking:


let randomFruit= fruitArray[randomNum];

didn’t think I could pass in a value into the square brackets like that :sweat_smile:

1 Like

Not even sure why that would work lol :sweat_smile:

the challenge about accessing object properties with variables is here if you need a review:

edit: sorry, it’s an array here…

I think this one maybe is the first one about arrays and a variable for index:

1 Like

Last thing!

any idea why the final alert isn’t triggering? I feel like it should since the while loop breaks after fruitGuess!=randomFruit.

Thanks! This will help for sure. Ive totally seen this done before but I forget all the things I can do and manipulate with these things. Sometimes I just pass over information, especially if im focused on a broader concept.

Ah fixed the alert :crazy_face: I put it in an else if. Im unclear as to why it would not run just sitting outside of the while loop like this :

if (convertGuess != randomFruit){
alert(“try again”) }

} while (fruitGuess!=randomFruit);

alert(“YOU WIN”);

Thanks so much for your help!

1 Like

This is very simplified, but the JavaScript interpreter is basically plugging in the values for all the variable references on a line until there aren’t any left, and then evaluating.

So, if you had something like this:

const fruits = ["apple", "banana", "peach"];
const index = 1;

console.log(fruits[index].toUpperCase()); // "BANANA"

Then on that last line, it’ll see the reference fruits, and plug in its value:

console.log(["apple", "banana", "peach"][index].toUpperCase());

And then plug in the value of index:

console.log(["apple", "banana", "peach"][1].toUpperCase());

And then it evaluates from inside out:


Anyway my point is, you can always use a variable in place of an explicit value!