Help with functional tab deletion

Hi - I’m working on this current exercise, and I modified the stub problem to do a for loop instead of the slice function, and it works a bit. I saw the map function may work well, but I just need some help with it. If anyone has some suggestions, that would be great. Here’s what I put in, but I’m still working on it.


// When you close a tab
Window.prototype.tabClose = function (index) {

  for (let i = 0; i < index; i++) {
    var tabsBeforeIndex = this.tabs.splice(i);
    console.log (tabsBeforeIndex)
  }
  for (let i = index+1 ; i > index; i--){
    var tabsAfterIndex = this.tabs.splice(i);
    console.log (tabsAfterIndex)
  }

Here’s the challenge. https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/understand-the-hazards-of-using-imperative-code/

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

1 Like

Hi!

There is more than one way to solve the problem, but in Your case, You’re still using a function that modifies the contents of the array (which is the initial problem). To solve it, You must understand what splice is doing and compare it to slice.

If You still have problems, try to imagine what the values for the array are after each iteration.

Hint:

The shortest way (I’ve found) to solve this problem is by removing 2 characters and adding 2 more (all of this under the tabClose function).

Thanks. I actually got it to work. I added an if statement and if the index > 1, I set the tabAfterIndex first, and then the tabsBeforeIndex. If the index was anything else, I just used the existing statements. It seems to work well at least for the exercise. I may have to try to test this method later.

Here’s the actual code I used just in case it may help anyone else. I don’t want to overshare anything, I just think people can learn sometimes from working on it, and then seeing at least one possible answer.

   if (index > 1) {
    var tabsAfterIndex = this.tabs.splice(index+1); // get the tabs after the tab
    var tabsBeforeIndex = this.tabs.splice(0, index); // get the tabs before the tab
 
  } else {  
    var tabsBeforeIndex = this.tabs.splice(0, index); // get the tabs before the 
    var tabsAfterIndex = this.tabs.splice(index); // get the tabs after the tab
  };

Since the line:

var tabsBeforeIndex = this.tabs.splice(0, index); // get the tabs before the tab

is duplicated in the else statement code block, you can simply move it to be a single line to be either before or after the if/else statement.

Hi - I played around with it, and got this version to work.

   if (index > 1) {
    var tabsAfterIndex = this.tabs.splice(index+1); // get the tabs after the tab
  };
   var tabsBeforeIndex = this.tabs.splice(0, index); // get the tabs before the tab
   if (index <= 1) {
     var tabsAfterIndex = this.tabs.splice(index); // get the tabs after the tab
  };