Hello all,
Can someone explain the attached code please..And in particular, how the program execution flows between the different scope blocks.. Thanks
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Recursion</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
//<![CDATA[
function runRecursion() {
var addNumbers = function sumNumbers(numArray,indexVal,resultArray) {
// recursion test
if (indexVal == numArray.length)
return resultArray;
// perform numeric addition
resultArray[0] += Number(numArray[indexVal]);
// perform string concatenation
if (resultArray[1].length > 0) {
resultArray[1] += " and ";
}
resultArray[1] += numArray[indexVal].toString();
// increment index
indexVal++;
// call function again, return results
return sumNumbers(numArray,indexVal,resultArray);
}
// create numeric array, and the result array
var numArray = ['1','35.4','-14','44','0.5'];
var resultArray = new Array(0,''); // necessary for the initial case
// call function
var result = addNumbers(numArray,0, resultArray);
// output
document.writeln(result[0] + "<br />");
document.writeln(result[1]);
}
//]]>
</script>
</head>
<body onload="runRecursion();">