It's giving me the right output onto the console but is not passing the last 2 test cases. Any help would be appreciated! cheers:)

Tell us what’s happening:

Your code so far


var newArr=[];
const squareList = (arr) => {
  var tempMap=arr.map(item=>{
    if(item>=0){
      return item;
    }
    else{
      return null;
    }
  })
 // Only change code below this line
var tempFiltered= tempMap.filter(items=> {
   if(Number.isInteger(items))
   {
     return items;
   }
   else{
     return null;
   }
 });

var reduced=tempFiltered.reduce((acc,cur)=>{
 return newArr.push(Math.pow(cur,2))
},0);

 return newArr;
 // Only change code above this line
};

const squaredIntegers = squareList([-3.7, -5, 3, 10, 12.5, 7, -4.5, -17, 0.3])
console.log(squaredIntegers);

Your browser information:

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

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

Link to the challenge:

newArr is a global/top-level variable and is not being reset between function calls, move it inside the function instead.

You can try logging it out with the array outside and inside the function, to see this.

let squaredIntegers = squareList([-3.7, -5, 3, 10, 12.5, 7, -4.5, -17, 0.3])
console.log(squaredIntegers);

squaredIntegers = squareList([4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2])
console.log(squaredIntegers);
1 Like

Thanks it worked , i’m new to JS and i hope you don’t mind and i have another doubt , i feel like i’m far lagged compared to the short answers here . will i reach that level someday?

I don’t see why not, just keep practicing and learning. Functional style code using ES6 syntax can be a little hard when you are new to it. It will get better with time. I’d suggest also looking at some tutorials on YouTube and articles on this subject to help out.

Also, shorter code is not always better code. Sometimes it is good to be explicit about the intermediate steps. Just because you can do it all inside a reduce doesn’t mean you have to, or that it is better. Strive for readable code, not short, fancy and clever code that other people will find hard to read.

1 Like

Thanks man , really appreciate your suggestion. i hope i will make it there someday.