Quotes help make search much faster. Example: "Practice Makes Perfect"

Wednesday, November 7, 2012

Codecademy: Sample Solution to "To Each Their Own" under jQuery track



//Sample Solution to script.js
$('document').ready(function() {

//first we add some random divs (random contents too!)
var divCount = (Math.floor(Math.random()*5 + 5));
//building the list of random html
var divContent = [];
for (var i=0;i<divCount;i++) {
    divContent.push("Random! "+Math.random());
}
//adding each random String in a div using .each()
//again, you'll be able to do this by the end of the lesson
$.each(divContent,function(index,value) {
    var newDiv = $('<div/>');
   
    if (Math.random() > 0.35) {
        newDiv.addClass('eachMe');
    }
   
    newDiv
        .html(Math.random())
        .appendTo('body');
});
    alert(alertTheEachMes());
});

//you edit this!!
function alertTheEachMes() {

//use $() to select all the divs with class 'eachMe'
var $allOfTheEachMeDivs = $('.eachMe');

//keeper for our alerts
var capturedAlerts = [];

//YOUR CODE HERE
//create a map() function to add all of the alerts to our
//captured alerts array see the hint if you are unsure
capturedAlerts = $.map($allOfTheEachMeDivs,function(value){
return $(value).html();
});

//we return all of the alerts we captured
return capturedAlerts;
}

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts