Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

reverse a string if its value its greater than 3

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'
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #reverse #string #greater
ADD COMMENT
Topic
Name
1+6 =