2

if block is executing properly. What is wrong with the code?

let arr = [4, -4, 5.8, 1.6];
let newA = arr.filter((x) => {
  if (x > 0 && Number.isInteger(x)) {
    return x * x;
  }
})

console.log(newA);

Expected output

[16]

Actual output:

[4]

why?

2
  • 3
    Filter should return true/false result, what you're looking is probably map. Commented Feb 25, 2019 at 14:36
  • Because filter expects the predicate to return a boolean (true/false), and you return x * x which evaluates to for example 16 for your first element which is a truthy value i.e. is not filtered from your result set. Commented Feb 25, 2019 at 14:36

6 Answers 6

5

The given code, does the filtering part, because any quadratic positive integer is a truthy value, but it does not return the new value.

It looks like, you need two actions, one filtering and changing the filtered values.

In this case you need Array#filter for getting positive interger numbers and Array#map for a quadratic value.

let array = [4, -4, 5.8, 1.6],
    result = array
        .filter(x => x > 0 && Number.isInteger(x))
        .map(x => x * x);

console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

1

The reason is already explained in previous answer, you can use reduce if you want to use single function

let arr = [4, -4, 5.8, 1.6];
let newA = arr.reduce(function(accumulator, current) {
  if (current > 0 && Number.isInteger(current)) {
    accumulator.push(current * current);
  }
  return accumulator
}, [])
console.log(newA)

1 Comment

This is how I will solve the problem too... There is no need to iterate twice on the array.
1

Filter just filters the value and return boolean result.. You can use map as below,

let arr = [4, -4, 5.8, 1.6];
let newA = arr.filter((x) => x > 0 && Number.isInteger(x))
.map((x) => x * x);

console.log(newA);

Comments

1

With reduce method :

let arr = [4, -4, 5.8, 1.6];

let newA = arr.reduce( ( acc, x ) => {
  if ( x > 0 && Number.isInteger(x) ) { acc = acc.concat( [x*x] ) } ;
  return acc;
}, []) ;

console.log(newA);

Comments

0

Not sure about the use case, but looks like to be "get the square of all positive integers in an array". If it is then the below code should work.

let arr3=[4,-4,5.8,1.6];   
let new3 = arr3.filter((x)=>{ 
    return (x>0 && Number.isInteger(x));
}).map(function(y){ 
return y*y;
})

First extract the list of all positive integers and then return the square of each item in the list.

Comments

-1

let arr = [4, -4, 5.8, 1.6];
let newA = arr.filter((x) => {
  if (x > 0 && Number.isInteger(x)) {
    return x * x;
  }
})

console.log(newA);

1 Comment

Just copying the code from the question is not an answer, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.