javaScript nested for-in and for loops

Total Post:1


 886  View(s)
Rate this:
I am a beginner in javaScript. Can someone please help me with the below problem ?

I want to write a code with for in and for loop so that :

It will loop through all recipes in the recipeBook, and print the recipe name followed by its ingredients list. For example, the output for the chocolate cake might look as follows:
Chocolate Cake:
- 1 3/4 cup flour
- 1/2 cup cocoa
- 2 tsp baking powder
- 1 cup Chelsea White Sugar
- 125g butter
- 2 Tbsp Chelsea Golden Syrup
- 2 eggs
- 1 1/2 cup milk
- 2 tsp baking soda
- 1 tsp vanilla essence

Code to add "chocolate cake" to the recipe book could look like this:

recipeBook["Chocolate Cake"] = [

  "1 3/4 cup flour",
  "1/2 cup cocoa",
  "2 tsp baking powder",
  "1 cup sugar",
  "125g butter",
  "2 Tbsp golden syrup",
  "2 eggs",
  "1 1/2 cup milk",
  "2 tsp baking soda",
  "1 tsp vanilla essence"
  1. Re: javaScript nested for-in and for loops

    Hi folks, you should make a table like charts which is shown as you wants. than uses loops in/for or nested for type loops for recipeBook. 

    you can follow this code is shown below..

    <body> <h1>Nested Loops</h1>
    <p>The Nested for/in statement loop for chocolate cake recipeBook</p>
    <p id="demo"></p>
    var y = [];
    y[0]= {quantity:1 3/4 fname:"cup", lname:"flour"}; //fname:first name, lname:last name
    y[1]= {quantity:1/2 fname:"cup", lname:"cocoa"};
    y[2]= {quantity:2 fname:"tsp",mname:"backing",lname:"powder"};
    y[3]= {quantity:1 fname:"cup",mname:"chelsea",lname:"white sugar"};
    y[4]= {quantity:125g fname:"butter"};
    ......... .........
    and so on as your choice....
    var x;
    for (x in chocolate) {
        y += chocolate[x] + " ";
    }document.getElementById("Mindstick answer").innerHTML = y;

    define all variables correctly and it's another way to solve your problem. you should try this code for loop... 

    for (for (var i = 0; i < chocolate.length; i++) { 
        for (var j = 0; j < chocolate[i].length; j++) {
            for (var k = 0; k < chocolate[i][j].length; k++) {

    you can explore more in nested loop by this blog,which is given by our another mindstick member.


    glad to help, i hope it will helpful for you.

    Happy Coding :)

      Modified On Jun-07-2018 05:02:26 AM


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.