Is my solution wrong? Kinda misunderstood

Working on Understand the Hazards of Using Imperative Code lesson and i kinda misunderstrood, why in Window.prototype.tabClose = function(index)

we can’t just use:


With this one we just close 1 tab starting from index. Why should we use 2 additional variables tabsAfterIndex and tabsBeforeIndex ?

Hey there.
I assume you’re talking about this.

// 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

You’re actually supposed to change this code :smile: see the comments :sweat_smile:
// Only change code below this line
// Only change code above this line

Yea, but there is 2 solutions in hints, and they are different from mine :smiley:

here is 2 screenshot of them Screenshot by Lightshot and Screenshot by Lightshot

I mean, why we need to use additional variables at all?

I have no idea. The author probably didn’t think about that solution.
Your solution is correct. In fact, it’s the one i used for that specific assignment as well. Didn’t seem to cause any problems for me.
I assume, the author’s intention was for students to use slice, instead of splice, as to not modify the original array, but that’s just my guess.

Well, thanks. i was just wondering maybe my solution passed that unit test but it is wrongg :smile:

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