Hello, I need to discuss the following code a little:
allWords = [
[ "go", "went", "gone" ]
, [ "be", "was", "been" ]
, [ "eat", "ate", "eaten" ]
];
allWords.forEach( function( words ) {
ix = Math.round( Math.random() * 2 );
wordToAsk = words[ ix ];
questions = [];
words.forEach( function( word ) {
if( wordToAsk != word ) {
questions.push( word );
}
else {
questions.push( "?" );
}
} );
question = questions.join( " - " );
answer = prompt( question );
if( answer == wordToAsk ) {
console.log( "Correct!" );
} else {
console.log( "Wrong" );
}
} );
- I don’t understand how the
words
array is created here.words
is used as a parameter to an anonymous (is it anonymous?) forEach-function:words.forEach( function( word )
- and is later looped on itself withword.forEach( function( word )
.
Each array of verbs insideallWords
becomes the newwords
at each iteration of the forEach-loop.
But I kinda don’t understand how. I mean it was never declared before being used in the function definition, so I’m thinking there should be some kind of reference error. How does Javascript “know” where to look for the content ofwords
here?
The code is commented here, but I can’t find an explanation for my problem.