Question about practice 1 from Variables, Data Types

Hi,

I went through the developers essentials course last week, I am redoing all the practices because most things don’t come to me as logical yet.

In the practice below I want to know about the logic behind the index part. Index in this case is also a variable. Why do you have to calculate the minimum and maximum this way and does it not work as array(0) in the If activity?

If someoen can explain to me the full thought behind this workflow then I’d be happy to learn. I don’t need to solve anything, I just want to understand!

Thank you in advance!

@JVDS This is a Simple Logic based on Identifying Maximum and Minimum Values.

  1. Considering you have 6 Values in the Array.
  2. We Assume there is a Minimum Value and Maximum Value, Hence We Keep one of the Value as a Max Value and one of the Value as a Minimum.
  3. Next, We Compare All the Values in the Array with the Assumed minimum and maximum value
  4. If any other Value is Lesser than the Assumed Value, The new Value is Assigned as minimum.
    If any other Value is Greater than the Assumed Value, The new Value is Assigned as Maximum.

I hope you get the Logic :sweat_smile:

index starts at 0 and ends at 5 as you iterate through the loop. The conditions are checking the data in your array to see if the current value is less than or equal to the next value.

On the first iteration, the loop won’t do anything, as min and max are set to the first element, 7. On the next iteration where the value of array(1) is 5, the condition for calculating the min value will see that array(1) < min, which is 5 < 7. Since this condition is true, the minimum value min is set to array(1), which is 5. The min condition will continue to update min until the list is exhausted, meaning min will be set to the smallest value.

The max condition works the same way, but in the opposite direction, checking if the current value in the array is greater than the last maximum value found.

1 Like

Hey, thanks for both of your responses.
I understand what both of you meant and said, but that doesn’t make it clear why I need “index” as a seperate variable and not using the index from the array.

I hope my question makes sense, if I’m asking something super werid also let me know!

The array itself does not contain an index variable. The index used in the workflow is generated by the For Each activity starting with 0 and adding 1 after each iteration. The index is the position of each of the values in the array. For example, value 7 is at index 0 and value 4 is at index 3.

So because the array doesn’t have an index variable we have to create one for it, is that correct?

1 Like

That’s right, and the variable is updated in the For Each parameters.

1 Like

Great, thanks for the help!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.