MV Adding an integer to the current value in a specific index of an array

I want to add 1 to [index 2] of an array each time this script runs:

Code:
let blue = Math.round(Math.random() * ((400 - 100) + 100));
if (blue <= $gameVariables.value(119)[0]) {}
else{$gameVariables.setValue(119, [blue,$gameVariables.value(119)[1],$gameVariables.value(119)[2]]);}

let sky = Math.round(Math.random() * ((40 - 10) + 10));
if (sky <= $gameVariables.value(119)[1]) {}
else{$gameVariables.setValue(119, [$gameVariables.value(119)[0],sky,$gameVariables.value(119)[2]]);}

let cloud = 1; // I know this won't add 1 at the moment because this is where I need help.
$gameVariables.setValue(119, [$gameVariables.value(119)[0],$gameVariables.value(119)[1],cloud]);

This script keeps track of a record of the largest number for each variable (blue, sky, and cloud) each time it runs. The script works perfectly for the first two index positions of the array, but I'm struggling with adding 1 to the final index position. I've been able to set [index position 2] to 1 (as in the example above to show my progress of working code thus far), but I want to add 1 to the previous value of [index position 2] each time this script runs.

Things I can do:

Code:
let cloud = 2 + 3;

correctly sets [index position 2] to 5 (2 + 3 = 5), so I can do math here, however if I add 1 to the index position of the variable:

Code:
let cloud = 1 + $gameVariables.value(119)[2];

I get NaN returned in [index position 2], which means whatever was/is in $gameVariables.value(119)[2] before I tried adding 1 to it wasn't a number, thus I can't add 1 to "not a number".

What's confused me is that in my code I'm able to check if "blue" is less than or equal to $gameVariables.value(119)[0], so why is $gameVariables.value(119)[0] acting like a number whereas $gameVariables.value(119)[2] isn't the first time this code runs before any of the index positions have any values in them yet?

I've been all over Google, Stack Overflow and these forums for hours (in fact since last night too) looking at examples, but I'm probably trying to do something so super-basic that I'm just not seeing what I'm missing.

As always, thank you everyone for your amazing help. I'm still learning and these forums have been incredible!!
I want to add 1 to [index 2] of an array each time this script runs:

Code:
let blue = Math.round(Math.random() * ((400 - 100) + 100));
if (blue <= $gameVariables.value(119)[0]) {}
else{$gameVariables.setValue(119, [blue,$gameVariables.value(119)[1],$gameVariables.value(119)[2]]);}

let sky = Math.round(Math.random() * ((40 - 10) + 10));
if (sky <= $gameVariables.value(119)[1]) {}
else{$gameVariables.setValue(119, [$gameVariables.value(119)[0],sky,$gameVariables.value(119)[2]]);}

let cloud = 1; // I know this won't add 1 at the moment because this is where I need help.
$gameVariables.setValue(119, [$gameVariables.value(119)[0],$gameVariables.value(119)[1],cloud]);

This script keeps track of a record of the largest number for each variable (blue, sky, and cloud) each time it runs. The script works perfectly for the first two index positions of the array, but I'm struggling with adding 1 to the final index position. I've been able to set [index position 2] to 1 (as in the example above to show my progress of working code thus far), but I want to add 1 to the previous value of [index position 2] each time this script runs.

Things I can do:

Code:
let cloud = 2 + 3;

correctly sets [index position 2] to 5 (2 + 3 = 5), so I can do math here, however if I add 1 to the index position of the variable:

Code:
let cloud = 1 + $gameVariables.value(119)[2];

I get NaN returned in [index position 2], which means whatever was/is in $gameVariables.value(119)[2] before I tried adding 1 to it wasn't a number, thus I can't add 1 to "not a number".

What's confused me is that in my code I'm able to check if "blue" is less than or equal to $gameVariables.value(119)[0], so why is $gameVariables.value(119)[0] acting like a number whereas $gameVariables.value(119)[2] isn't the first time this code runs before any of the index positions have any values in them yet?

I've been all over Google, Stack Overflow and these forums for hours (in fact since last night too) looking at examples, but I'm probably trying to do something so super-basic that I'm just not seeing what I'm missing.

As always, thank you everyone for your amazing help. I'm still learning and these forums have been incredible!!
The reason the first works is that when you do $gameVariables.value(119)[0] the first return value you get is "undefined". Checking whether a number is less than undefined will always return false, so your if statement will assign the number to the index.

This doesn't work with your setting of cloud, because there you're trying to *add* a number to "undefined", and that isn't a valid operation so you get NaN.

Any reason you can't just do

JavaScript:
const v = $gameVariables.value(119);
let blue = Math.max(Math.round(Math.random() * ((400 - 100) + 100)), v[0]);
let sky = Math.max(Math.round(Math.random() * ((40 - 10) + 10)), v[1]);
let cloud = ($gameVariables.value(119)[2] || 0) + 1;
$gameVariables.setValue(119, [blue, sky, cloud]);

