Imperative code lesson

Hey guys, I completed the challenge but still confused.

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

could anyone explain to me what these code lines mean?
tabsBeforeIndex - > saved tabs before I even opened the window?
tabsAfterIndex → tabs after window is opened?
Ty.
Your code so far


// tabs is an array of titles of each site open within the window
const Window = function(tabs) {
 this.tabs = tabs; // We keep a record of the array inside the object
};

// When you join two windows into one window
Window.prototype.join = function(otherWindow) {
 this.tabs = this.tabs.concat(otherWindow.tabs);
 return this;
};

// When you open a new tab at the end
Window.prototype.tabOpen = function(tab) {
 this.tabs.push('new tab'); // Let's open a new tab for now
 return this;
};

// 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(index + 1); // Get the tabs after the tab

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

 // Only change code above this line

 return this;
};

// Let's create three browser windows
const workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']); // Your mailbox, drive, and other work sites
const socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']); // Social sites
const videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']); // Entertainment sites

// Now perform the tab opening, closing, and other operations
const finalTabs = socialWindow
 .tabOpen() // Open a new tab for cat memes
 .join(videoWindow.tabClose(2)) // Close third tab in video window, and join
 .join(workWindow.tabClose(1).tabOpen());
console.log(finalTabs.tabs);
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36

Challenge: Understand the Hazards of Using Imperative Code

Link to the challenge:

Without looking closely, I assume they are just the tabs before and after the tab that is being closed.

tab 1 - tab 2 - tab 3 (being closed) - tab 4 - tab 5

So tab 1 and 2 is tabsBeforeIndex and tab 4 and 5 is tabsAfterIndex

1 Like

so index is referred to the tab being closed I assume?

I believe so, you can see the calls to tabClose at the bottom.

Alright, I was just confused about the name it helps me understand the code better, to why refer index to a closed tab.