CrossRoad State... I DON"T KNOW WHAT TO DO

Tell us what’s happening:
I Don’t know what to do so far… This challenge is making me perplexed as if don’t understand this FP… What should i do get HINTS or Re-Take the course !!

  **Your browser information:**

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

Challenge: Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem

Link to the challenge:

what have you tried so far? what is giving you issues?
the forum can help guide you to the right answer

Hi @AdamsAlbarka !

It is normal to struggle when you are learning higher order functions for the first time.

First start off by solving this problem away from code.
How would you solve this as a human?

Your task is to take a list of numbers and return a new list of squares of only the positive integers

For example, if you were given this list
[-3, 4.8, 5, 3, -3.2]

you would first filter out the negative numbers and numbers with decimals so you are only left with the positive integers.

[ 5, 3]

Then you need to square each number in that new list and return it.
[ 25, 9 ]

That is how to solve it.

Slowly convert what we just did into code.

Start by trying to figure out how to filter out negative numbers and numbers with decimals using the filter method.

Try to write out some code and if you get stuck then we can help you trouble shoot from there.

Hope that helps!

1 Like
const squareList = arr => {

  // Only change code below this line

  arr.filter(arr => arr >= 1)

    .map(arr => arr)

    .reduce((sumAll, all) => sumAll * all) /

     arr.filter(arr => arr >= 1)

     

  return arr;

  // Only change code above this line

};

const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);

console.log(squaredIntegers);

Here is my code, still bringing out the decimal number and all in the console
Thanks

all those methods return a new array, you are not saving that returned array anywhere, and arr is not being changed


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

First off, don’t use “arr” within the callback functions, it’s super irritating. The callback functions are applied to every element of the array one-by-one, so use a distinct name for them.
Second please read the code and actually explain to yourself, what you are doing, because I don’t know what your goal here is.

For example, map(arr => arr) does a whole lot of nothing and none of your methods creates squares.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.