“Given a sentence, I want you to reverse every other word”

For example:
Well hello, Jordan would be Well ,olleh Jordan

My original solution

function sentenceRev(sentence) {
  var newSentence = '';
  var splitEm = sentence.split(' ');
  for(i=0;i<splitEm.length;i++) {
    if(/^-?\d*[13579]$/.test(i) && i !== 0) {
      newSentence += splitEm[i].split('').reverse().join('');
    }
    else {
      newSentence += splitEm[i];
    }
    newSentence += ' ';
  }
  return newSentence;
}

console.log(sentenceRev('what about this sentence then you big smelly wonderful human being I want to see your god damn javascript'));

Jordan’s cleaner solution – making use of array methods and shorthand code.

var words = 'what about, this sentence then you big smelly wonderful human being I want to see your god damn javascript'.split(' ');
words.forEach(function(word, i){
  (i % 2 !== 0) ? words[i] = word.split("").reverse().join('') : '';
});
console.log(words.join(' '));

Worthy of Note

Worthy of Note is a site aimed at Web Designers & Developers. It offers a wide range of resources to help assist anyone in the web industry.

View