Can do betterBrokenclock December 6, 2011 at 7:21 PM
Well, your code does not work if you have an indexed array where some index are missing...
For exemple, with the array {4: "Cat",5: "Dog",7: "Lion"} your fonction will never return the actual position of the seached item but only its index. You should better use the for...in statement like this :
function getPosition(arrayName,arrayItem){
var i=0;
for (item in arrayName){
if (item==arrayItem) return i;
i++;
}
}
Well, this function return 0 for the first position, as usual.
Can do betterBrokenclock December 6, 2011 at 7:21 PM
Well, your code does not work if you have an indexed array where some index are missing... For exemple, with the array {4: "Cat",5: "Dog",7: "Lion"} your fonction will never return the actual position of the seached item but only its index. You should better use the for...in statement like this : function getPosition(arrayName,arrayItem){ var i=0; for (item in arrayName){ if (item==arrayItem) return i; i++; } } Well, this function return 0 for the first position, as usual.
Post your Comment