ES6: Write Higher Order Arrow Functions problem

Some help here please.

I have this code:

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
  "use strict";
  // change code below this line
  //const squaredIntegers = arr;
 
   
 let squaredIntegers = arr.map(x => {
     
       if(x>0) { x * x }
              
 });
    //Do something
 
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

and I get an empty array: ,

Your map callback is more complicated that it needs to be. You dont need an if statement or curly braces.

You can take a loot at this, specifically the second example and then figure out how to write that as an arrow function in the .map() to work for your problem.
https://www.w3schools.com/jsref/jsref_map.asp

Thanks for your input.

This is the array I get now: 16,31.359999999999996,9.8596,1764,36,69.5556,

This is my new code:

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
  "use strict";
  // change code below this line
  //const squaredIntegers = arr;
 
const myFunction = (num) =>{
  if(num>0) return num * num;
}  

 let squaredIntegers = arr.map(myFunction);
    //Do something 


  // change code above this line
  return squaredIntegers;
}; 
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

Take a look at https://www.w3schools.com/jsref/jsref_isinteger.asp to assist you in determining an integer.

I did not realize they were only supposed to do positive integers. I could have provided some better information otherwise. Going to start asking for links to the challenges so i can better understand what is needing to be accomplished.

Thanks.

It got a little better. Now my code is:

const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = (arr) => {
  "use strict";
  // change code below this line
  //const squaredIntegers = arr;
 
const myFunction = (num) =>{
  if(Number.isInteger(num)) return num * num;
}  

 let squaredIntegers = arr.map(myFunction);
    //Do something 


  // change code above this line
  return squaredIntegers;
}; 
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers); 

and my array is: 16,1764,36,4

so, I have these empty entries.

Thanks for your input.

I managed to clear the array from the undefined values with filter and to only square positive numbers. It now works. Thanks.

2 Likes

Members I am unable to understand that what is going in code ??

what is filter(), reduce(), map() used for ?

All three are array methods.

  • List item

Thefilter() is used when you want to output only the conditions you specify within the () which is usually a function.

the reduce()method will reduce the elements of your array to only one output . What the reduce() method should do with the elements of your array ,you write that code within the () which is is also a function.

the map() method is used on an array when you want to do something with each element of an array and output an array.

for more help try this link below.
https://fundamentalsofcode.com/javascript-array-methods-filter-map-and-reduce/
Hope this helps . I am also just a beginner so bear with me if my explanation is not perfect.