forEach with anonymous function

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" );
} );
  1. 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 with word.forEach( function( word ).
    Each array of verbs inside allWords becomes the new words 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 of words here?
    The code is commented here, but I can’t find an explanation for my problem.

The array is not created, as much as accessed.

words => [ "go", "went", "gone" ], etc.

word => 'go', etc.

You don’t need to in-line everything, you can create those functions separately and then reference them by name.


It’s silly to define a function when calling it, it’s silly to do it here too. There are cases where you’d rather inline it but usually you’ll get easier-to-read code if you define it separately. I think codecademy might give the impression that those functions need to be defined right there and then or that they are in any way different from other functions.

If you forget where dealWithTenses gets its input from then you’re forgetting what array’s forEach method does.