Functional Programming - Use Higher-Order Functions map, filter, or reduce to Solve a Complex Problem

Tell us what’s happening:
Describe your issue in detail here.
Hello everyone I need to know Why my code did not pass. I’ve use map filter and reduce. map and filter work properly but reduce did not work. i want it to do the square of the items in the array. someone can help me

Your code so far

const squareList = arr => {
  // Only change code below this line
  return arr.filter((a)=>{return a>0}).filter((a)=>{return Number.isInteger(a)}).reduce((arr,b)=>{return arr.push(b*b)},[]);
  // Only change code above this line
};

const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
const squareList = arr => {
  // Only change code below this line
  return arr.filter((a)=>{return a>0}).filter((a)=>{return Number.isInteger(a)}).reduce((arr,b)=>{return arr.push(b*b)},[]);
  // Only change code above this line
};

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0

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

Link to the challenge:

first of all, don’t write long complicated lines like that. break it down to some extent. for example lets find positive integers in one go:

let posInts= arr.filter((a)=> a>0 && a % parseInt(a) === 0)

then we can use console.logs in different parts of the code to see what each part is doing

console.log('posInts:',  posInts)

thanks i have already found a solution

1 Like

wrap it in blur spoiler here

[quote=“kaghenimbale, post:1, topic:563583”][quote=“kaghenimbale, post:1, topic:563583, full:true”]
Tell us what’s happening:
Describe your issue in detail here.
Hello everyone I need to know Why my code did not pass. I’ve use map filter and reduce. map and filter work properly but reduce did not work. i want it to do the square of the items in the array. someone can help me

Your code so far

const squareList = arr => {
  // Only change code below this line
  return arr.filter((a)=>{return a>0}).filter((a)=>{return Number.isInteger(a)}).reduce((arr,b)=>{return arr.push(b*b)},[]);
  // Only change code above this line
};

const squaredIntegers = squareList([-3, 4.8, 5, 3, -3.2]);
console.log(squaredIntegers);
const squareList = arr => {
  // Only change code below this line
  return arr.filter((a)=>{return a>0}).filter((a)=>{return Number.isInteger(a)}).reduce((arr,b)=>{return arr.push(b*b)},[]);
  // Only change code above this line
};

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0

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

Link to the challenge:

[/quote]

return arr .filter((a)=>{return a>0 && Number.isInteger(a)}) .map((a)=>{return a*a});
[/quote]

return arr
.filter((a)=>{return a>0 && Number.isInteger(a)})
.map((a)=>{return a*a});

does it work?
not here

yes it work very well.
show me what you have done

just did in parts and logged results for each step. eg:

...
  const posInts= arr.filter((x)=> x % parseInt(x) === 0)
  const skwaird=posInts.map(x=>x*x)
...

Hi there, please do not share code solutions on the forum.
We encourage supporting others with tips and hints etc. (not by posting solutions)

Thanks for your understanding

1 Like

your code is working bro run it, it will work

1 Like

You can use the JS array reduce method to get the desired result. But, the way the reduce is used in the above code is not correct.

Take this code snippet from the above code:

.reduce((arr,b)=>{return arr.push(b*b)},[])

The arr.push method as in Array#push definition says:

The push() method adds one or more elements to the end of an array and returns the new length of the array.

You are to return a concatenated array, not the length of the array - hence the result is not correct.