I have this piece of code where I delete the first (or last) element from an array, according to a boolean variable. I do like this:
{...
console.log("length before " + waypoints.length)
waypoints = this.deleteWaypoint(waypoints)
console.log("length after " + waypoints.length)
...}
deleteWaypoint(waypoints){
if (this.first){
this.first = false;
return waypoints.shift()
} else {
this.first = true;
return waypoints.pop()
}
}
The first log prints that waypoints has a certain length, then I call the method to delete elements and when the second log prints is length after undefined. "First" is a global variable initialized to true.
Why is that?
Array.prototype.shift: "Theshift()method removes the first element from an array and returns that removed element."return waypointsindeleteWaypoint