After once again reaching a point that I couldn’t complete questions on my own, I went back to redo the past sections. Now I’m at the functional programming section having a tough time solving this. The first time around I used slice because it didn’t change the code, but looking back I didn’t truly understand to solve it and that was the easy way out.
I see socialWindow is showing correct and workWindow and socialWindow are having issues, but I’m not sure how to remove 'Vimeo' in in the middle of the array and to keep 'Vine' by tweaking the slice function. Same goes for 'Work mail
The functions calling on each other may be my problem. I don’t know. .join(workWindow.tabClose(1).tabOpen()) is tough for me to wrap my head around. I don’t know why.
**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 AKA slice(start, end before this position)
var tabsBeforeIndex = this.tabs.splice(0, index); // Get the tabs before the tab
var 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 to add "Vine" and "Work mail" AKA videoWindow and workWindow
return this;
};
// Let's create three browser windows
var workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']); // should be 'GMail', 'Work mail', 'Docs', 'freeCodeCamp', 'new tab'
var socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']); //
var videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']); // should be 'Netflix', 'YouTube', 'Vine'
// 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
.join(workWindow.tabClose(1).tabOpen());
console.log(finalTabs.tabs);
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 Safari/537.36.
Challenge: Understand the Hazards of Using Imperative Code
Link to the challenge: