Ad
Fundamentals
Strings

using recursion

Code
Diff
  • def reverse_string(n):
        if n == '': 
            return ''
        
        return reverse_string(n[1:]) + n[0]
    
    print(reverse_string('hello world'))
        
    • def reverse_string(n):
    • #return n[::-1]
    • return ''.join(reversed(n))
    • if n == '':
    • return ''
    • return reverse_string(n[1:]) + n[0]
    • print(reverse_string('hello world'))

using recursion

Code
Diff
  • // const reverseStr = str => [...str].reverse().join('');   <-- weak smh
    
    function reverseStr(str) {
      if (!str) return '';
      
      return reverseStr(str.slice(1)) + str.charAt(0);
    }
    
    console.log(reverseStr('hello world'));
    • // const reverseStr = str => [...str].reverse().join(''); <-- weak smh
    • function reverseStr(str)
    • {
    • let reversedstr = '';
    • stringArr = str.split('');
    • stringArr.forEach(function(char, index) {
    • reversedstr = char + reversedstr;
    • })
    • return reversedstr;
    • function reverseStr(str) {
    • if (!str) return '';
    • return reverseStr(str.slice(1)) + str.charAt(0);
    • }
    • console.log(reverseStr('hello world'));