# JavaScript If index is out of bounds of array, set index to closest boundary index array bounds matlab,index outside bounds array c#,index

Let's say we have an array that can have a length of 1 to ??

We have a way of navigating this array up or down by clicking a button that either navigates upward or downward, which is displayed in the DOM (think pagination).

We would want our navigation to stop when either we are out of the lower bound, or stop when our navigation is out of the upper bound.

When we reach the lower-bound we would know this by the value -1

When we reach the upper-bound we would know this by the value ( > array.length - 1 ).

Our function accepts the parameter index as shown

``````function goToChapter( index ){
// compute value of index to within bounds.
return array[ index ];
}
``````

Let's say all we want to do is use the computed within bounds index to get a value from the returned array.

Examples

``````var array ["chapZero", "chapOne", "chapTwo", "chapThree"];

goToChapter( 2 );
=> "chapTwo;

goToChapter( 4 );
=> "chapThree"; // because four is out of upper bound so index became three.

goToChapter( -1 );
=> "chapZero"; // because -1 is our of lower bound so index became zero.
``````

I realize this can be done with a few if statements, I am looking for a way to use some sort of math formula that perhaps

1. converts any negative number to 0;
2. leaves any positive number alone;
3. uses the length of the array to determine when to return the upper limit.

UPDATE

I'm going with Andand's answer. I've added enough to copy paste the solution into the console if you find this helpful and want to test it out.

``````var array = [0,1,2,3,4];

function goToChapter( index, array ){
return array[(Math.min( array.length - 1, Math.max( 0, index )))];
}

goToChapter( -1, array ); => 0;

goToChapter( 3, array ); => 3;

goToChapter( 8, array ); => 4;
``````

Thanks Andand.

NOTE:

I also found this helpful, but not as fast.

``````function setWithinArrayBounds( index, array ){
return !!index ? 0 : index > ( array.length - 1 ) ? ( array.length - 1 ) : index;
}
``````
Answer:1

## How do you load a new html page when select an item from a <select> html element

I currently have a button for each language to allow users to view website in difference languages <a href="http://www.jthink.net/songkong/en" title='English'><img src="http://static....

I currently have a button for each language to allow users to view website in difference languages <a href="http://www.jthink.net/songkong/en" title='English'><img src="http://static....

1. load html page in div
2. load html page from javascript
3. load html page on button click
4. load html page into another html page
5. load html page in div using jquery
6. load html page into div onclick
7. load html page using jquery
8. load html page using javascript
9. load html page from assets android
10. load html page in php
11. load html page in webview android
12. load html page in node js
13. load html page in iframe
14. load html page in react
15. load html page in another html page
16. load html page in wpf
17. load html page in popup jquery
18. load html page in python
19. load html page in bootstrap modal
20. load html page in div using ajax

## AngularJS display array based off checkbox selection

I have 4 check boxes, and depending on which one/pair is selected I want a specific array to be displayed on the page. I'm relatively new to angularjs so I'm not sure how to get this to work. So how ...

I have 4 check boxes, and depending on which one/pair is selected I want a specific array to be displayed on the page. I'm relatively new to angularjs so I'm not sure how to get this to work. So how ...

1. angularjs display array of objects
2. angularjs display array in html
3. angularjs display array of strings
4. angularjs display array list
5. angularjs display array
6. angularjs display array in table
7. angularjs display array index
8. angularjs display array data
9. angularjs display json array
10. angularjs display count of array
11. angularjs display image byte array
12. angularjs display element of array

## EmberJS: is it possible to save only modified objects?

I have a small and quite standard application that: Fetches objects from the server Renders the objects and allows some editing on them. If it is important, this editing only affects DS.hasMany ...

I have a small and quite standard application that: Fetches objects from the server Renders the objects and allows some editing on them. If it is important, this editing only affects DS.hasMany ...

## List of Timezones in JSON format [closed]

I'm looking for a list of time zones that corresponds to major cities. Like what java.util.TimeZone can provide. I mean, I want to resolve the America/Los_Angeles identifier to UTC-8:00 in javascript. ...

I'm looking for a list of time zones that corresponds to major cities. Like what java.util.TimeZone can provide. I mean, I want to resolve the America/Los_Angeles identifier to UTC-8:00 in javascript. ...

1. timezones list json
1