Need help understanding this challenge

this challenge is a little bit OOP and there are parts that I don’t understand I am trying to understand it line by line

this part here is not clear to me in this method tabClose we have two constants tabsBeforeIndex and tabsAfterIndex I would like to understand what tabs beforeIndex really mean are it going to splice the array from 0 as the first parameter and the second parameter index will delete this amount of items in the array. and what about the second constants what it equal if the first constants started to splice from 0 ?

// When you close a tab
Window.prototype.tabClose = function(index) {

  // Only change code below this line

  const tabsBeforeIndex = this.tabs.splice(0, index); // Get the tabs before the tab
  const tabsAfterIndex = this.tabs.splice(1); // Get the tabs after the tab

  this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // Join them together

  // Only change code above this line

challenge link

Yes, that is what the splice method does, it removes items from an array and returns the items it removed. The first arg is the index in the array to start deleting and the second arg is how many items to delete starting at that index. tabsBeforeIndex will then be an array of the deleted items.

I’m not sure I understand this question. The first splice does start at 0. After the first splice then the first element in this.tabs will be the tab at index because the first splice deleted all of the elements before index but not index itself. The second splice starts at 1 which will delete everything after the first element in this.tabs.

ok so the first constant contains the tabs from the first element to the element has the index equals to index parameter

but what about the second parameter I do not know what it really equals you said it equals the elements from index 1 to the end but I cannot grasp it

if tabsAfterIndex equals tabs.splice(1) so all the next elements will be from index 1 to the end of the array but we already spliced the tabs from 0 to the specified index

how after that tabs equals tabsBeforeIndex and tabsAfterIndex this is complicating

and I could’t find the element that has been removed from the array which is the closed tab

I think the easiest way to understand this is to write it out. Let’s say you have an array with five tab elements:

[tab1, tab2, tab3, tab4, tab5]

And let’s say you want to close the third tab in the array. That tab is at array index 2, so you would call tabClose and pass in the number 2.

The first call to splice removes 2 array entries, beginning at index 0. So after the first splice the array this.tabs will be left as:

[tab3, tab4, tab5]

Does that make sense? Do you see now how the second splice will work?

ok so the splice method doesn’t delete the item which it is index equals to 2

so if I have an array with 100 elements if I used the splice method from 0 and the second parameter is 50 it will not take the element that equals 50

Right. Always go to the docs if in doubt:

“deleteCount: An integer indicating the number of elements in the array to remove from start.”

The number 2 in this case is the number of items to delete. It doesn’t refer to an actual index in the array.

1 Like

ok know I completely understand the first constant
now I have a question does the splice method affects the real array or modify it

for the second constant we started splicing from index 1 so is the first parameter was the element did not been spliced in the previous constant

I hate to be “that guy”, but like I said, if in doubt, go to the docs. After reading the first sentence in the doc, you tell me :slight_smile:

I’m not trying to be a jerk here. This is good practice. You need to get comfortable reading documentation because professional developers spend a lot of time doing this.

Look at my example above. What is the first item in the array after the first splice?

1 Like

Yes it changes the array so the tabs after the tabsAfterIndex parameter will equal the element that it is index will be closed

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.