Found a couple problems with sum():
First off, instanceof works with *objects* not primitives, and so you
must do:
var a:Array = new Array(new Number(2),new Number(3),new Number(54),new
Number(6),new Number(7),new Number(8),new Number(9));
instead of say:
var b:Array = new Array(2,3,54,6,7,8,9);
So you *have* to check with typeof for primitives, then instanceof for
objects. Horrible, I know.
Additionally, it would help to have a return statement :D.
Array.prototype.sum = function() {
var x = 0;
var a = this.length;
while (--a -(-1)) {
var num = this[a];
if (typeof(num) == "number" || num instanceof Number) {
x += num;
} else {
return undefined;
}
}
return x;
};
//Here are the test cases i wrote to verify functionality
var a:Array = new Array(new Number(2),new Number(3),new Number(54),new
Number(6),new Number(7),new Number(8),new Number(9));
var b:Array = new Array(2,3,54,6,7,8,9);
var c:Array = new Array(2,new Number(3),new Number(54), 6,7,8,9);
var d:Array = new Array("2",3,4,new Number(54),6,7,8,9); //should return
undefined
trace(a.sum());
trace(b.sum());
trace(c.sum());
trace(d.sum());
Lastly, there were a couple places where you did not use 'var' and the
compiler caught it when I encapsulated your AS 1 into a class. I'll post
if you like.
Peace
C
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com