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 function is not explicitly returning anything, so actually squareIntegers looks like below:

[ 
  undefined,
  undefined,
  undefined,
  undefined,
  undefined,
  undefined,
  undefined,
  undefined
]

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.

You need to make sure you are reading all the instructions. You are only supposed to be squaring positive integers. You are squaring all the positive numbers in the realNumberArray.

You need to make sure the final array only contains squares of positive integers.

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.

They are not empty. They are actually the value undefined. You could use the filter method to make sure the array you use the map on only has positive integers. Then your map method would have no if statement logic and would only return squared values. The reason you are getting undefined is because if the number is not an integer (in your current map callback function) you implicitly return undefined. Functions which do not return values will always return undefined.

Also, the instructions say to square positive integers. Now you are squaring integers (both negative and positive).

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.