Here's a tough question which I'm unable to solve.
Write a recursive function that accepts a string and returns its length. Do not use the .length property, loops, or any built-in methods in Javascript to find the solution.
I'm quite close with this solution but I'm not able to find an alternative to the .slice() method. This function currently works but doesn't fit the question's parameters because if uses that method, and the .length property:
let lengthGetter = (str, def) => {
let len = def || 0;
if (str[0] === undefined) return len;
return lengthGetter(str.slice(0, str.length - 1), len + 1);
};
console.log(lengthGetter("hello")) // 5
Even if it involves throwing away my current function, can anyone find a solution to this problem? Keep in mind you can't use any built-in methods or loops, which makes it quite challenging.
str[n]; if that'sundefinedit means you're beyond the end of the string (or before the start).