Skip to content

Commit

Permalink
Complete htmlJqueryColor.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Jan 1, 2015
1 parent 9e5b1ea commit a2a5043
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions htmljQueryColor/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,31 @@ $(function(){
// becomes: <p><span>Hey</span><span>there</span></p>
// HINT: the `split` array method is your friend

// TODO: your code here!
var addSpan = function () {
$('p').each(function (idx, p) {
var $pElement = $(p);

var text = $pElement.text().trim().split(' ').reduce(function (acc, cur) {
return acc + '<span>' + cur + '</span>' + ' ';
}, '');

$pElement.html( text );
});
};

// --------------STEP 2--------------
// Next, change spans to random colors, once per second

// TODO: your code here!

var changeSpanColors = function () {
$('span').each(function (idx, s) {
var r = Math.floor( Math.random() * 256 );
var g = Math.floor( Math.random() * 256 );
var b = Math.floor( Math.random() * 256 );
var rgb = 'rgb(' + r + ', ' + g + ', ' + b + ')';
$(s).css( 'background-color', rgb );
});
};

addSpan();
setInterval(changeSpanColors, 1000);
});

0 comments on commit a2a5043

Please sign in to comment.