What's the problem here? (recursion)

It isn’t from the curriculm, the function should round off a number to a given fixed value, i want to solve this problem without for loops (i already solved it like that) and without .toFixed()

function roundOffFloat (num, fixval) {
    num = String(num);
    if (num[0] == ".") {
    return num.slice(0, fixval + 1)
    }
    else return num[0] + roundOffFloat(num.slice(1, num.length), fixval)
}
console.log(roundOffFloat(2.898833894348), 9)

The function seems to work fine (you aren’t really rounding anything per se, but it functions the way I think you expect it to work). That isn’t the issue: look at this carefully, what is wrong with it?

console.log(roundOffFloat(2.898833894348), 9)
1 Like