Struggling with Iterators and Arrow Functions

Hi everyone,

I just wanted to share something and ask couple of questions.

I’m really struggling with iterators and I think the issue is mainly the arrow functions. It looks like a line of mess, my brain just can’t process what goes where.

const mashPotatoes = arr => arr.whatever((item1, item2) => item1 > item2)

All I see is a dump of ='s and =>'s makes really difficult to read and understand. Even the “As a function declaration AND using a named function” option has all that arrow functions in it

Anyone else feel completely lost here? If you were feeling like that and found a way to resolve the issue can you please share your tips/tricks?

I’m doing full stack training and at “Practice JavaScript Syntax” part where I failed every single practice from 8 to 12, didn’t try 13 and 14 yet but it’s quite obvious now how it will go.

I started to lose my motivation and feeling like I’ll fail again.

Please post a link to the exercise you are working on. Thanks.

May we assume that whatever is a placeholder for an iterator method?

Hello,

Thank you for your reply. I’m not stuck on a specific exercise, I’m struggling with the arrow functions. My question was more about hearing other people’s experience about this specific syntax, if anyone else had issues understanding/remembering and what approach they used to overcome the issue.

Whole thing is spaceholder but again, that code on its own doesn’t mean anything. I’m not asking about “how to make x work”, it’s just sharing experience.

Thanks

I was struggling at first too. But now I cannot live without arrow functions :smiley:.

In addition to Codecademy, I found that reading articles helped a lot in understanding advanced programming concepts. Therefore, I advise you to also search for articles for any concepts you feel uncomfortable with.

Before taking a dive into arrow functions just make sure you understand how exactly Javascript works with functions. recommended article.

You should know that you can store functions in variables and also you can pass them as arguments.

Now let me try to explain arrow functions with my little experience. First remember that arrow functions are just syntax sugar for functions so there is nothing new here just an easier syntax

const sayHello = name => "Hello, " + name;

const sayHello = here you are storing your function in sayHello variable.

name => "Hello, " + name; this is your function
first, it takes one argument name, and here is the first note in arrow functions

  • If your function will take only one argument you can get rid of parentheses you also still can write it like that if you want (name) =>.

but if it will take more than one argument, you will have to wrap arguments with parentheses.

( param1, param2 ) => 

Secondly, The arrow => if your function will only return a value without any logic you can directly write the value you want to return after the arrow

// taking two arguments and return the sum of them.
const sum = (a, b) => a + b; 

sum(5, 6) // returns 11

but if you need to do some logic in your function you will have to add curly braces
const funName = param => { // write whatever you want like a normal function}

==============================================
As you can see, there is nothing new! Just an easier way to write functions you can write your code with normal functions if you want:

function mashPotatoes(arr) {
  return arr.whatever(function( item1, item2 ) {
    return item1 > item2
  }) // whatever is a method(function) that takes another function as an argument:D 
}

==============
Don’t give up and take your time in reading more articles to understand it well.

1 Like

Hey,

Thanks for that, very helpful. I’ll read it again in the morning with a fresh mind.

I think this is the only thing I disagree :joy: I find arrow function syntax much more difficult to read/understand.

1 Like

Sure because it’s still new to you, after a short time you will search for the one who added arrow functions to javascript to thank him :joy:.

If that happens, I promise you’ll be the first one hearing about that :smiley:

1 Like

On the whole, Arrow Functions were not intended to replace the standard function expression, but to reduce the code bloat in callbacks and to simplify helper functions such as the add() function mentioned above. The relaxed syntax is the only real difference.

Arrow functions do not need,

  • curly braces
  • return keyword
  • parenthesized single parameter

when they are simple, single expression returns.

Curly braces are needed around multiple statements, and when braces are employed the return keyword is required. Parens on the parameter are required when there is no parameter or multiple parameters.

const sine = x => Math.sin(x);
2 Likes

thank you for the details, tomorrow I’ll do a review and read more articles+examples about the subject.

I guess I’m reaching to burnout which causes short temper and tantrum :slight_smile: this might be a reminder to myself and whoever reads that, sometimes we need to rest.

have a good day! :slight_smile:

1 Like