0

I am working on a problem where I need to be able to reverse a sentence but only words that are greater than 4 can be reversed. the rest of the words must be the same as they are. I have tried to check to see if length is greater than 4 but that does not return the result I am looking for. All I need is to reverse any words that are greater than 4 in the sentence. Any help is greatly appreciated.

Edit: Here is the simple of what I know how to do. It reverses all of the sentence. I am sure that there needs to be some way to break apart each word and determine the length of the word and then bring the sentence back together, but I don't know how that would be done.

var sentence = "This could be the answer I need";

if (sentence.length > 4) {
 console.log( sentence.split('').reverse().join(''));
}

Thank you

pertrai1
  • 4,146
  • 11
  • 46
  • 71
  • 6
    Sharing your code would be a good place to start.... – iandotkelly Jun 14 '14 at 01:47
  • 1
    your code, and a few examples of input and expected output (and how they differ from your expectations regarding your code). – Elliott Frisch Jun 14 '14 at 01:49
  • I believe your problem lies on line 437 of your `strrev.c` source file. No, seriously, despite massive advances by Raymond Chen (http://blogs.msdn.com/b/oldnewthing/), psychic debugging is still not an exact science :-) – paxdiablo Jun 14 '14 at 01:50
  • What behaviour is expected for compound words like "red-haired" for example ? Or weirder, what about possession and abbreviations ? ("let's go to dave's") – axelduch Jun 14 '14 at 02:04

5 Answers5

5

In Short:

var s = 'This is a short sentence'
  , e = s.split(' ').map(function(v){ return v.length>4?v.split('').reverse().join(''):v; }).join(' ');

console.log(e); // 'This is a trohs ecnetnes'

Explained:

var s = 'This is a short sentence' // set test sentence
  , e = s.split(' ')               // 'This is a short sentence' ==> ['This','is','a','short','sentence']
         .map(function(v,i,a){     // REPLACE the value of the current index in the array (run for each element in the array)
            return v.length > 4    // IF the length of the a 'word' in the array is greater than 4
                 ? v.split('')     // THEN return: 'word' ==> ['w','o','r','d']
                    .reverse()     // ['w','o','r','d'] ==> ['d','r','o','w']
                    .join('')      // ['d','r','o','w'] ==> 'drow'
                 : v;              // OR return: the original 'word'
         }).join(' ');             // ['This','is','a','trohs','ecnetnes'] ==> 'This is a trohs ecnetnes'

console.log(e); // 'This is a trohs ecnetnes'
joopmicroop
  • 891
  • 5
  • 15
  • 1
    Thank you for your help and taking the time to explain. – pertrai1 Jun 15 '14 at 07:19
  • Why is this not working?: function spinWords(words){ //TODO Have fun :) let splitWords = words.split(' ').map(function(v){ if(v.length > 4){ return v.split('').reverse().join(''); } else{ } }).join(' ') console.log(splitWords) } spinWords("Jesus is Lord"); – Iamlimo Mar 22 '20 at 02:12
  • You forgot "return v;" in your else statement. – joopmicroop Mar 25 '20 at 16:45
0

You've not shown us your source code, so its hard to know how far you got. Not wanting to just give you the code, therefore removing the opportunity to learn how to put it together, I suggest that you look into the following things:

  • The String split() method, which could be used to split your sentence into individual words in an array

  • Look into how to iterate over an array of your string words in a for-loop, looking for those that are greater than 4 characters long.

  • Understand how to reverse a string in situ - see this answer. Only apply this to the strings that are over the right size. Make sure you replace your original strings with the reversed ones in the arry.

  • Then understand how the Array join() method works.

I'm sorry if I've been over-simplistic in my descriptions - but it is hard to understand from your question how much you need this spelled out. Hope this is helpful.

Community
  • 1
  • 1
iandotkelly
  • 9,024
  • 8
  • 48
  • 67
0

The simplest approach is to do the following: Split the string on the space. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split)

The use the forEach function to loop through each element, reversing if the length is > 4 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)

Then finish with join to put all the elements of the array into a string (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join)

James Black
  • 41,583
  • 10
  • 86
  • 166
0

// For word length greater than equal to 5

function reverseString(s){
  return s.split(' ').
      map( v => { 
         return v.length > 4 ?  v.split('').
         reverse().join('') : v; 
      } ).join(' ');  

}

OR

function reverseString(string){
  return string.replace(/\w{5,}/g, function(w) { return w.split('').reverse().join('') })
}

Replace with your string word length

Abdul Hamid
  • 3,222
  • 3
  • 24
  • 31
-1

Use regex to match words with at least 5 characters, and replace with reversed characters:

var s = "I am working on a problem where I need to be able to reverse a sentence but only words that are greater than 4 can be reversed.";

s2 = s.replace(/\b(\w\w\w\w\w+)\b/g, function(word) {
    return word.split("").reverse().join("");
});

console.log(s2);

outputs:

I am gnikrow on a melborp erehw I need to be able to esrever a ecnetnes but only sdrow that are retaerg than 4 can be desrever. 

fiddle

Fabricator
  • 12,722
  • 2
  • 27
  • 40
  • This doesn't work for compound words with a hyphen or if punctuation is stuck to a word (a dot or a comma for instance) – axelduch Jun 14 '14 at 02:02
  • @aduch, changed to using regex, now it's more concise, and takes care of the edge cases. – Fabricator Jun 14 '14 at 04:19