i wanna solve using recursive function.. but can’t solve it

function test3(num) {
    value = value * Number(str[i]);
    let value = 1;
    let str = String(num);
    for (let i = 0; i < str.length; i++) {
        if (str.length <= 1) {
        }
        return test3(value);
    }
    return value;
}

i wanna make single Digits using recursive function. but, the code’s value can’t access.. i m searching in google about recursive fuction, it’s not good answer.. why cant access ‘value’ ?

i wanna make

234,
2* 3* 4
2* 4
8

786,
7 * 8 * 6 -> 336
3 * 3 * 6 -> 54
5 * 4 -> 20
2 * 0 -> 0

like that. i want to know why can’t access ‘value’ in the function. thank you.

Answer

You can’t access variables form the parent function’s scope. If you want to access variables, pass them in as a parameter. Regardless, this is much easier done without strings:

function productOfDigits(num) {
    if (num < 10) {
        return num;
    }
    return (num % 10) * productOfDigits(Math.floor(num / 10));
}

// use this one
function repeatedProductOfDigits(num) {
    if (num < 10) {
        return num;
    }
    // multiply all the digits then try again
    return repeatedProductOfDigits(productOfDigits(num));
}

num % 10 gets the last digit, and Math.floor(num / 10) gets every digit except the last, so productOfDigits(786) == 6 * productOfDigits(78) == 6 * 8 * productOfDigits(7) == 6 * 8 * 7.