As the title suggests, I was trying to recursively solve a JavaScript problem. An exercise for my internet programming class was to invert any string that was entered in the function, and I saw this as a good opportunity to solve this with recursion. My code:
function reverseStr(str){
str = Array.from(str);
let fliparray = new Array(str.length).fill(0);
let char = str.slice(-1);
fliparray.push(char);
str.pop();
str.join("");
return reverseStr(str);
}
writeln(reverseStr("hello"))
fliparray, which isn't actually getting used, you havestr.join(""), which doesn't reference its result, and you're reusing variables in a confusing way. Create a new variable when you doArray.from(str)so that it doesn't look like you're calling invalid methods on a string.