I believe there are some discrepancies in the explanations. I don’t want to think that. Was hoping for a better conclusion. Pls let me know if I am getting this wrong.
Solution 1:
var tabsAfterIndex = this.tabs.splice(index);
should be replaced with
var tabsAfterIndex = this.tabs.splice(1);
The original code here is this
var tabsAfterIndex = this.tabs.splice(index + 1);// {discrepancy)
As I write this the problem is solved simply by removing the “P” from splice.
l
var tabsBeforeIndex = this.tabs.splice(0, index);
does NOT return the tabs before. It returns the closed tab followed by the “after” elements. I don’t see “imperative/splice() is hazardous…creates side effects”…the code is written for slice and they wrote splice instead (discrepancy because the actual arguments are incorrect-they work for slice). After passing the challenge I decided to experiment…
var tabsOmittingIndex = this.tabs.splice(index, 1);
this.tabs = tabsOmittingIndex; // no reason to join, right?
// Only change code above this line
console.log(this);// logs { tabs: [ 'Vimeo' ] }
TypeError: Cannot read property 'tabs' of undefined
TOTALLY unanticipated. I’m returning the “cut-out”. the donut hole just like the solution was advocating. Maybe this is because I am defining a new var? Maybe this is what they are talking about? SPLICE acting like SLICE?
If, for example,
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.splice(0, 2);
document.getElementById("demo").innerHTML = fruits;
}
</script>// returns Apple,Mango
My guess is that we are assigning the removed portion to the new var then creating another “after” var to remove the closed tab with
var tabsAfterIndex = this.tabs.splice(1);
Window.prototype.tabClose = function (index) {
// Only change code below this line
var tabsOmittingIndex = this.tabs.splice(index, 1);
this.tabs = tabsOmittingIndex; // already Joined
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
.
Challenge: Understand the Hazards of Using Imperative Code
Link to the challenge: