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

Friday, December 7, 2012

Codecademy: Codeyear A Better Way: Using Associative Arrays


//sample solution


//students on the roll
var roll = ["robert","joe","sharon"];

//students actually in the class
var students = {"sharon":true, "robert":true};

//Loop over the roll array. For each name in the roll array, check if
//that name exists in the students associate array.
//Whenever you find a student that is present, increment the numPresent
//counter by 1
var numPresent = 0;
for(var i=0; i<roll.length; i++){
if(students[roll[i]]===true){
numPresent++;
}
}
console.log("The number of students present is "+numPresent+".");

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts