I wrote a program where i can quesry whether an ice cream flavor is in my top ten, but no matter what i put, it says, "ice cream flavor" is not in top ten. Here is the code.
var iceCream = {
"0": "Chocolate Fudge Brownie",
"1": "Half Baked",
"2": "New York Super Fudge Chunk",
"3": "Coffee Heath Bar Crunch",
"4": "Cherry Garcia",
"5": "Mud Pie",
"6": "Milk & Cookies",
"7": "Cinnamon Buns",
"8": "Chocolate Chip Cookie Dough",
"9": "Boston Cream Pie",
};
var rankFlavor = function(flavor) {
for (var i = iceCream.length; i --{
if (iceCream[i] === flavor ) {
return flavor + " is number " + (i + 1) + " . "
}
}
return flavor + " is not among my top 10.";
};
var iceCream = [
'Chocolate Fudge Brownie',
'Half Baked',
'New York Super Fudge Chunk',
'Coffee Heath Bar Crunch',
'Cherry Garcia',
'Mud Pie',
'Milk & Cookies',
'Cinnamon Buns',
'Chocolate Chip Cookie Dough',
'Boston Cream Pie'
];
function rankFlavor(flavor)
{
var i;
for (i = 0; i < iceCream.length; i++) {
if (iceCream[i] === flavor) {
return flavor + ' is number ' + (i + 1) + '.';
}
}
return flavor + ' is not among my top 10.';
}
alert(rankFlavor('Half Baked')); //2
alert(rankFlavor('Mud Pie')); //6
alert(rankFlavor('Banana')); //??
You were treating an object like an array, but it doesn't work that way
Bookmarks