PDA

View Full Version : Help creating a simple error checking function


Krucialist
05-14-2008, 01:45 AM
I am attempting to create a function that will simply check the elements of an array and make sure there are no negative numbers inside the array.

Ideally, I was trying to design this in a manner that would replace any negative numbers that were found with a zero. Im not even sure if that is possible, I just kinda assumed it was. If I couldnt do that then I wanted to at least send an alert that would notify a user that a negative value was found.

Let me know if this isnt a clear enough explanation of what I am trying to do.

Any help with this would be greatly appreciated.

mrhoo
05-14-2008, 02:36 AM
Array.prototype.thinkPositive= function(){
var L= this.length,tem;
while(L){
tem= parseFloat(this[--L]+'') || 0;
this[L]= Math.max(tem,0);
}
return this;
}


var A=[1,.23,'7','-3',2.45,-3,'spot','b54',false,6,0,5,null,11,-1.1,7];
alert(A.thinkPositive())

/* returned value:
(Array- all items are typeof 'number')
[1,0.23,7,0,2.45,0,0,0,0,6,0,5,0,11,0,7]
*/