Hello.
I believe there is a bug in this section of the curriculum:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/understand-the-hazards-of-using-imperative-code
If I understand correctly, the premise of this exercise is to find the piece of code that is throwing the error, and understand why the imperative paradigm in prone to errors.
In the exercise an object is define, and this objects has different methods, one of them being the following:
// 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(index); // get the tabs after the tab
this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // join them together
return this;
};
As described in the comments, tabsBeforeIndex
grabs the values in the array from start (index 0) up to but not including index
. Then tabsAfterIndex
grabs the values after the index until the end of the array.
So for example if tabs = [0,1,2,3,4,5]
and index = 2
then tabsBeforeIndex = [0,1]
and tabsAfterIndex = [3,4,5]
.
I understand that the point of the exercise is to show that using splice modifies the original array (imperative programming modifies the state of the program), and that after using splice in tabsBeforeIndex
the this.tabs
array is different.
But, the example in my opinion should be as following:
// 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(index+1); // +1 is used because the first param in splice is inclusive
this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // join them together
return this;
};
Sorry if am mistaken but I think this is an error.