Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Aldhanekaa/fec6d4cf3582b297176f86c474447643 to your computer and use it in GitHub Desktop.
Save Aldhanekaa/fec6d4cf3582b297176f86c474447643 to your computer and use it in GitHub Desktop.
Functional Programming: Understand the Hazards of Using Imperative Code : Using my solution
// 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) {
console.log(this.tabs)
this.tabs = this.tabs.concat(otherWindow.tabs);
return this;
};
// When you open a new tab at the end
Window.prototype.tabOpen = function () {
this.tabs.push('new tab'); // Let's open a new tab for now
// console.log(this.tabs)
return this;
};
// When you close a tab
Window.prototype.tabClose = function (index) {
// Only change code below this line
// console.log(this.tabs)
var tabsBeforeIndex = this.tabs.splice(index, 1); // Get the tabs before the tab
// console.log(this.tabs)
// console.log(this.tabs)
// this.tabs = this.tabs;
// Only change code above this line
console.log(this)
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
// .join(workWindow.tabClose(1).tabOpen());
console.log(finalTabs.tabs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment