This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
@@ -18,7 +18,26 @@
|
||||
<script>
|
||||
'use strict';
|
||||
|
||||
const reverseString = (str) => {};
|
||||
// const recurse = (str, nr) => {
|
||||
// if (nr < 0) {
|
||||
// return '';
|
||||
// }
|
||||
|
||||
// return str.charAt(nr) + recurse(str, nr - 1);
|
||||
// };
|
||||
|
||||
// const reverseString = (str) => {
|
||||
// return recurse(str, str.length - 1);
|
||||
// };
|
||||
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user