www.webdeveloper.com
Results 1 to 3 of 3

Thread: Need Help (newbie)

Hybrid View

  1. #1
    Join Date
    Dec 2011
    Posts
    4

    Need Help (newbie)

    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.";
    };

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    782
    Hope you don't mind, but I changed a few things:

    Code:
    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
    Last edited by bionoid; 12-29-2011 at 06:00 PM.

  3. #3
    Join Date
    Dec 2011
    Posts
    4
    Thanks so much. I just started learning about a week ago, so i need a lot of help. Thanks again )

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles