new class js -advanced
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
function reverseString(str) {
|
||||
// Base case: if the string has only one character, return it
|
||||
if (str.length <= 1) {
|
||||
return str;
|
||||
}
|
||||
// Recursive case: return the last character of the string and call the function with the rest of the string
|
||||
return reverseString(str.slice(1)) + str[0];
|
||||
}
|
||||
|
||||
// Test cases
|
||||
console.log(reverseString('hello')); // => "olleh"
|
||||
console.log(reverseString('Recursion')); // => "noisruceR"
|
||||
console.log(reverseString('')); // => ""
|
||||
console.log(reverseString('A')); // => "A"
|
||||
Reference in New Issue
Block a user