Code
Javascript求和
<script language = "javascript" type = "text/javascript">
function Sum(a) {
if ((a instanceof Array) || (a && typeof a == "object" && "length" in a)) {
var total = 0;
for (var i = 0; i < a.length; i++) {
var element = a[i];
if (!element) continue;
if (typeof element == "number") total += element;
else throw new Error("All element must be numbers!");
} return total;
} else throw new Error("Argument must be an array!!");
}
var a = [1, 2, 3];
var sumNumber = Sum(a);
document.write(sumNumber);
</script>
Javascript求和
<script language = "javascript" type = "text/javascript">
function Sum(a) {
if ((a instanceof Array) || (a && typeof a == "object" && "length" in a)) {
var total = 0;
for (var i = 0; i < a.length; i++) {
var element = a[i];
if (!element) continue;
if (typeof element == "number") total += element;
else throw new Error("All element must be numbers!");
} return total;
} else throw new Error("Argument must be an array!!");
}
var a = [1, 2, 3];
var sumNumber = Sum(a);
document.write(sumNumber);
</script>