Home > front end >  Unable to understand for loop exercise
Unable to understand for loop exercise

Time:10-17

I am taking a JavaScript course, and I needed your help to understand something that is not very clear to me.

They gave me code block, where the statement says that I must exchange the variables from fruits to foodSchedule for foods that are not vegan.

const fruits = ['Strawberry', 'Banana', 'Orange', 'Apple'];
const foodSchedule = [{name: "Salad", isVegan: true},{name: "Salmon", isVegan: false}, {name: "Tofu", isVegan: true}, {name: "Burger", isVegan: false}, {name: "Rice", isVegan: true}, {name: "Pasta", isVegan: true}];

for (let index = 0; index < foodSchedule.length; index  ) {
    const food = foodSchedule[index];
    if(!food.isVegan){
        food.name = fruits.shift();
    }
}

console.log(foodSchedule);

But what I don't understand is in the for line

for (let index = 0; index < foodSchedule.length; index  )

Could someone be so kind as to explain it to me, I would really appreciate it. I have several loop exercises that use that nomenclature and I don't understand why.

CodePudding user response:

https://www.w3schools.com/js/js_loop_for.asp

The for statement creates a loop with 3 optional expressions:

for (expression 1; expression 2; expression 3) {
  // code block to be executed
}

Expression 1 is executed (one time) before the execution of the code block.

Expression 2 defines the condition for executing the code block.

Expression 3 is executed (every time) after the code block has been executed.

The page explains it like this:

From the example above, you can read:

Expression 1 sets a variable before the loop starts (let i = 0).

Expression 2 defines the condition for the loop to run (i must be less than 5).

Expression 3 increases a value (i ) each time the code block in the loop has been executed.

In your example, your loop starts with the first index of the array (0), foodSchedule.length is the length of the array. The statement runs until the intex is equal to the length (index < foodSchedule.length).

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift

The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.

In your example, data is removed from the array if the food is not (!=) vegan.

CodePudding user response:

In order to understand the loop, you must understand the array first.

  1. An array is an object, which can hold more than one value.

  2. Each element in the array has an index that defines its location or order in the array. arrays index starts from 0.

  3. You can access each element individually via its index.

    example :

    const fruits = ['Strawberry', 'Banana', 'Orange', 'Apple'];
    
    console.log(fruits[0]);
    console.log(fruits[2]);
    
       

  4. If you want to access all element in the array, then you must loop through the array using its index and that's what the for loop and other loops are used for.

  5. For loop, has three parts in it:

    1. A variable that defines which index to start looping from in the array.

    2. A condition that compares the index variable with the array length (the number of elements in the array).

    3. The way/frequency of looping by increasing or reducing the index variable.

    4. A block of code to execute if the condition in the for loop is true.

examples to clarify all that:

You can uncomment each consol.log individually to see the result for each one.

const fruits = ['Strawberry', 'Banana', 'Orange', 'Apple'];

// accessing all elements in the array starting from the first one which has index 0
for (let i = 0; i < fruits.length; i  ) {
    console.log(fruits[i]);
};


// accessing elements from the 3rd to the end of array
for (let i = 2; i < fruits.length; i  ) {
    // console.log(fruits[i]);
};

// accessing elements from the last to first in the array
let arrayLength = fruits.length;
for (let i = arrayLength; i > -1; i--) {
    // console.log(fruits[i]);
};

// accessing every second element in the array starting from 0
for (let i = 0; i < fruits.length; i = i 2) {
    // console.log(fruits[i]);
};

  • Related