reverse words in sentence javascript code example

Example 1: reverse words in a string javascript

function reverseString(str) {
    return str.split("").reverse().join("");
}
reverseString("hello");

Example 2: reverse words javascript

function reverseInPlace(str) {
  var words = [];
  words = str.match(/\S+/g);
  var result = "";
  for (var i = 0; i < words.length; i++) {
     result += words[i].split('').reverse().join('') + " ";
  }
  return result
}
console.log(reverseInPlace("abd fhe kdj"))

Example 3: reverse a word javascript

function reverseWords(str) {
  const allWords = str.split(" ")
  return allWords.map(item => item.split("").reverse().join("")).join(" ")
}

Tags:

Java Example