While we're at it, those random numbers seem awfully complex. First of all, MV/MZ's devs added a randomInt function to the Number class so that you don't have to do the multiplication, and since you're using rigid numbers I think that can be simplified: (400 - 100) is obviously just 300, and I'm pretty sure Math.round(Math.random() * ((400 - 100) + 100)) can just be written as Math.randomInt(400)
const v = $gameVariables.value(119);
let blue = Math.max(Math.round(Math.random() * ((400 - 100) + 100)), v[0]);
let sky = Math.max(Math.round(Math.random() * ((40 - 10) + 10)), v[1]);
let cloud = ($gameVariables.value(119)[2] || 0) + 1;
$gameVariables.setValue(119, [blue, sky, cloud]);

returns NaN for index 0 and index 1, but it does return the correct value (+1) for index 2.

Code:
let blue = Math.round(Math.random() * ((400 - 100) + 100));
if (blue <= $gameVariables.value(119)[0]) {}
else{$gameVariables.setValue(119, [blue,$gameVariables.value(119)[1],$gameVariables.value(119)[2]]);}

let sky = Math.round(Math.random() * ((40 - 10) + 10));
if (sky <= $gameVariables.value(119)[1]) {}
else{$gameVariables.setValue(119, [$gameVariables.value(119)[0],sky,$gameVariables.value(119)[2]]);}

let cloud = ($gameVariables.value(119)[2] || 0) + 1;
$gameVariables.setValue(119, [$gameVariables.value(119)[0],$gameVariables.value(119)[1],cloud]);

However, if I take the part of your code dealing with [index position 2] and add it to the bottom of my code, it does return a value for all three index positions.

So let cloud = ($gameVariables.value(119)[2] || 0) + 1; is what was needed to get it working!

Thank you!
returns NaN for index 0 and index 1, but it does return the correct value (+1) for index 2.

Code:
let blue = Math.round(Math.random() * ((400 - 100) + 100));
if (blue <= $gameVariables.value(119)[0]) {}
else{$gameVariables.setValue(119, [blue,$gameVariables.value(119)[1],$gameVariables.value(119)[2]]);}

let sky = Math.round(Math.random() * ((40 - 10) + 10));
if (sky <= $gameVariables.value(119)[1]) {}
else{$gameVariables.setValue(119, [$gameVariables.value(119)[0],sky,$gameVariables.value(119)[2]]);}

let cloud = ($gameVariables.value(119)[2] || 0) + 1;
$gameVariables.setValue(119, [$gameVariables.value(119)[0],$gameVariables.value(119)[1],cloud]);

However, if I take the part of your code dealing with [index position 2] and add it to the bottom of my code, it does return a value for all three index positions.

So let cloud = ($gameVariables.value(119)[2] || 0) + 1; is what was needed to get it working!

Thank you!
Ah, yeah. Math.max still needs defined values for its arguments. I could write a version that works, but if that line is all you needed to fix what you're doing, then I'll leave you to it. I would still consider looking at simplifying those numbers though.
Ah, yeah. Math.max still needs defined values for its arguments. I could write a version that works, but if that line is all you needed to fix what you're doing, then I'll leave you to it. I would still consider looking at simplifying those numbers though.

I will, and thank you.

I am curious as to how the || is working in

Code:
$gameVariables.value(119)[2] || 0) + 1

I know || is a logical OR, but I'm not sure what that means, especially in context here. Is it saying that if [index 2] isn't defined then set it to 0?
Code:
let cloud = ($gameVariables.value(119)[2] || 0) + 1;

Oh, I think I see it now. It's saying to let cloud = $gameVariables.value(119)[2] OR let cloud = 0.

And since $gameVariables.value(119)[2] is initially NaN, then it's saying let cloud = 0 (+1).

I guess my other question now is how does the code know not to set $gameVariables.value(119)[2] to 0 when $gameVariables.value(119)[2] IS an actual number? Is it just a case of since if the value is NaN it just moves on to an operation it CAN perform, but once it's no longer NaN it can then perform the +1 function?
Code:
let cloud = ($gameVariables.value(119)[2] || 0) + 1;

Oh, I think I see it now. It's saying to let cloud = $gameVariables.value(119)[2] OR let cloud = 0.

And since $gameVariables.value(119)[2] is initially NaN, then it's saying let cloud = 0 (+1).

I guess my other question now is how does the code know not to set $gameVariables.value(119)[2] to 0 when $gameVariables.value(119)[2] IS an actual number? Is it just a case of since if the value is NaN it just moves on to an operation it CAN perform, but once it's no longer NaN it can then perform the +1 function?
In constructs like this, what it's actually saying is "return the left side if it's truthy, or the right side if the left side is falsy". So when it's undefined, it takes the right side. As soon as it's a valid value, it will return the left side instead.
@BookishNerDan , please avoid double posting, as it is against the forum rules. You can use the "Edit" function on your posts to add additional information you've forgotten or respond to multiple people. You can review our forum rules here. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/adding-an-integer-to-the-current-value-in-a-specific-index-of-an-array.158823/