Implement a custom forEach in javascript | JS Interviews

Поделиться
HTML-код
  • Опубликовано: 10 сен 2024
  • Visit chaicode.com for all related materials, community help, source code etc.
    Javascript interview series
    Sara code yaha milta h
    github.com/hit...
    Discord pe yaha paaye jaate h:
    ""/discord
    Instagram pe yaha paaye jaate h:
    / hiteshchoudharyofficial

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

  • @chaiaurcode
    @chaiaurcode  Год назад +23

    Small correction. It’s actually this.hasOwnProperty(i)
    I missed it while explaining.

    • @Error404Codes
      @Error404Codes 11 месяцев назад +1

      i think it's actually this.hasOwnProerty(this[i]) cause "i" is just index isnot it ?

    • @swayamprakashsahoo9002
      @swayamprakashsahoo9002 11 месяцев назад

      this.hasOwnProperty(this[I])

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

      @@Error404Codes this.hasOwnProperty(i) is the correct way, because incase of array, hasOwnProperty() method checks whether an index exists or not.

  • @deepeshji
    @deepeshji Год назад +8

    Second implementation was unexpected, thank you sir for making it on more advanced level.

    • @chaiaurcode
      @chaiaurcode  Год назад +8

      Glad you liked it!

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

      This JavaScript code is extending the built-in Array.prototype with a new method called forEachTwo. This custom forEachTwo method is designed to iterate over an array and apply a provided callback function to each element. Let's break down the code step by step:
      Extending Array Prototype:
      The code begins by defining a new method called forEachTwo on the Array.prototype. This means that once this code is executed, all arrays in the JavaScript environment will have access to the forEachTwo method.
      Function Parameters:
      The forEachTwo method takes two parameters:
      callback: This is the function that will be called for each element in the array.
      thiscontext: An optional parameter that represents the context in which the callback function should be executed.
      Callback Function Check:
      The code starts by checking if the provided callback is a function. If it's not a function, an error is thrown with the message "Not a function."
      Getting Array Length:
      The length of the array on which the method is called is stored in the length variable.
      Looping through Array Elements:
      The code uses a while loop to iterate over the array's elements. The loop continues as long as the index i is less than the array's length.
      Checking Property Ownership:
      Inside the loop, there's a condition if (this.hasOwnProperty). However, this condition is incomplete and should actually be checking if the array instance itself (this) has a property corresponding to the current index i. The correct condition should be if (this.hasOwnProperty(i)).
      Executing the Callback:
      If the array has the property at the current index, the callback function is executed using the call method. The call method allows you to invoke a function in a specific context (thiscontext) and pass arguments to it. The callback is invoked with three arguments:
      The current array element (this[i])
      The index of the current element (i)
      The entire array (this)
      Incrementing the Index:
      After executing the callback (if applicable), the index i is incremented by one.
      The purpose of this code is to create a custom version of the forEach method for arrays, which can handle a specified execution context for the callback function. However, there is a minor issue in the code related to the if (this.hasOwnProperty) condition that needs to be corrected for the code to work as intended.

  • @ck0024
    @ck0024 Год назад +4

    This JavaScript code is extending the built-in Array.prototype with a new method called forEachTwo. This custom forEachTwo method is designed to iterate over an array and apply a provided callback function to each element. Let's break down the code step by step:
    *Extending Array Prototype:*
    The code begins by defining a new method called forEachTwo on the Array.prototype. This means that once this code is executed, all arrays in the JavaScript environment will have access to the forEachTwo method.
    *Function Parameters:*
    The forEachTwo method takes two parameters:
    *callback:* This is the function that will be called for each element in the array.
    thiscontext: An optional parameter that represents the context in which the callback function should be executed.
    *Callback Function Check:*
    The code starts by checking if the provided callback is a function. If it's not a function, an error is thrown with the message "Not a function."
    *Getting Array Length:*
    The length of the array on which the method is called is stored in the length variable.
    *Looping through Array Elements:*
    The code uses a while loop to iterate over the array's elements. The loop continues as long as the index i is less than the array's length.
    *Checking Property Ownership:*
    Inside the loop, there's a condition if (this.hasOwnProperty). However, this condition is incomplete and should actually be checking if the array instance itself (this) has a property corresponding to the current index i. The correct condition should be if (this.hasOwnProperty(i)).
    *Executing the Callback:*
    If the array has the property at the current index, the callback function is executed using the call method. The call method allows you to invoke a function in a specific context (thiscontext) and pass arguments to it. The callback is invoked with three arguments:
    The current array element (this[i])
    The index of the current element (i)
    The entire array (this)
    Incrementing the Index:
    After executing the callback (if applicable), the index i is incremented by one.
    The purpose of this code is to create a custom version of the forEach method for arrays, which can handle a specified execution context for the callback function. However, there is a minor issue in the code related to the if (this.hasOwnProperty) condition that needs to be corrected for the code to work as intended.

  • @madhulikablore
    @madhulikablore 5 месяцев назад

    This video will compel you to think about how things work internally. Undoubtedly, this gives a better understanding. Thank you. I ended up creating custom map, filter, reduce methods :)

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

    you are changing the way we code or the way we learn things
    great efforts by you
    we appreciate your effort
    always be happy sir
    ap hamesha salamat rahay

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

    in depth explanation king
    big respect KIng

  • @husnainali2754
    @husnainali2754 Год назад

    Sir You're making a huge impact . Universe of JavaScript. Thank You Sir

  • @abhishekkashyap2984
    @abhishekkashyap2984 Год назад +11

    Sir , please try to upload next react video soon..🙏

    • @chaiaurcode
      @chaiaurcode  Год назад +27

      Aaj hi evening me kr dete h ji

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

      @@chaiaurcode one of the best teacher and humble person , i have encountered in my life!!..🙏❤️

    • @sabirkhan-ln8lb
      @sabirkhan-ln8lb Год назад +1

      ​@@chaiaurcodeyes sir roz upload kiya karo

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

      ​@chaiaurcode Thank you very much sir❤❤

    • @VikalpDailyVlogs
      @VikalpDailyVlogs Год назад

      Why you didn't get me earlier 😮 what a teacher you are

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

    I wanted to express my heartfelt appreciation for your exceptional dedication in providing interview-focused content that delves deep into JavaScript concepts. Your effort in preparing us for real-world challenges is truly commendable, and I'm incredibly grateful for the valuable insights you've imparted to help us succeed in our future endeavors.

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

    Sir just a small request,
    aap batate ho ki, interview me question pucha karo,
    question puchne se interview intrective rahta hai, interview acha jata hai,
    Can you please video on that?
    Kaise technical discussion karna hai?
    kaise question kar sakte hai?
    question karne ke time par kya kya chize dhyan me rakhni chahiye?
    Aap ki communication skills badhiya hai sirji 🔥

  • @akashthoriya
    @akashthoriya Год назад

    Great series sir, learning a lot.
    Concept pura refresh and ache se clear ho jata hai.

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

    loved it hitesh sir ❤aur videos chiye jaldi jaldi

  • @vijayroy7634
    @vijayroy7634 8 месяцев назад

    wow Sir amazing❤. I just learn portotype from this video alone.

  • @snehasish-bhuin
    @snehasish-bhuin Год назад

    mind blowing implementation of call with context

  • @NeerajKumar-kl6xf
    @NeerajKumar-kl6xf 5 месяцев назад

    .........Wow😮, what a video.
    Please make more such sensible and thinkable videos

  • @mostlyinfinite
    @mostlyinfinite Год назад

    Started loving your content and loving your way of talking even more

  • @dynamiczonex
    @dynamiczonex 11 месяцев назад

    please continue this series
    this series are very helpful for inteview

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

    🚀Inside the loop, there's a condition if (this.hasOwnProperty). However, this condition is incomplete and should actually be checking if the array instance itself (this) has a property corresponding to the current index i. The correct condition should be if (this.hasOwnProperty(i)).

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

      Ohh yes, absolutely correct. I missed it while explaining.

  • @Jonathan.106
    @Jonathan.106 Год назад

    Thanks for the Interview series Sir , Much gratitude

  • @user-gi7vy1gb1n
    @user-gi7vy1gb1n Год назад +3

    Kaafi OP channel hai aapka!

  • @bhavinpatel9285
    @bhavinpatel9285 10 месяцев назад

    dhanyavad guruji

  • @stmathematics7895
    @stmathematics7895 Год назад

    thanku so much sir for this amazing series😊😊

  • @nikhatansari3310
    @nikhatansari3310 10 месяцев назад

    Thank you so much sir 😊

  • @mriduakaxxehacker
    @mriduakaxxehacker Год назад

    This series is amazing but React js series is OP ✅

  • @anirudhcodes
    @anirudhcodes Год назад

    Awesome Javascript questions!! 👏👏

  • @abbykrm
    @abbykrm Год назад

    Thank you sir.... Very helpful ❤❤

  • @ankushladani496
    @ankushladani496 Год назад

    Dhanyawad Guruji...❤

  • @HDC00
    @HDC00 Год назад

    wow !!!

  • @user-kp1zd7on1d
    @user-kp1zd7on1d Год назад

    sir ye topic map() method -pe bhi banao aur forEach and isko compare krke batao behind the scenes

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

    sir ek requeest hay ap sy
    kindly JAMSTACK vs MERNSTACK py ek video bana dijiye

  • @akhandpratapsinghhbtu9622
    @akhandpratapsinghhbtu9622 11 месяцев назад

    ❤❤❤❤❤❤

  • @web_mechanics
    @web_mechanics 10 месяцев назад

    Amezing

  • @VinayakPhal
    @VinayakPhal 9 месяцев назад

    @chaiaurcode, but I really don't understand why need a `hasOwnProperty` check, when I am iterating using `for` loop, I tried by adding custom properties on Array prototypes, it doesn't come in the picture, also we don't need if we use `for-of` loop. I understand we need it in `for-in` loop, but rarely anyone uses `for-in` to iterate over an array. I may be wrong, can you please correct me on this sir.

    • @VinayakPhal
      @VinayakPhal 9 месяцев назад

      Also, I remember you've mentioned `hasOwnProperty` is very expensive operation, in the scenario of holey arrays.

  • @VishalSharma-rn7mt
    @VishalSharma-rn7mt Год назад

    Pranam Gurudev 🙏❤️

  • @akhandpratapsinghhbtu9622
    @akhandpratapsinghhbtu9622 11 месяцев назад

    ❤❤❤❤

  • @umargulzar2982
    @umargulzar2982 Год назад

    thiscontext is undefined ??
    How will we call this function forEachTwo ??
    I'm little confused ?

  • @rendevous9253
    @rendevous9253 8 месяцев назад

    best videos

  • @kumar7163
    @kumar7163 Год назад

    Thanks

  • @WaqasKhan-xy9nq
    @WaqasKhan-xy9nq Год назад

    love form the core of heart❤❤

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

    waiting for next react lecture videos

  • @chiraglimbasiya3935
    @chiraglimbasiya3935 Год назад

    Very Informative 👍

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

    Sir non cs walo ko job developer job milna muskil hai kya

  • @Multi-Face
    @Multi-Face Год назад

    sir please make more videos on fetch, promises, async/await their relations and usecases on real life and combine projects plz.......

  • @HaiderAli-sz9hk
    @HaiderAli-sz9hk Год назад

    Sir please continue this series

  • @Tech_Life_2.0
    @Tech_Life_2.0 Год назад

    🎉🎉🎉 thanks sir

  • @rendevous9253
    @rendevous9253 8 месяцев назад

    interesting

  • @VishalSharma-rn7mt
    @VishalSharma-rn7mt Год назад

    Awesome

  • @umargulzar2982
    @umargulzar2982 Год назад

    Mubarak Ho Sir India ne chand par land kia hai ❤❤

  • @greeneryman2699
    @greeneryman2699 Год назад

    Let's go

  • @ankushladani496
    @ankushladani496 Год назад

    Samaj gaye Guruji...❤

  • @tanishkagupta6864
    @tanishkagupta6864 11 месяцев назад

    Sir please javascript ke or interview questions bhi lao

  • @music11953
    @music11953 Год назад

    Great video sir ❤

  • @user-dy5vo7ov4j
    @user-dy5vo7ov4j Год назад

    Nice video sir.

  • @buntysingh244
    @buntysingh244 Год назад

    Han ji

  • @ofcljaved
    @ofcljaved Год назад

    I found it confusing that inside the while loop the condition used in `if` block is this.hasOwnProperty which i believe is a method not a property or am i mistaken?

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

      You are correct. Slight mistake from my side

  • @patelronak153
    @patelronak153 Год назад

    Amazing ❤

  • @endgame3842
    @endgame3842 Год назад

    Writing for react😊😊

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

    sir kindly explain me this
    const ubit={
    name:"akram",
    seat_no:"eb21103066",
    session: askSession=()=>{
    console.log(`my session is ${this.session}. am i a good student?:${this.isgood}`);
    }
    }
    const my=Object.create(ubit);
    my.session="evening";
    my.isgood="true";
    my.askSession();
    console.log(ubit);
    why i can not use arrow function in an object for acacessing current context using this.

  • @TorjumanRimon
    @TorjumanRimon Год назад

    Sir, I had articles on custom forEach, map, filter at hashnode last year

  • @shivamkhandelwal2019
    @shivamkhandelwal2019 9 месяцев назад

    sir event loop bhi bata do plz

  • @baba-sw9do
    @baba-sw9do Год назад

    🙏🙏

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

    hum aap ko 500 likes den gay or aap hum ko 5 LPA ka package legwa dena , Kuch oor esay hi video ke sahyetaa se !!
    :)

    • @chaiaurcode
      @chaiaurcode  Год назад +4

      JavaScript Interviews ke liye best resource hoga ye

  • @rashi1180_
    @rashi1180_ Год назад

    sir java script ka project ka ak playlist bna dijiye please

  • @surendrababu9962
    @surendrababu9962 Год назад

    hey bro this playlist available in English ?

    • @chaiaurcode
      @chaiaurcode  Год назад

      Not yet, but will be available soon

  • @rendevous9253
    @rendevous9253 8 месяцев назад

    prototype?

  • @MiniMoviePlay
    @MiniMoviePlay Год назад

    ara ma na ap ki video next js ki freecodecamp pr dekhi thi ap hindi ma nextjs ki playlist bnai

  • @CodingRaja
    @CodingRaja Год назад

    What is thiscontext

  • @greeneryman2699
    @greeneryman2699 Год назад

    Ok

  • @saifalivoice1777
    @saifalivoice1777 Год назад

    salam ritesh bai.....typescript urdu mein bna dein....
    from pakistan

  • @rashi1180_
    @rashi1180_ Год назад

    sir java script ka project ka ak playlist bna dijiye jisme javascript ka only project ho please pl please

  • @aijazahmad375
    @aijazahmad375 11 месяцев назад

    500 likes

  • @rendevous9253
    @rendevous9253 8 месяцев назад

    100 comments

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

    first like

  • @sandeepsharma-tw6ux
    @sandeepsharma-tw6ux Год назад +2

    First view 😊

  • @sureshsabbiti3018
    @sureshsabbiti3018 11 месяцев назад

    i am not watching this video because you are using hindi instead of english

  • @tech_channel110
    @tech_channel110 Год назад

    "use strict";
    const freezeObj={
    name:"akram",
    password:2334,
    };
    Object.freeze(freezeObj);
    freezeObj.name="jawad";
    console.log(freezeObj.name);
    sir why it is not giving me typerror

  • @Edufact-qb1lo
    @Edufact-qb1lo Месяц назад +1

    *Sir Rate this code*
    Array.prototype.myForEach = function(callback) {
    if (this == null) {
    throw new TypeError('Array.prototype.myForEach called on null or undefined');
    }
    if (typeof callback !== 'function') {
    throw new TypeError(callback + ' is not a function');
    }
    // Convert this (which should be an array) into an object to work with it
    let Obj = Object(this);
    // Get the length of the array
    let len = Obj.length >>> 0;
    for (let i = 0; i < len; i++) {
    if (i in Obj) {
    callback(Obj[i], i, O);
    }
    }
    };

  • @AsAOptionsTrader
    @AsAOptionsTrader 11 месяцев назад

  • @akhandpratapsinghhbtu9622
    @akhandpratapsinghhbtu9622 11 месяцев назад

    ❤❤❤❤