Ad

Use split method to make the input string an iterable for the forEach method. Concatenate each character to the start of the result string.

If each refactoring is supposed to get shorter than woopsie.

Code
Diff
  • // 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;
    }
    
    console.log(reverseStr('hello world'));
    • // const reverseStr = str => [...str].reverse().join(''); <-- weak smh
    • function reverseStr(str)
    • {
    • let stringArr;
    • let joined;
    • stringArr = str.split('');
    • stringArr.reverse();
    • joined = stringArr.join('');
    • return joined;
    • let reversedstr = '';
    • stringArr = str.split('');
    • stringArr.forEach(function(char, index) {
    • reversedstr = char + reversedstr;
    • })
    • return reversedstr;
    • }
    • console.log(reverseStr('hello world'));