Got tired to solve reserve function using recursion

I want to write a function that returns a string using recursion.
Given the string “freecodecamp”, the program should return “pmacedoceerf”.
I wrote my function but I get tired to return base case.
Here is my code so far :slight_smile:

function lastToOne(str) {
  if (str === str[0]) { // base case
    return str[0];
  } else { // recursion case
    lastToOne(str - str[str.length - 1]);

so what are my mistakes in my function.

you can’t use - between strings, you may want to look in something like slice or substring

also you are not using the value returned from the function, you need to have a return statement in both cases

Ok, But what condition should I write at the base case. And what I should return in that return statement Or is my base case code is right?

your base case can be right, it depends on what you do with the other one

what do you want each step of your recursion to do?

I want function that reverses a string using recursion.
For example, Given the string “freeCodeCamp”, the program should return “pmaCedoCeerf”.

Hey @abdulrahman.mhd.anas, a very important aspect to remember is that in Javascript functions implicitly returns “undefined”, unless otherwise specified.

This is vital because recursions need to compute based on the returned value of the previous function in the call stack.

Or in other words, are you sure you are returning something from your function?
Hope it helps.

Thank you , could help me solve this challenge
I want function that reverses a string using recursion.
For example, Given the string “freeCodeCamp”, the program should return “pmaCedoCeerf” .

and what each step of the function should do?

like, your base case you are saying, str === str[0], so it should be one letter long. which letter? if the starting string is "freeCodeCamp"?

you know what is the general behaviour of your function, but what should it do step by step?

For example, If I call my function console.log(lastToOne(“car”)); ,
The output shout return ”rac”
The function should make the last letter of the word is the first letter and the second-to-last-number is the second letter and so on. Like the example above.

yes, that’s what the function output should be. But what about the middle steps?

For example, this function is called, some recursion cycles happen, and there is a point where str === str[0] is true. What is the value of str at that point? there are two possibilities, which one you want it to be?

If the str Is only one letter like “a” , ”b” and so on.
Do you there are too many ways using recursion to solve this challenge?

but which one?

the input is “car”, so which letter?

No, if only I call function like that lastToOne(“a”);

Thank you @ilenia for your helping. I appreciate you.
I’ll try to solve the challenge. When the function works, I will reply to you the final function so you can understand what I am was meaning.

recursion is calling a function inside itself, with a different argument.

for what you are doing it seems you want to call the function again with a substring of the outer argument

how would you do this algorithm step by step with pen and paper?