Created
January 14, 2019 15:43
-
-
Save KimTrijnh/a64add25013b76055906446075d64a7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { | |
var tabsBeforeIndex = this.tabs.splice(0, index); // get the tabs before the tab | |
var tabsAfterIndex = this.tabs.splice(1); // get the tabs after the tab | |
this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // join them together | |
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()); | |
alert(finalTabs.tabs); | |
//SPLICE(INDEX,NUM) return a spliced array & change the original array. So tabsAfterIndex should go with 1 instead of index. | |
//Another principle of functional programming is to always declare your dependencies explicitly. | |
//The function is easier to test, you know exactly what input it takes, and it won't depend on anything else in your program. |
INTERMEDIATE JS ALGORITHM
Make Person
var Person = function(firstAndLast) {
// Complete the method below and implement the others similarly
this.getFullName = function() {
return firstAndLast;
};
this.getFirstName = function() {
return firstAndLast.split(' ')[0];
}
this.getLastName = function() {
return firstAndLast.split(' ')[1];
}
this.setFirstName = function(first) {
firstAndLast = first + ' ' + firstAndLast.split(' ')[1];
}
this.setLastName = function(last) {
firstAndLast = firstAndLast.split(' ')[0] + ' ' + last;
}
this.setFullName = function(newName) {
firstAndLast = newName;
}
return firstAndLast;
};
var bob = new Person('Bob Ross');
bob.getFullName();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.