What is the parameter in the arrow function syntax given below?

What is the parameter in the arrow function syntax given below?
0

#1

Tell us what’s happening:
I am just trying to review concepts so that I can learn to understand javascript. This is an isolated example compared to my other question about for loops, as I was suggested to review the material.Maybe I’ll even do the javascript section again.
I want to return to free code camp once I review the basics to test my knowledge.

Your code so far


Here are the two methods
fruits.forEach(function(ChosenFruit){
  console.log(' I want to eat a ' + ChosenFruit);
});

fruits.forEach(fruit => console.log('I want to eat a ' + fruit)); ///is the parameter fruit?

fruits.forEach(function(ChosenFruit){      //the paramater is ChosenFruit here?
  console.log(' I want to eat a ' + ChosenFruit);
});

///potentially lacks a closing curly brackets
  // Only change code below this line.

fruits.forEach(fruit => console.log('I want to eat a ' + fruit))

fruits.forEach(function(ChosenFruit){      //the paramater is ChosenFruit here?
  console.log(' I want to eat a ' + ChosenFruit);

///potentially lacks a closing curly brackets

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:


#2

///is the parameter fruit?

Yes


#3

forEach is a higher order function which allows you to iterate over an array and apply a callback function to each element of the array. The callback function for forEach takes three arguments. The first argument is the current element value being iterated. The second argument is the index of the current element being iterated. The third argument is the actual array that is being iterated over. Most of the time, you will probably only make use of the first and second arguments of this function, but the third is there in case you need it.

All of the code you wrote above except the last one, are correct syntax. The last one (below) is missing a closing curly bracket which closes the forEach callback function AND is missing a closing parentheses which closes the forEach function.

You seem to understand what fruit and ChosenFruit represent and how to use them.


#4

Is the following example now corrected?


#5

The } should be before the /// here? comment and there should also be a ) after it.

fruits.forEach(function(ChosenFruit){ // the paramater is ChosenFruit here?
console.log(' I want to eat a ' + ChosenFruit);
});

#6

[quote=“randelldawson, post:5, topic:172222”]
fruits.forEach(function(ChosenFruit){ // the paramater is ChosenFruit here?
console.log(’ I want to eat a ’ + ChosenFruit);
});

Is this the correct use of closing and beggining brackets when I use a function? I ask because I am trying to get over this maladaptive pattern.

I think I quoted you here.


#7

Where are you writing your code at the moment? A ‘proper’ editor, like Atom or VS Code will help you to match your brackets and braces and to indent your code properly, which will help a lot with syntax issues.


#8

Yes. I think I understand how to do it. Some of this is just practice unfortunately. But im aware of jsbin