[Another solutions?] ES6: Write Higher Order Arrow Functions

[Another solutions?] ES6: Write Higher Order Arrow Functions
0.0 0

#1

Hi everyBuddy,

I actually figured it out, but I want to know how you guys would solve this with different approaches.

The problem asks you to return a new array with only positive integers that are squared. So:

  1. Expected new array: [4, 42, 6]
  2. Expected output: [16, 1764, 36]
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34];
const squareList = (arr) => {
  "use strict";
  // change code below this line
  const squaredIntegers = arr;
  // change code above this line
  return squaredIntegers;
};
// test your code
const squaredIntegers = squareList(realNumberArray);
console.log(squaredIntegers);

Here’s how I do it

// change code below this line
const filterdArr = arr.filter(num => num > 0 && (num % 2 === 0 || num % 2 === 1));
const squaredIntegers = filterdArr.map(num => num * num);
// change code above this line

I used .filter() to filter the positive integers first, then used .map() to return a new squared array.

I have 2 concerns:

  1. Is there another way to determine whether a number is integer?
  2. Can I solve this without using filter first? Or more sophisticated (maybe complicated), is there a way I can use .filter() as nested with .map(), so I don’t need to declare filteredArr.

Thanks everyone!


#2

Hi

Yes. If you divide by 1 and there is no remainder then it would be an integer. (Check that it is not zero too)

num % 1 === 0

Yes. Many array methods return an array so can be chained. I would still filter first, then map as it seem clearer to me (and why do a math operation on a number that you are discarding anyways?)

const squaredIntegers = arr
		.filter(num => num > 0 && (num % 2 === 0 || num % 2 === 1))
		.map(num => num * num);

	// change code above this line

Normally you could return the result of this directly without saving to a local variable but the challenge as written implies that you save that to a local variable squaredIntegers and then return the value of that variable.


#3

Ha! You’re right. If there’s some fraction then the remainder of dividing by 1 is not 0. That’s simple but clever! Why didn’t I think of that?

Also, you remind me that chaining is not unusual in JS. It’s just that I don’t use if often so I can’t think of it. And since other numbers are filtered, there’s no reason for .map() to do math operation on them. I guess it’s your point on “nested .filter()”.

Thanks @alhazen1


#4

Using the test [4, 5.6, -9.8, 3.14, 42, 6, 8.34]
Filter first makes sense because

Filter 7 times + square 4 times = 11 operations
vs
Square 7 times + filter 7 times = 14 operations

I’m sure you could come up with a single method solution using reduce() where

  1. you test for integer
  2. and if integer square
  3. then push square to accumulator array

but that would be harder to understand at a glance and I don’t know that it would run any faster. Unless you were repeating the operation many times probably not even worth checking which is fastest.


#5