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...
<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 This is an excellent page about Javascript arrays: http://www.hunlock.com/blogs/Mastering_Javascript_Arrays
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!
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
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!