Answers for "reverse a string if its value its greater than 3"

0

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'
Posted by: Guest on August-02-2021

Code answers related to "TypeScript"

Browse Popular Code Answers by Language