Understand-the-hazards-of-using-imperative-code

Tell us what’s happening:

Instruction says:
//
Run the code in the editor. It’s using a method that has side effects in the program, causing incorrect output. The final list of open tabs should be [‘FB’, ‘Gitter’, ‘Reddit’, ‘Twitter’, ‘Medium’, ‘Netflix’, ‘YouTube’, ‘Vine’, ‘GMail’, ‘Work mail’, ‘Docs’, ‘freeCodeCamp’, ‘new tab’] but the output will be slightly different.

Work through the code and see if you can figure out the problem, then advance to the next challenge to learn more.
//

But actually right output should be [ ‘FB’, ‘Gitter’, ‘Reddit’, ‘Twitter’, ‘Medium’, ‘new tab’, ‘Netflix’, ‘YouTube’, ‘Vine’, ‘GMail’, ‘Work mail’, ‘Docs’, ‘freeCodeCamp’, ‘new tab’ ]

You’ve missed one new tab (that was opened for cat memes), I guess.

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) {
  var tabsBeforeIndex = this.tabs.slice(0, index); // get the tabs before the tab
  var tabsAfterIndex = this.tabs.slice(index + 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);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:

If you think there is an error in a challenge, please create a detailed GitHub Issue describing it.

I’m so sorry, but I’m a newbie in coding. So I don’t even know how to work with GitHub (have no experience). But I’ll try, thanks =)

You should see that the current Output is slightly different. You should see what happen to the object videoWindow after it runs tabsBeforeIndex (0,2)…

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

alert(finalTabs.tabs);

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Thank you for understanding.

2 Likes

As much as I agree that the answer shouldn’t just be just pasted into there, that example above was, in fact, not even the spoiler. @ArielLeslie I just want to mention it so people aren’t misled by it. :smiley: Please look favorably on my addition :smiley:

What I would like to see, however, is more discussion concerning proper methods on solving this problem. Especially when in the following problem explaining what was happening there. The “true” answer isn’t discussed until towards the end of the section. Just minor feedback.

@peacelovecookies I was stumped on this one for a while, because I wanted to get the “actual” solution. But there’s still a missing piece in your example! (If you are looking to “actually” solve it :wink:) Honestly, imo this one was a bit of an error in the wording of the explanation for what the goal is (it feels like a trick question!), but still a great problem to solve, and felt very happy when I did :smiley: