How can you access an element in a list with over 10k values for a dataset in code.org

Viewed 33

so I am trying to recreate a wordle game inside of code.org using the wordle dataset.

//Getting Wordle Answer 
var answers = getColumn("Wordle", "validWordleAnswer");
var letters =  ["letter1", "letter2", "letter3", "letter4", "letter5"];
var index = (randomNumber(0, answers.length));
console.log(index);

So ofcourse the console.log outputs my index number, but out of the 10k words that are stored in the dataset, how can I access the specific one that the randomnumber generated? Foe example the index was 500, what can I write inorder to console.log the correct answer? Thanks?

1 Answers

The length of the array doesn't matter, 1 element or 20 billion. You still access it with bracket notation:

console.log(answers[index]);

Here's a small example (remember, indexing starts at 0):

const answers = ["chewy", "rocks", "piano", "forte", "phone"];
const index = Math.floor(Math.random() * answers.length);

console.log(index);
console.log(answers[index]);

Related