Write Higher Order Arrow Functions Challenge too Difficult

Write Higher Order Arrow Functions Challenge too Difficult
0

#1

Tell us what’s happening:
I am moving along through the course fine when periodically I hit a problem like this that uses really challenging functions (in combination) that have only been seen once (maybe twice) at a point that I cannot recall. So I have to use the spoiler to get through the lesson

It feels like freecodecamp expects the user to have a second nature of of use for the functions, but from a resource other than freecodecamp. Do you have any suggestions on how to memorize or creatively build a library of functions that are most useful in a daily developer’s life?

Thank you for any suggestions.

  • Dad-with-little-time-and-must-be-extremely-efficient

Your code so far


const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
  "use strict";
  // change code below this line


  const squaredIntegers = arr.filter( (num) => num > 0 && num % parseInt(num) === 0 ).map( (num) => Math.pow(num, 2) );
      return squaredIntegers;



  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/es6/write-higher-order-arrow-functions


#2

Hi,

The square of 2 is 2^2, but the square of 3 is 3^3. Also, what’s the reasoning behind parseInt(num) === 0?

In my opinion, FCC is just a guide, like a syllabus, to give you some guidance on the topics you should learn to get a strong understanding of the basics. I wouldn’t try to solve the challenges just by reading the tutorials on its website. Again, it’s good as a guidance, but as soon as you have any trouble understanding a topic, you should find help by searching on Google, Stackoverflow or any other resources. YouTube has lots of training videos you might find useful while getting started with Javascript.

That being said, coding is the best way to learn. Try to code and if you’re stuck, Google will be helpful.

One more thing: don’t give up! :slight_smile:

Happy coding.


#4

If you read closely, it’s num % pareseInt(num) === 0 which is a mathematically correct way to check if number is an integer or fraction. Some people don’t know (or prefer to use) Number.isInteger.


#5

Actually that’s what I wanted to write but I missed the first part. Thanks.

I know it’s a perfect valid way of doing it, I was just wondering if the idea behind it was clear. I think it’s much clearer to use num % 1 === 0 because you just need to divide the number by any integer to know if the number is an integer or not.


#6

@JimmyStevens I try to read forum posts from people working on similar things and take notes on every function, concept, or interesting thing I come across.

For example, @ex-jedi recently posted a solution that uses the Infinity global variable (Need Help - Basic Algorithm Scripting - Return Largest Numbers in Arrays). I hadn’t thought to solve it that way, but I like it. I plan to add it to my notes because I may find it useful at some point in the future.

I use org-mode for this, but I think others use Evernote, Google Drive, and other similar systems.


#7

That is also a valid way. However whether you do num & parseInt(num) or num % 1 the result is the same in both cases. The question of which one is more performant is a bit dicey for num % 1 will cause subtraction operation to be performed num - 1 times whereas num % parseInt(num) would cause subtraction to be performed just ` times.
That is the only trade off here.

Some use cases

1.5 % 1 = 0.5
1.5 % parseInt(1.5) = 1.5 % 1 = 0.5

33.21 % 1 = 0.21
33.21 % parseInt(33,21) = 33.21 % 33 = 0.21

#8

Do you have any suggestions on how to memorize or creatively build a library of functions that are most useful in a daily developer’s life?

@JimmyStevens - I still fumble through higher-order functions, and the arrow function syntax was a concept that threw me off many times. If you haven’t come across the book “Eloquent JavaScript” yet, the Marijn Haverbeke’s chapter about higher-order functions was very useful for me. The book is available free online (Link to “Higher Order Functions” Chapter).

There are some really challenging exercises and sections in that book, and I am far from working through all of it, but working through the first five or six chapters, after I had gotten a feel for JavaScript’s syntax, was a useful way for me to start wrapping my head around some concepts (like higher-order and arrow functions) that were really hard for me.

If you can commit to getting through that chapter’s exercises, I think you’ll start to feel more comfortable with passing functions as arguments to other functions, thinking about return signatures, and working with arrow functions. You mentioned developing a “second nature” for those functions - maybe this will help?


#9

I believe this challenge is way off, I mean someone who doesn’t know about filter or map method might never be able to solve this challenge, at least that’s the only way I know and if we look at the solution, they also used those methods.

So why present this challenge to people if they hadn’t yet seen filter, map and reduce?


#10

@JimmyStevens
There at times appear to be some speed bumps in the curriculum. You can be flying through challenges like an open-book Home Ec exam only to be stopped dead by a question asked in such a way that you might not know the answer even if it were handed to you. Consider these the “uphill” parts of the race. Take your time to absorb the material and you will be rewarded later.

I did not think the example was a particularly good example of succinct code with arrow functions. The second part was hardly any shorter and probably less clear than the first. What exactly you were being asked to do was not obvious to everyone and the algorithmic part of the challenge overshadowed the higher order function lesson.

That said, the challenges are just a framework to keep you on track. You will surely need more outside research as you progress through the program.

As far as functions to know I would at least know of any of the array methods listed on the MDN array page. Some of the more useful ones you should commit to memory - push, pop, shift, unshift, reduce, map, filter, forEach,slice, splice, join and concat off the top of my head. Do the examples yourself, know the parameters provided to the callbacks, know if they return a new array or mutate the array itself. Also know method chaining.

For committing to memory, consider revisiting some previous challenges with the intent of refactoring using array methods and higher order functions. You’ll see that some can be reworked in a clear, almost self-commenting way that resembles spoken word. (And occasionally you will find that sometimes the trusty for…loop is really the best way to go) . In future challenges attempt to write better code with these - nested for…loops are almost always clearer if rewritten with an array method(s).

Tweaking 10 lines of loop code into a short, sweet one-liner is good practice too. The result often will be too complicated to ever use real-world but the exercise is still good practice.

My last advice to you - don’t do all your work in FCC. Use repl.it or codepen.io or similar and copy your final solution into FCC. You’ll be less tempted to move on by that green checkmark and more likely to improve your code beyond just good enough to pass. You’ll also have a great scratch pad to explore any “what if” notions.

Good luck to you.

                      // filter pos ints                    new array of squares
const squaredIntegers = arr.filter(el=> el%1 === 0 && el>0).map(el=>el*el);

#11

Some good advice here, thanks @alhazen1!


#12

There is a section further on that explains the methods, and it makes much more sense after that. The ES6 section is imo in the wrong place because of issue like this. Several exercises expect knowledge of concepts that are carefully explained in detail in later sections, and it’s not particularly good for learners, it kinda breaks the curriculum slightly. If you had done that section first, and then the challenge simply introduced the syntax for arrow functions, it would be much more sensible.