Obviously I had to look at the hint and answer because there was absolutely no chance of me ever working it out myself.
const squaredIntegers =
arr.filter( (num) => num > 0 && num % parseInt (num) === 0)
.map( (num) => Math.pow (num,2));
I’m trying to decifer this and figure out what everything means and what each bit is doing. So far I think I understand it as this.
if (num>0 && num %parseInt)
num = 0;
Although I’ve never come across these new functions map filter etc. Not sure why the exercise just expects you how to know all this. Can someone please break down the top bit of code and explain what is happening pleeeeeease.
Do you understand how
map work? I’m trying to figure out which bit to start with to answer your question.
Hi there, @AlexofSmeg. Same here. I ended up looking at the answer as well and figuring out the logic later. There are links at the bottom of the answer page that explain in great detail the map(), filter(), and reduce() functions mentioned.
const squaredIntegers = // we're creating an object called squaredIntegers
arr.filter((num) => num > 0 && num % parseInt(num) === 0) /*we use the
filter function using the higher order arrow method, eliminating the need
to use the word "function" and a function name. This filter function takes
one argument called "num". The conditions are then outlined that "num"
has to be greater than zero and the remainder, indicated by the (%) symbol
must equal zero. */
.map((num) => Math.pow(num, 2)); /*the map() function allows us to call a
function on every element of an array we are searching and then return a
new array with the returned information. Here the author is using Math.pow,
but it could have also been written (num * num).
EDIT: Also to better explain the use of parseInt(), as taken from Mozilla’s MDN page:
If parseInt encounters a character that is not a numeral in the specified
radix, it ignores it and all succeeding characters and returns the integer
value parsed up to that point.
In pseudo code, I suppose we’d say “the number remainder of the parsed number must equal zero”.
I hope this helps.
Yeah its annoying learning the standard JS syntax then getting thrown a bunch of ES6 syntax all of a sudden. I find the template literals and spread operators very helpful and easy to use but am still getting used to arrow functions.
I think it was the udacity es6 course, I had a good time with that one but also went into it with a bit of prior experience.
If anybody know any good resources to practice es6 functions and es6 higher order functions, please link it
the “Use Destructuring Assignment to Assign Variables from Objects” sums up my point entirely. The explanation and example simply gives you no idea how to do the task at the end… wow
I’m ‘glad’ I’m not the only one who has found it difficult. I literally logged into this forum for the first time and the this post was the first on the list.
If you don’t know about filter and map before coming across with this challenge, there’s no way to solve it I think. After I solved it I looked at the solution and to my surprise they also used filter and map. I think they should explain those methods before presenting this challenge.
Don’t feel bad about looking at the solution, you did nothing wrong here.
So when you go thru a lesson and there’s something you don’t understand, make a stop, try to think in the right question to ask so you can google it and learn more about it.
I am a beginner too, but I already got through that part. freeCodeCamp is great for exercises, but sometimes the explanations are not enough. The way I solved it when I was stuck (I was completely stuck on ES6) was looking at videos on youtube. I began understanding filter, map, reduce, sort better. Also I stopped using freeCodeCamp for a week or so, I also checked the free course of watchandcode, that also helped me understand many things better. To study only with 1 source is hard, I personally mix many sources, and many youtube videos to understand topics. I still struggle A LOT, but the map and filter got A TINY LITTLE BIT easier for me after a while.
I suggest you check youtube videos about map and filter, because after reading your doubt, I think you are not understanding map and filter. And arrow functions too.
I don’t know if this is good or bad, but I feel happy reading this post! Knowing I was not the only one struggling with ES6, and still struggling.
I was about to ask, do you know any good youtubers or videos on youtube that you can recommend? I hope the rest or the curriculum isn’t as difficult as the ES6 part!!!
I couldn’t really recommend one specific youtuber, because many have helped me. But there is this one that helped me the most with the map, filter, sort, etc. part. which is Traversy Media. He also offers courses on Udemy, but I wouldn’t recommend that one, I purchased the course, and it is really dissapointing.