Functional Programming - Understand the Hazards of Using Imperative Code

Tell us what’s happening:
Can someone give me a hint? Im at a loss what to do here…

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 (Macintosh; Intel Mac OS X 10.15; rv:107.0) Gecko/20100101 Firefox/107.0

Challenge: Functional Programming - Understand the Hazards of Using Imperative Code

Link to the challenge:

you need to focus on these 3 lines of code.
Something is wrong here that you are being asked to fix.
Specifically the point of this exercise is to find the

method that has side effects in the program, causing incorrect behaviour.

You must first understand what is meant by “side effects”.
Usually this term is used to mean when someone modified something they are not supposed to modify.

By my count, in the 3 lines above, there are 2 things being modified.
So take a look, maybe use some console.log statements and see what might be happening that is considered a bad side effect of running these three statements unchanged.

Found it! Went over the lines a couple of times and figured one of the lines looked weird (something to do with the zero indexing) and that did it. Thnx!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.