arrays - Biggest index with a value in a for loop javascript -


i have severals arrays, , find biggest index value.

var test1 = [0, 0, 200, 0, 0, 0]; var test2 = [0, 400, 200, 0, 250, 0]; var test3 = [240, 0, 0, 0, 0, 0]; var test4 = [240, 0, 0, 0, 100, 0]; 

below differents arrays, , each one, retrieve this

test1 : 2, 200 test2 : 4, 250 test3 : 0, 240 test4 : 4, 100 

i tried break when value different of zero, , not 1 biggest index value.

function seekandfind(array) {     (var = 0; < array.length; i++) {                 if ( array[i] !== 0 ) {             console.log(array[i]);              break;         }     } } 

fiddle : http://jsfiddle.net/jeremdsgn/p4xld/

why not go backwards through array?

function seekandfind(array) {     (var = array.length - 1; >= 0; i--) {                 if ( array[i] !== 0 ) {             console.log(i, array[i]);              break;         }     } } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -