logo CodeStepByStep logo

recursionMystery5

Language/Type: JavaScript recursion mystery
Author: Melissa Hovik (on 2016/09/25)

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) {
        let rest = s.substr(0, s.length - 1);
        let last = s.substr(s.length - 1, 1);
        return last + mystery5(rest);
    } else {
        let first = s.substr(0, 1);
        let 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

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.