logo Practice-It logo

recursionMystery5

Language/Type: JavaScript recursion mystery
Author: Melissa Medsker-Galloway (on 2016/11/18)

For each call to the following function, indicate what value is returned:

function mystery5(s) {
    if (s.length == 0) {
        return s;
    } else if (s.length % 2 == 0) {
        var rest = s.substr(0, s.length - 1);
        var last = s.substr(s.length - 1, 1);
        return last + mystery5(rest);
    } else {
        var first = s.substr(0, 1);
        var rest = s.substr(1);
        return "(" + first + ")" + mystery5(rest);
    }
}
mystery5("foo");
mystery5("kakuro");
mystery5("koopatroopa");
mystery5("computer");
mystery5("01234");
mystery5("(1 - 2) = -1)");

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.