1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

please help a newbie with this simple script...

Discussion in 'JavaScript' started by yulsa, Feb 26, 2011.

  1. #1
    Hi, can anyone please help me write a script?

    I have a list of ingredients, that have different names and a quantity associated with them, like this:

    ingredient_01 3
    ingredient_02 5
    ingredient_03 8
    ingredient_04 7
    etc... (have the lists in tables, and I can copy them into the javascript no problem)

    I need to run some kind of function/program/magic that will create an output for me from this table with 3 of ingredient_01, 5 of ingredient_02 i.e I need to print each ingredient as many times as the associate number states, so the output looks like this:

    ingredient_01
    ingredient_01
    ingredient_01
    ingredient_02
    ingredient_02
    ingredient_02
    ingredient_02
    ingredient_02
    ingredient_03
    ingredient_03
    ingredient_03
    ingredient_03
    ingredient_03
    ingredient_03
    ingredient_03
    ingredient_03
    ingredient_04
    ingredient_04
    ingredient_04
    ingredient_04
    ingredient_04
    ingredient_04
    ingredient_04

    I've been reading about arrays, but I haven't been able to write anything that actually runs...

    Any tips?

    Thank you everyone...
     
    yulsa, Feb 26, 2011 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    <html>
    <head>
    <script type='text/javascript'>
    	var ingredients = new Array("ingredient_01 3", "ingredient_02 5", "ingredient_03 8", "ingredient_04 7");
    	for (var i=0; i<ingredients.length; i++) {
    		var parts = ingredients[i].split(' ');
    		var total = parts[1];
    		for (var j=0; j<total; j++) document.write(parts[0]+"<br />");
    	}
    </script>
    </head>
    <body>
    </body>
    </html>
    
    Code (markup):
    Homework questions are really boring :p

    This is an excellent page about Javascript arrays:
    http://www.hunlock.com/blogs/Mastering_Javascript_Arrays
     
    Last edited: Feb 26, 2011
    Cash Nebula, Feb 26, 2011 IP
  3. yulsa

    yulsa Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you so much!

    It's actually for a project I'm doing... But I'm so in over my head with Javascript that simply the fact that there are two things in the array threw me off for a week! Always humbled by the good coders out there...

    And thank you for the link!

     
    yulsa, Feb 28, 2011 IP
  4. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You're welcome :)

    So what kind of project are you doing? The results seem so pointless that I thought it just had to be a homework question :D
     
    Cash Nebula, Feb 28, 2011 IP
  5. yulsa

    yulsa Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Generating sort of tag clouds (or wordles) for terms based on how frequently they come up. But I only have a list of the terms, and number for frequency... javascript is the only script I'm familiar with enough to understand how it does the work...

    Guess some of those homework projects have real life applications! :)

     
    yulsa, Mar 1, 2011 IP