MZ help with understanding syntax.

hi guys, i just wanna make sure im not going insane. Can you read this and tell me what it says?

let _array = [big, little, giant, teenytiny];

if(_array.indexOf("little"))
{
do summersault!;
}

thanks for the help in advance.

also... i swear im not dumb.😅
It doesn't say anything.

First off, when you declare your array you're saying the four elements are other variables called big, little, giant, and teenytiny, but you don't declare those variables or assign them values anyplace.

Then, in your if condition, you look to see if the string "little" is in the array (has an index), which will fail because you assigned the array elements to be the names of variables, not strings. (I'm also not sure that it would work regardless, without a space after if)

Then, do in that context doesn't mean anything. There's only such a thing in a do...while loop, which forces a loop to execute at least once, but you don't have anything like that inside of your code.

Finally, neither your code nor I know what summersault! is. If it's a function, you're calling it incorrectly because there need to be parentheses (whether you're actually passing in any arguments or not).

I suggest you check out some JavaScript tutorials. I like w3schools, starting with the Statements chapter.

It doesn't say anything.

First off, when you declare your array you're saying the four elements are other variables called big, little, giant, and teenytiny, but you don't declare those variables or assign them values anyplace.

Then, in your if condition, you look to see if the string "little" is in the array (has an index), which will fail because you assigned the array elements to be the names of variables, not strings. (I'm also not sure that it would work regardless, without a space after if)

Then, do in that context doesn't mean anything. There's only such a thing in a do...while loop, which forces a loop to execute at least once, but you don't have anything like that inside of your code.

Finally, neither your code nor I know what summersault! is. If it's a function, you're calling it incorrectly because there need to be parentheses (whether you're actually passing in any arguments or not).

I suggest you check out some JavaScript tutorials. I like w3schools, starting with the Statements chapter.

On the if statement note it's fine not having a space after it. Javascript doesn't care about whitespace at all as long as you separate keywords from other alphanumeric characters.
On the if statement note it's fine not having a space after it. Javascript doesn't care about whitespace at all as long as you separate keywords from other alphanumeric characters.
Thanks for the info 🙂
Hi there!

If you are saying that because of an error, I think you just miss the " " quotes around your array elements, and maybe that is what causing the error.

Another thing is:
if(_array.indexOf("little"))
{
do summersault!;
}

Even if you fix the elements to be properly strings, your if statement will always return true, regardless the array has the element or not.

Because when the indexOf method does not find the element inside the array, it will return -1.
And -1 will return true.

I didn't quite get your question clearly, so I assume that was the problem?
What do you not understand about the code you posted? Can you be more specific?
On the if statement note it's fine not having a space after it. Javascript doesn't care about whitespace at all as long as you separate keywords from other alphanumeric characters.
It doesn't say anything.

First off, when you declare your array you're saying the four elements are other variables called big, little, giant, and teenytiny, but you don't declare those variables or assign them values anyplace.

Then, in your if condition, you look to see if the string "little" is in the array (has an index), which will fail because you assigned the array elements to be the names of variables, not strings. (I'm also not sure that it would work regardless, without a space after if)

Then, do in that context doesn't mean anything. There's only such a thing in a do...while loop, which forces a loop to execute at least once, but you don't have anything like that inside of your code.

Finally, neither your code nor I know what summersault! is. If it's a function, you're calling it incorrectly because there need to be parentheses (whether you're actually passing in any arguments or not).

I suggest you check out some JavaScript tutorials. I like w3schools, starting with the Statements chapter.


guys thanks so much, my sanity has been retained thanks to you, ok I was having an issue where I was trying to find if an element in the array has a particular substring or string just by comparison. didn't really have to be a string tbh... but yeah. um basically I was doing this

if(this._gatherer.indexOf("GoldCoin"))
{
$_stoneObtained.s40 = true;
}
_gatherer being an array right? so indexOf("GoldCoin") would basically be checking the array at that index to return the actual index number if the element is present and null or undefined if its not in the array. I thought that would work like in my head it should have cause declaring something in an if statement without any conditions is the same and asking if its true or false but for the life of me i couldnt get it to work.

what I should have done was this

if(this._gatherer.indexOf("GoldCoin") > -1)
{
$_stoneObtained.s40 = true;
}

now with this its checking if the index returned is actually greater than neg 1 which is basically saying that yeah its in there somewhere so just um carry on~ its not like this isn't completely roundabout and unorthodox but if it works then you do you... basically my code giving me sass. guys if I'm wrong about anything here or if I'm over looking anything pls let me know. I feel like I'm forgetting something really important.


Hi there!

If you are saying that because of an error, I think you just miss the " " quotes around your array elements, and maybe that is what causing the error.

Another thing is:


Even if you fix the elements to be properly strings, your if statement will always return true, regardless the array has the element or not.

Because when the indexOf method does not find the element inside the array, it will return -1.
And -1 will return true.

I didn't quite get your question clearly, so I assume that was the problem?
What do you not understand about the code you posted? Can you be more specific?
that was totally it btw
declaring something in an if statement without any conditions is the same and asking if its true or false
I really am not sure what you're trying to say here...you're not declaring anything inside of your if condition.
what I should have done was this
That works, but if you're not using the index for anything, why bother getting it? Just do
Code:
if (array.includes(this_element))

the includes method returns a straightforward true or false.
I really am not sure what you're trying to say here...you're not declaring anything inside of your if condition.

That works, but if you're not using the index for anything, why bother getting it? Just do
Code:
if (array.includes(this_element))

the includes method returns a straightforward true or false.
ah! I DIIIID~~ and it didn't work wuuuu~~~ that was like the first thing I tried... if you're saying that it works... I must've been doing something wrong.

I'm gonna be so mad if this works lol.
guys thanks so much, my sanity has been retained thanks to you, ok I was having an issue where I was trying to find if an element in the array has a particular substring or string just by comparison. didn't really have to be a string tbh... but yeah. um basically I was doing this

if(this._gatherer.indexOf("GoldCoin"))
{
$_stoneObtained.s40 = true;
}
_gatherer being an array right? so indexOf("GoldCoin") would basically be checking the array at that index to return the actual index number if the element is present and null or undefined if its not in the array. I thought that would work like in my head it should have cause declaring something in an if statement without any conditions is the same and asking if its true or false but for the life of me i couldnt get it to work.

what I should have done was this

if(this._gatherer.indexOf("GoldCoin") > -1)
{
$_stoneObtained.s40 = true;
}

now with this its checking if the index returned is actually greater than neg 1 which is basically saying that yeah its in there somewhere so just um carry on~ its not like this isn't completely roundabout and unorthodox but if it works then you do you... basically my code giving me sass. guys if I'm wrong about anything here or if I'm over looking anything pls let me know. I feel like I'm forgetting something really important.



that was totally it btw
Okay, so a little bit about conditionals and how they work. As you've seen, an explicit conditional is exactly what it says on the tin: checking that indexOf("GoldCoin") is > -1 just straight checks that the value returned by indexOf is greater than the numeric value -1.

It gets a bit tricker when you omit a comparison operator. What this does is returns true if the expression evaluates to a *truthy* value, and false if it evaluates to a *falsy* value. Note that this is not the same as *true* and *false*, which are specific boolean values. A value is also considered truthy if it meets one of several other conditions such as: containing an object, containing a number that isn't 0, containing a non-empty string. Falsy values include the integer value 0, an empty string, null, undefined, etc.

So just doing if(this._gatherer.indexOf("GoldCoin")) is saying "if the indexOf function returns a truthy value this condition is true, and if it doesn't return false".

But as Eliaquim pointed out, -1 is a truthy value. So even if the index wasn't found, your condition was met.
if you're saying that it works... I must've been doing something wrong.
Well it's kind of the point for that function existing, right? It wouldn't be a default method in the JavaScript library if it didn't work 😛

As has already been pointed out, if your actual code is anything like your original post, there are a number of other things that would cause it to fail.

If you were looking for a string but your array values weren't declared as strings, then includes() would return false.

If you had your next code statement with do in front of it, that would fail.
I kind of knew the whole do thing as it was meant as... pseudocode. since we're here I have another problem where its kind of iffy and between the lines but I cant get it to work because I'm missing something.

can I use this?

var _monies = {
yen1 : 10000000,
yen2 : 10000
}

var i = 2;

alert(eval(_monies.yen) + i.ToString()); // <<-------------------------

to access monies?
I kind of knew the whole do thing as it was meant as... pseudocode. since we're here I have another problem where its kind of iffy and between the lines but I cant get it to work because I'm missing something.

can I use this?

var _monies = {
yen1 : 10000000,
yen2 : 10000
}

var i = 2;

alert(eval(_monies.yen) + i.ToString()); // <<-------------------------

to access monies?
Not as written; it'd need to be either _monies.yen1 or _monies.yen2. You can't do _monies.yen because that isn't a property you defined in the object.

Also adding a string to an integer just appends it, so if you did that, the alert would be either "100000002" or "100002".
Not as written; it'd need to be either _monies.yen1 or _monies.yen2. You can't do _monies.yen because that isn't a property you defined in the object.

Also adding a string to an integer just appends it, so if you did that, the alert would be either "100000002" or "100002".
aw ok, i was hoping i could use a 'for loop' to cycle through the different element of _monies.
what about this?

alert(eval(_monies.yen + i.ToString()));
im just grasping at straws rn tbh.
aw ok, i was hoping i could use a 'for loop' to cycle through the different element of _monies.
what about this?

alert(eval(_monies.yen + i.ToString()));
im just grasping at straws rn tbh.
Your alert is literally identical to the previous one as far as I can see. 😛

Why don't you tell us what you're trying to do? That should allow us to help you better than troubleshooting code you're not sure about.
Your alert is literally identical to the previous one as far as I can see. 😛

Why don't you tell us what you're trying to do? That should allow us to help you better than troubleshooting code you're not sure about.


hmmmm ok. SOOO I have an object of ridiculous proportions
and an array which is equally as big that contains strings. I need to check the array to make sure that certain elements are present and change the elements in the object to match the status of each corresponding array element. but I don't wanna write like the same if statement with tiny changes 1000 times that... not fun of healthy. so ummm lol yeah.

if I can use a 'for loop' like this


var specialObj = {
item1 : "boop",
item2 : "boop",
item3 : "boop",
item4 : "boop",
item5 : "boop"
}




for (let i = 0; i < thearray.length; i++) {
if(thearray == RndmData)
{
eval("specialObj.item") + i.ToString() = "Big boop!"
}

}

yeah so something like that instead of having an if statement a million times. in honesty I only have to do it 40 time but that's still a lot and I'm not sure how it'll affect performance.
You should be using the code tags (the </> icon on the top bar). It will preserve indents and not turn your brackets into BBCode commands. (see how [i] turned half your post into italics?)
Code:
for (let i = 0; i < thearray.length; i++) {
if(thearray[i] == RndmData)
So far, so good...
Code:
eval("specialObj.item") + i.ToString() = "Big boop!"
What on earth is any of that supposed to be doing?

1 - Why are you using the string "specialObj.item" instead of referring to your variable? This seems to be a continuing point of confusion for you and it's just...kinda weird 😛 If you put something in quotes, that's a string, the actual sequence of characters. If you don't, it's referring to a code object (variable, function, etc.). That's it.

2 - Presuming you meant to reference the strings you stored in your little hash array earlier, why are you running an eval on it?

3 - There's no reason to call toString (which you misspelled) on i. If you add a number (which i is) onto the end of a string, it will concatenate the string.

4 - Both eval and toString are going to return constant values. You can't assign constant values to something else. That's like saying "4 + 7 = 19" - it's an invalid left hand assignment.
yeah so something like that instead of having an if statement a million times. in honesty I only have to do it 40 time but that's still a lot and I'm not sure how it'll affect performance.
Why would if statements affect performance? As far as your computer knows, there's no difference between running a loop with 40 iterations and going down 40 lines of if statements, it's performing the exact same operations to evaluate the condition.

The only difference is for your fingers.
hmmmm ok. SOOO I have an object of ridiculous proportions
and an array which is equally as big that contains strings. I need to check the array to make sure that certain elements are present and change the elements in the object to match the status of each corresponding array element. but I don't wanna write like the same if statement with tiny changes 1000 times that... not fun of healthy. so ummm lol yeah.

if I can use a 'for loop' like this


var specialObj = {
item1 : "boop",
item2 : "boop",
item3 : "boop",
item4 : "boop",
item5 : "boop"
}




for (let i = 0; i < thearray.length; i++) {
if(thearray == RndmData)
{
eval("specialObj.item") + i.ToString() = "Big boop!"
}

}

yeah so something like that instead of having an if statement a million times. in honesty I only have to do it 40 time but that's still a lot and I'm not sure how it'll affect performance.
I think you may have misunderstood what I meant by "what are you trying to do?" I need you to tell us *why* you're doing this, not the method you're attempting to use to achieve it. What is this actually for?
You should be using the code tags (the </> icon on the top bar). It will preserve indents and not turn your brackets into BBCode commands. (see how [i] turned half your post into italics?)

So far, so good...

What on earth is any of that supposed to be doing?

1 - Why are you using the string "specialObj.item" instead of referring to your variable? This seems to be a continuing point of confusion for you and it's just...kinda weird 😛 If you put something in quotes, that's a string, the actual sequence of characters. If you don't, it's referring to a code object (variable, function, etc.). That's it.

2 - Presuming you meant to reference the strings you stored in your little hash array earlier, why are you running an eval on it?

3 - There's no reason to call toString (which you misspelled) on i. If you add a number (which i is) onto the end of a string, it will concatenate the string.

4 - Both eval and toString are going to return constant values. You can't assign constant values to something else. That's like saying "4 + 7 = 19" - it's an invalid left hand assignment.

Why would if statements affect performance? As far as your computer knows, there's no difference between running a loop with 40 iterations and going down 40 lines of if statements, it's performing the exact same operations to evaluate the condition.

The only difference is for your fingers.
wuuuu~ my poor fingers.

idk I thought it would be embarrassing is someone saw my code and looked like a library of if statements.
but if there is no other way … then ill do it! I have to finish it after all...

I do know that there prolly is an easier way but my way of approaching the whole problem was probs wrong to begin with soo ok.
I think you may have misunderstood what I meant by "what are you trying to do?" I need you to tell us *why* you're doing this, not the method you're attempting to use to achieve it. What is this actually for?
basically its a complicated type of file based menu system that's really pretty but has so many details... ill be very happy when its done tho cause I have so many more parts to do lol. I wanna finish it soon so I can start with like other stuffs like music, art, story, chars etc. the story part and music is easy for me. The art is gonna be hard tho. I never backdown from a challenge tho!
You can do like

for (const key of Object.keys(specialObj))

to loop through each key.
You can do like

for (const key of Object.keys(specialObj))

to loop through each key.
can you please explain a bit more I think I'm getting it but not quite.

edit' I think I got it now but since the keys are not like index where they have a numeric value I wont be able to loop through checking each element for a corresponding 'key' so the problem persists.

also
You should be using the code tags (the </> icon on the top bar). It will preserve indents and not turn your brackets into BBCode commands. (see how [i] turned half your post into italics?)

So far, so good...

What on earth is any of that supposed to be doing?

1 - Why are you using the string "specialObj.item" instead of referring to your variable? This seems to be a continuing point of confusion for you and it's just...kinda weird 😛 If you put something in quotes, that's a string, the actual sequence of characters. If you don't, it's referring to a code object (variable, function, etc.). That's it.

2 - Presuming you meant to reference the strings you stored in your little hash array earlier, why are you running an eval on it?

3 - There's no reason to call toString (which you misspelled) on i. If you add a number (which i is) onto the end of a string, it will concatenate the string.

4 - Both eval and toString are going to return constant values. You can't assign constant values to something else. That's like saying "4 + 7 = 19" - it's an invalid left hand assignment.

Why would if statements affect performance? As far as your computer knows, there's no difference between running a loop with 40 iterations and going down 40 lines of if statements, it's performing the exact same operations to evaluate the condition.

The only difference is for your fingers.

More syntax stuffs and this one is sorta tricky.

Say for instance you have the following,

JavaScript:
this.bg_ = new Sprite(ImageManager.loadTitle1("V5"));
    this.bg_.x = 0;
    this.bg_.y = 0;
    this.bg_.width = Graphics.width / 12;
    this.bg_.height = Graphics.height / 12;
    this.addChildAt(this.bg_,1);


you need to create 40 of the following and using a for loop and you do exactly that.
the result is you end up with 40 of them, um 40 instances of it?
No problem right?

well my question id how exactly do you access the properties of the different instances since they're all accessed using;

Code:
this.bg
//eg
this.bg.x
//or
this.bg.height
//or even
this.bg.anchor
//etc

i think there is a way to do this its just that i can't figure it out. actually if anyone can give me any info to help withthis id be super...um grateful??

Original thread: https://forums.rpgmakerweb.com/threads/help-with-understanding-syntax.147349/