Skip to content

Instantly share code, notes, and snippets.

@kellylougheed
Created December 5, 2016 02:23
Show Gist options
  • Save kellylougheed/2512590f9954e5b27fdfb2274e0adf43 to your computer and use it in GitHub Desktop.
Save kellylougheed/2512590f9954e5b27fdfb2274e0adf43 to your computer and use it in GitHub Desktop.
Colors every other word in a string by wrapping <span> tags around it.
function colorEveryOther(str) {
var arr = str.split(" ");
for (var i = 0; i < arr.length; i++) {
if (i % 2 === 0) {
arr[i] = "<span>" + arr[i] + "</span>";
}
}
var newStr = arr.join(" ");
return newStr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment