JavaScript filter() method in 6 minutes! 🚰

Поделиться
HTML-код
  • Опубликовано: 19 ноя 2024

Комментарии • 19

  • @BroCodez
    @BroCodez  Год назад +10

    // 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;
    }

  • @joehaar1466
    @joehaar1466 Год назад +3

    I'm learning Python with your videos. The best material I could find so far. Thank you, bro.

  • @alecrestorick
    @alecrestorick 27 дней назад

    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.

  • @suntoshaqula
    @suntoshaqula 28 дней назад

    crisp and straight awesome job

  • @Aarif6904
    @Aarif6904 7 месяцев назад

    You made the code structure easier for me. Thank you so much brother❤

  • @piotrmazgaj
    @piotrmazgaj 3 месяца назад

    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.

  • @TheSwoopster
    @TheSwoopster 6 дней назад

    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.

  • @xzex2609
    @xzex2609 Год назад +2

    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 ??

  • @xzex2609
    @xzex2609 Год назад +6

    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

    • @nikolabosevski1435
      @nikolabosevski1435 Год назад +1

      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

    • @xzex2609
      @xzex2609 Год назад +2

      @@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.

  • @juniorrokudevelopertutoria3734
    @juniorrokudevelopertutoria3734 22 дня назад

    Ok, got the idea, thanks!

  • @Rafael_Perez21
    @Rafael_Perez21 2 месяца назад

    Excelent video bro

  • @sportsknowledge92
    @sportsknowledge92 9 месяцев назад +2

    Please create small small project in js

  • @kathikr9360
    @kathikr9360 7 месяцев назад

    thank you

  • @ФёдорСёмочкин
    @ФёдорСёмочкин 7 месяцев назад

    it cool, thanks !😉

  • @nikolabosevski1435
    @nikolabosevski1435 Год назад +1

    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

    • @julios8228
      @julios8228 9 месяцев назад +2

      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.