Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to filter this array?

let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,38, 39, 40];  


let newArr = arr.filter(x => x < 20 && x > 29);

console.log(newArr);

I have the above array and I want to filter numbers 1 - 19 and 30 - 40 into a new array. The new array should not contain numbers between 20 - 29.

All my results seem only include 20-29 or [], which is of course not what I want. Is there another way to write NOT between 20 - 29?

1 Answer

The problem is in your filter function. Since you want numbers below 20 OR those above 29, then your function should be like this instead:

let newArr = arr.filter(x => x < 20 ||  x > 29);