I'm looking up array values to navigate a slider back and forth and have come accross a hole in my logic regarding undefined values. As it stands, I look up the current slide within the array, add or subtract from the position in the array, and if that value exists update the position. It works, however, if the value within the array doesnt exist (i.e. there are 3 objects in the array and the sliders trying to look up #4) it throws a type error.
Need a better way of saying "if value exists, then update). Seems pretty simple but I havn't thought of a good solution.
I suppose I can easily get rid of the update variable, but I think it's good to look up the value in the array once and save it for use in the body class update, know what I mean?
See code below and thank you!
function slide(direction){
/* Get Current */
var current = $bod.attr('class'),
next,
update;
/* Navigate */
if (direction === 'right'){
next = $current + 1;
} else if (direction === 'left'){
next = $current - 1;
} else if (direction === 'home'){
next = $home;
}
/* Update */
// Issue is here. If the next slide is undefined, everything crashes
update = $slides[next].slide;
// Was trying to address the issue with this line:
if (update){
$bod.removeClass(current).addClass(update);
$current = next;
}
}