Don't know where did I go wrong?

Tell us what’s happening:
I just changed this line “var tabsAfterIndex = this.tabs.splice(index)” to
var tabsAfterIndex = this.tabs.splice(index+1, tabs.length-(index+1))

As splice removes from the array, so tabsAfterIndex should be from the index + 1 to the end of the tabs. So, where did I go wrong?

Your code so far

// tabs is an array of titles of each site open within the window
var 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

 var tabsBeforeIndex = this.tabs.splice(0, index); // get the tabs before the tab
 var tabsAfterIndex = this.tabs.splice(index+1, tabs.length-(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
var workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']); // Your mailbox, drive, and other work sites
var socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']); // Social sites
var videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']); //  Entertainment sites

// Now perform the tab opening, closing, and other operations
var finalTabs = socialWindow
                   .tabOpen() // Open a new tab for cat memes
                   .join(videoWindow.tabClose(2)) // Close third tab in video window, and join

Your browser information:

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

Challenge: Understand the Hazards of Using Imperative Code

Link to the challenge:

1 Like

As (array).splice removes from the array

Yes, it removed from the array already.
So, there is no reason to make splice from index again.

You want to skip the third tab, you got the first two in your hand.
To ignore the third tab, use array.splice(1) so you will start from the second tab and get the wanted output.

1 Like

Oh yeah! Silly mistake :sweat_smile:
Thanks a lot for pointing out.

“Examine the code in the editor. It’s using a method that has side effects in the program, causing incorrect behavior.” …

@Divyalok123 I see you’ve solved it. Part of this lesson in my opinion is to teach about the mutation of the Array. It can be solved by simply removing 2 characters in the given code :slight_smile:


NOT Splice:


Oh yes! You are absolutely right. In our previous solution by doing splice(1) we are still mutating the array which is against what this challange is trying to teach us.
Thanks a lot for your solution and for providing links of the related docs!:slightly_smiling_face: