An Example

Another example, this time in a web page context. Consider what it does before testing it.

Example 7.1. recLib.js
'use strict'

const suma = function (arr) {
    if (arr.length <= 1)
        return arr[0];
    else {
        console.log(arr[0] + " " + arr);
        return arr[0] + suma(arr.slice(1));
    }
}

const digitSum = function (n) {
    if (n === 0)
        return 0;

    return n % 10 + digitSum(Math.floor(num / 10));
}

Example 7.2. Execution Code
'use strict';

let arr = [];
let input = null;

do {
    if (input === 'wq')
        break;
    if (input == null)
        continue;
    arr.push(Number(input));
} while(input = prompt('Enter numeric array element, "wq" for stop'));

console.log(arr);
console.log(`Sum of elements: ${suma(arr)}`);


Example 7.3. Host HTML5
<!doctype html>
<html>
    <head>
        <title>Recursion #3</title>
        <meta charset='utf-8'/>
        <script src='recLib.js'></script>
    </head>
    <body>
        <h1>Recursion Example 3</h1>
        <script src='executionRec3.js'></script>
    </body>
</html>

Test It!