I am going through a boot camp at freeCodeCamp and am stuck on a problem, trying to use other resources than the 'get hint' option they provide.
I am currently tasked with writing a function that finds all the positive integers in an array, squares them, then returns the new array. I do know what each map, filter, and reduce do, but seem to be struggling with how they get implemented. Here is the code from before I started working...
const squareList = arr => {
// Only change code below this line
return arr;
// Only change code above this line
};
const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
and the code after I added my own spin...
const squareList = arr => {
// Only change code below this line
arr.filter(arr => arr.length > 0)
arr.map(x => x * 2)
return arr
// Only change code above this line
};
const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
so after running the code I put in, I at least don't get an error, it just gives me back the same array as what is input. it doesn't even sort out just the positive integers, so I'm quite confused on what I need to do. These challenges thus far have not needed any elaborate code, most solutions have been just a few lines of simple code but as a beginner I'm trying to get better at knowing what to call.
I have consulted documentation on developer.mozilla.org to try and get the right syntax, which I think has helped in providing an actual array instead of an error, but I could be wrong.
also, since this is a challenge, they do not want me to use 'for', 'while' or 'forEach()'.
Any help is appreciated, thank you!
EDIT: I want to add, as I was writing this question I tried a few different things like changing line 3 of my code to say...
squareList.filter(squareList = > squareList.length > 0)
just to test it out, and it did not work. If I were to guess, its not that I'm calling the wrong thing, it's that I'm calling the right thing in the wrong way.
EDIT 2: Thanks for the help everyone, it does make more sense. It seems I was about halfway there, I was calling the correct objects, but not filtering them properly. I also needed to make sure I returned the changed array instead of assuming it just returned it. I had spaced to use 'Math.round' to ensure it only used integers instead of squaring everything, and using '(x => x * 2) was just bad, I should have done (x => x * x) thats just me not thinking clearly. I did end up solving the challenge, thanks so much!
CodePudding user response:
I'm not sure I understand the limitations because they only state write below and above lines I would use anyway. But this works, much like your code, only some minor tweaks.
const squareList = arr => {
// Only change code below this line
arr = arr.filter(item => item > 0 && Math.round(item) == item).map(item => item * item).sort((a, b) => a - b);
return arr;
// Only change code above this line
};
const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);