// filter() = creates a new array by filtering out // elements with a callback // ----------- EXAMPLE 1 ----------- let numbers = [1, 2, 3, 4, 5, 6, 7]; let evenNums = numbers.filter(isEven); let oddNums = numbers.filter(isOdd); console.log(oddNums); function isEven(element){ return element % 2 === 0; } function isOdd(element){ return element % 2 !== 0; } // ----------- EXAMPLE 2 ----------- let ages = [16, 17, 17, 18, 19, 20, 65]; let adults = ages.filter(isAdult); let children = ages.filter(isChild); console.log(children); function isAdult(element){ return element >= 18; } function isChild(element){ return element < 18; } // ----------- EXAMPLE 3 ----------- const words = ['apple', 'orange', 'kiwi', 'banana', 'pomegranate', 'coconut',]; const longWords = words.filter(getLongWords); const shortWords = words.filter(getShortWords); console.log(shortWords); function getShortWords(element){ return element.length 6; }
Thank you for making all these videos. The way you explain everything helps me to understand. You are a very good teacher! You are a great help in my learning Java Script journey. Best wishes to you for all your endeavours.
gotta be real, its important to remember type coercion doesnt help you when using .filter - if you're using a string version of a number to try to filter in an array of numbers, you better convert that string.
I use expressions element % 2 for odd or return !( element % 2) , are these expressions are equal like == or strictly === 0 when we don't mention them ??
One bad thing about JS is that it works when there is errors and it don't caught errors like python or ... and shows unexpected behavior due to these errors , I wonder if there is any linter for this kind of situation . but believe me it got 30 minutes to find that i had a typo in element.lenght and I did not see it . end everything work like there is nothing but with a bug
There are some extensions on vs code for linting or you also use typescript which is a bit harder to learn at first but if you know python c++ or any similar language with types you should have no problem. It shows you a lot more errors so it's really useful
@@nikolabosevski1435 Thanks a lot for your help , I know there must be some better tools for catching errors. but sometime in the definition of a method is that behave unexpectedly so they are as they are and to avoid such situations i think one should know the behavior of a given method on such situations.
One thing about JS that has been a bit confusing is as in your example about isEven function even thoufh it takes a parameter when you define it, when you use it in the .filter method you dont pass an argument in it. Why is that? I have just always taken it for granted but im genuinely curious why it works that way
The parameters are all the elements in the numbers array. The filter function is going to iterate through all the elements of the numbers array and will evaluate the condition in the isEven() function.
// filter() = creates a new array by filtering out
// elements with a callback
// ----------- EXAMPLE 1 -----------
let numbers = [1, 2, 3, 4, 5, 6, 7];
let evenNums = numbers.filter(isEven);
let oddNums = numbers.filter(isOdd);
console.log(oddNums);
function isEven(element){
return element % 2 === 0;
}
function isOdd(element){
return element % 2 !== 0;
}
// ----------- EXAMPLE 2 -----------
let ages = [16, 17, 17, 18, 19, 20, 65];
let adults = ages.filter(isAdult);
let children = ages.filter(isChild);
console.log(children);
function isAdult(element){
return element >= 18;
}
function isChild(element){
return element < 18;
}
// ----------- EXAMPLE 3 -----------
const words = ['apple', 'orange', 'kiwi', 'banana', 'pomegranate', 'coconut',];
const longWords = words.filter(getLongWords);
const shortWords = words.filter(getShortWords);
console.log(shortWords);
function getShortWords(element){
return element.length 6;
}
Do django course please
I'm learning Python with your videos. The best material I could find so far. Thank you, bro.
Thank you for making all these videos. The way you explain everything helps me to understand. You are a very good teacher! You are a great help in my learning Java Script journey. Best wishes to you for all your endeavours.
crisp and straight awesome job
You made the code structure easier for me. Thank you so much brother❤
This is my seal. I have watched the entire video, understood it, and I can explain it in my own words, thus I have gained knowledge. This is my seal.
gotta be real, its important to remember type coercion doesnt help you when using .filter - if you're using a string version of a number to try to filter in an array of numbers, you better convert that string.
I use expressions element % 2 for odd or return !( element % 2) , are these expressions are equal like == or strictly === 0 when we don't mention them ??
One bad thing about JS is that it works when there is errors and it don't caught errors like python or ... and shows unexpected behavior due to these errors , I wonder if there is any linter for this kind of situation . but believe me it got 30 minutes to find that i had a typo in element.lenght and I did not see it . end everything work like there is nothing but with a bug
There are some extensions on vs code for linting or you also use typescript which is a bit harder to learn at first but if you know python c++ or any similar language with types you should have no problem. It shows you a lot more errors so it's really useful
@@nikolabosevski1435 Thanks a lot for your help , I know there must be some better tools for catching errors. but sometime in the definition of a method is that behave unexpectedly so they are as they are and to avoid such situations i think one should know the behavior of a given method on such situations.
Ok, got the idea, thanks!
Excelent video bro
Please create small small project in js
thank you
it cool, thanks !😉
One thing about JS that has been a bit confusing is as in your example about isEven function even thoufh it takes a parameter when you define it, when you use it in the .filter method you dont pass an argument in it. Why is that? I have just always taken it for granted but im genuinely curious why it works that way
The parameters are all the elements in the numbers array. The filter function is going to iterate through all the elements of the numbers array and will evaluate the condition in the isEven() function.