Reverse the words in a sentence using JavaScript
Write a program to reverse the order of words for a given sentence. For example if we have a sentence like “I love you” it should print “you love I”.
const reverseStr = (str) => { let tmpStr = ''; const result = []; for(let i = (str.length - 1); i >= 0 ; i--) { if(str[i] !== ' ') { tmpStr = str[i] + tmpStr; } else { result.push(tmpStr); tmpStr = ''; } if(i === 0) result.push(tmpStr); } return result.join(' '); }; const input = "Where there is a will there is a way"; console.log(input); console.log(reverseStr(input));