JavaScript JavaScript: How would I reverse ONLY the words in a string

How would i write a function that takes one argument that is a sentence and returns a new sentence where all words are reversed but kept in the same order as the original sentence?

Test Case:

wordsReverser("This is fun, hopefully."); Would return:

"sihT si nuf, yllufepoh."

This is what I have so far but notice that I cant get the period and comma to stay in place. I don't know if this was a typo by the interviewer or what?

function wordsReverser(str){
  return str.split(' ').
    map(function(item) {    
        return item.split('').reverse().join('');
    }).join(' ');
}

wordsReverser("This is fun, hopefully.");
//Output: 'sihT si ,nuf .yllufepoh'
Answer:1

Try this way:

function wordsReverser(str){
  return str.replace(/[a-zA-Z]+/gm, function(item) {    
        return item.split('').reverse().join('');
    });
}

wordsReverser("This is fun, hopefully.");
//Output: 'sihT si nuf, yllufepoh.'

How It Works: the replace() function will find each word and pass to the function which will reverse the word (the function returns the reverse word which replaces the word in the string) and all other should remain as that was before.

Answer:2
function wordsReverser(str){
  return str.split(/(\s|,|\.)/).
    map(function(item) {    
        return item.split('').reverse().join('');
    }).join('');
}

wordsReverser("This is fun, hopefully.");
// Output: sihT si nuf, yllufepoh.

Regex to extract whitespace, commas and periods.

Answer:3
function wordsReverser(str){
  return str.split(' ').
    map(function(item) {    
        var letters = item.match(/[a-z]*/i);
    return item.replace(letters[0], letters[0].split('').reverse().join(''));
}).join(' ');

}

 wordsReverser("This is fun, hopefully.");
//Output: 'sihT si nuf, yllufepoh.'

Most likely not fool proof

Answer:4

im having issues trying to fill a single 10 row textarea with data using ng-repeat instead it creates a new textarea for every data entry. how can i just insert into one? and i would like to make the ...

im having issues trying to fill a single 10 row textarea with data using ng-repeat instead it creates a new textarea for every data entry. how can i just insert into one? and i would like to make the ...

  1. fill textarea with javascript
  2. fill textarea with jquery
  3. fill textarea with php
  4. fill textarea with text
  5. fill textarea with json
  6. fill textarea with value
  7. fill textarea with html
  8. fill textarea with js
  9. fill textarea with content
  10. fill textarea with javascript jquery
  11. fill textarea from database
  12. fill textarea in div
  13. javascript fill textarea with text
  14. html fill textarea with text
  15. java fill textarea with arraylist
  16. jquery fill textarea with text

I have the following HEAD tag <head> <title>Test</title> <link id="css1" href="demo.css" rel="stylesheet" type="text/css"> </head> What I want to be able to in ...

I have the following HEAD tag <head> <title>Test</title> <link id="css1" href="demo.css" rel="stylesheet" type="text/css"> </head> What I want to be able to in ...

I'm learning how to use React JS with my Rails app. Disclaimer: This is the first time I am messing around with JS! I'm following a talk by Michael Chan at Rails Conf. Everything works beautifully ...

I'm learning how to use React JS with my Rails app. Disclaimer: This is the first time I am messing around with JS! I'm following a talk by Michael Chan at Rails Conf. Everything works beautifully ...

  1. react with rails 6
  2. react with rails api
  3. react with rails tutorial
  4. react with rails user authentication
  5. react with rails
  6. react with rails 5
  7. react with rails 5.2
  8. react-rails with redux
  9. react-rails with webpacker
  10. react-rails with browserify
  11. using react with rails
  12. react js with rails
  13. react native with rails
  14. integrate react with rails
  15. react router with rails
  16. react app with rails api
  17. react native with rails backend
  18. react js with rails tutorial
  19. react app with rails backend
  20. react js with rails api

I'm working on a form that has 4 different ajax request and I want to check if all 4 are successful. If they all are successful I want to switch step. Im trying something like this: If i get error ...

I'm working on a form that has 4 different ajax request and I want to check if all 4 are successful. If they all are successful I want to switch step. Im trying something like this: If i get error ...

  1. variable value jquery
  2. change variable value jquery
  3. get variable value jquery
  4. set variable value jquery
  5. update variable value jquery
  6. check variable value jquery
  7. css variable value jquery
  8. refresh variable value jquery
  9. add variable value jquery
  10. use variable value jquery
  11. access variable value jquery
  12. input value variable jquery
  13. change global variable value jquery
  14. php variable jquery value
  15. option value variable jquery
  16. attribute value variable jquery
  17. reset value variable jquery
  18. alert variable value in jquery
  19. display variable value in jquery
  20. print variable value in jquery