JS macro don't correctly run if else statement

Discussions and Tech Support specific to the iMacros Firefox add-on.
Forum rules
iMacros EOL - Attention!

The renewal maintenance has officially ended for Progress iMacros effective November 20, 2023 and all versions of iMacros are now considered EOL (End-of-Life). The iMacros products will no longer be supported by Progress (aside from customer license issues), and these forums will also no longer be moderated from the Progress side.

Thank you again for your business and support.

Sincerely,
The Progress Team

Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the search box (at the top of each forum page) to see if a similar problem or question has already been addressed.
3. Try searching the iMacros Wiki - it contains the complete iMacros reference as well as plenty of samples and tutorials.
4. We can respond much faster to your posts if you include the following information: CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST
Post Reply
zurello
Posts: 16
Joined: Tue Jul 19, 2016 6:08 am

JS macro don't correctly run if else statement

Post by zurello » Wed Feb 07, 2018 9:07 am

Hi to everyone.
I have FF 56.0.2 (32 bit), Windows 7 Home Premium and Imacros 8.9.7.
I write a js macro and it seems run perfectly, but i've noticed that are two problems.

Code: Select all

for (i=1;i<=500;i++)
{
var n = Math.floor(Math.random()*6);
  if (n=1) 
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Arancio1.iim");} 
   else 
   if (n=2)
    {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Marrone1.iim");}
    else if (n=3)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Viola1.iim");
  }
  else 
   if (n=4)
    {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Rosso1.iim");}
     else if (n=5)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Verde1.iim");
  }
  else  (n=6)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Giallo1.iim");
  }
  
  var m = Math.floor(Math.random()*6);
 
  if (m=1) 
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Arancio2.iim");} 
   else 
   if (m=2)
   {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Marrone2.iim");}
      else if (m=3)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Viola2.iim");
  }
  else 
   if (m=4)
     {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Rosso2.iim");}
      else if (m=5)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Verde2.iim");
  }
  else  (m=6)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Giallo2.iim");
  }
    {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Procedura.iim");
  }
}

First problem is that JS macro calls every time iim macro assigned to n=1 and m=1!
I controlled that var generator create right random number, but JS macro don't want call correct iim macro.
Second problem is that JS macro calls twice iim macro instead 1!
It's like JS macro run twice part with "var n" and twice "var m".
Is there someone that can help me?
Bye Bye
Last edited by zurello on Wed Feb 07, 2018 2:38 pm, edited 1 time in total.
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: Problem with js macro

Post by chivracq » Wed Feb 07, 2018 2:22 pm

zurello wrote:Hi to everyone.
I have

Code: Select all

FF 56.0.2 (32 bit) and Imacros 8.9.7.
I write a js macro and it seems run perfectly, but i've noticed that are two problems.

Code: Select all

for (i=1;i<=500;i++)
{
var n = Math.floor(Math.random()*6);
  if (n=1) 
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Arancio1.iim");} 
   else 
   if (n=2)
    {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Marrone1.iim");}
    else if (n=3)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Viola1.iim");
  }
  else 
   if (n=4)
    {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Rosso1.iim");}
     else if (n=5)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Verde1.iim");
  }
  else  (n=6)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Giallo1.iim");
  }
  
  var m = Math.floor(Math.random()*6);
 
  if (m=1) 
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Arancio2.iim");} 
   else 
   if (m=2)
   {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Marrone2.iim");}
      else if (m=3)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Viola2.iim");
  }
  else 
   if (m=4)
     {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Rosso2.iim");}
      else if (m=5)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Verde2.iim");
  }
  else  (m=6)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Giallo2.iim");
  }
    {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Procedura.iim");
  }
}
First problem is that JS macro calls every time iim macro assigned to n=1 and m=1!
I controlled that var generator create right random number, but JS macro don't want call correct iim macro.
Second problem is that JS macro calls twice iim macro instead 1!
It's like JS macro run twice part with "var n" and twice "var m".
Is there someone that can help me?
Bye Bye
You'll need to give a Descriptive Thread Title for me to have a look..., "Problem with js macro" is a bit useless as 60% of all Threads on this Sub-Forum are about "Problem with js macro"...! Try searching the Forum for similar Threads using those Keywords...! :roll:

Hum, and I notice your OS is missing from your FCI... (I didn't read any further, because of the useless Thread Title...)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
zurello
Posts: 16
Joined: Tue Jul 19, 2016 6:08 am

Re: JS macro don't correctly run if else statement

Post by zurello » Wed Feb 07, 2018 2:40 pm

I'm sorry, but for an italian it's a bit difficult explaining correctly 8) .
I've corrected title and i've written my OS.

Update.
I've changed something in the JS macro and now it seems work perfectly, but i don't understand why :?: .
I put === instead == in the if statment, changed final "else" of each if/else statement in "else if", and changed last condition "var===6" in "var===0" and now work perfectly.
Where was the problem/s?
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: JS macro don't correctly run if else statement

Post by chivracq » Wed Feb 07, 2018 4:36 pm

zurello wrote:I'm sorry, but for an italian it's a bit difficult explaining correctly 8) .
I've corrected title and i've written my OS

Code: Select all

FF 56.0.2 (32 bit), Windows 7 Home Premium and Imacros 8.9.7.
OK, good, that's a bit better indeed..., even if the English Grammar has to suffer a little bit, ah-ah...! (It's "One Macro runs/doesn't run..." and "Two Macros run/don't run...", but OK, never mind...)
Well "it's a bit difficult explaining correctly...", it's even more difficult for others to understand you if you don't even try, hardly maybe 10% of the Forum Users speak English natively, so we all have to face the same "Limitations"... :wink:

OK, I can read your Post now... :D
Oh hum..., it's about playing some Random Macros 500/1000 times, hum..., I don't "like" to help usually for that kind of Scripts, as they are usually typically used by Spammers... :roll:

But OK, a few "Thoughts" though, and I'm no JS Guru as I don't use '.js' Scripts myself, but I notice a few "Things"...:
- I'm not sure about '.js' Scripts, but 'if/else' Statements used in 'EVAL()' Statements need the '=' Sign to be doubled in the 'if' part, I guess it's the same Syntax in '.js'...?
=> Try with "if (n==1)" instead of your "if (n=1)"... :idea:

- Your "else (n=6)" (or even with "if (n==6)" if I'm correct about doubling the '=' Sign) looks "syntactically" incorrect to me as well...
I would think it's either "else" or "else if (n==6)" or even "else {n=6;}"... (But the last one is not what you want, I reckon...)

And for the rest, use 'alert()' and/or 'iimDisplay()' (or 'PROMPT' in the '.iim' parts) to follow your Vars and debug your Script... :idea:
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
zurello
Posts: 16
Joined: Tue Jul 19, 2016 6:08 am

Re: JS macro don't correctly run if else statement

Post by zurello » Wed Feb 07, 2018 5:06 pm

Hi Chivracq!
I see that you after try teach me imacros now try teach me english grammar...it's more difficult :D :D .
Thanks for the advice, i changed my code so:

Code: Select all

for (i=1;i<=500;i++)
{
var n = Math.floor(Math.random()*6);

  if (n==1) 
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Arancio1.iim");} 
   else 
   if (n==2)
   
   {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Marrone1.iim");}
   
   else if (n==3)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Viola1.iim");
  }
  else 
   if (n==4)
   
   {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Rosso1.iim");}
   
   else if (n==5)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Verde1.iim");
  }
  else if (n==0)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Giallo1.iim");
  }
  
  var m = Math.floor(Math.random()*6);

  if (m==1) 
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Arancio1.iim");} 
   else 
   if (m==2)
   
   {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Marrone1.iim");}
   
   else if (m==3)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Viola1.iim");
  }
  else 
   if (m==4)
   
   {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Rosso1.iim");}
   
   else if (m==5)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Verde1.iim");
  }
  else if (m==0)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Giallo1.iim");
  }
  
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Procedura.iim");
  

}
}

I just explained that i don't use my/your macros for spamming, i assure, so don't worry about them ;-).
Thanks for the advice of "alert" and "iimdisplay"; my old gwbasic teacher called a similar process the "correction loop" :D .
Now macro seems work perfectly!
Thanks a lot!
Bye!
chivracq
Posts: 10301
Joined: Sat Apr 13, 2013 1:07 pm
Location: Amsterdam (NL)

Re: JS macro don't correctly run if else statement

Post by chivracq » Wed Feb 07, 2018 8:41 pm

zurello wrote:Hi Chivracq!
I see that you after try teach me imacros now try teach me english grammar...it's more difficult :D :D .
Thanks for the advice, i changed my code so:

Code: Select all

for (i=1;i<=500;i++)
{
var n = Math.floor(Math.random()*6);

  if (n==1) 
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Arancio1.iim");} 
   else 
   if (n==2)
   
   {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Marrone1.iim");}
   
   else if (n==3)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Viola1.iim");
  }
  else 
   if (n==4)
   
   {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Rosso1.iim");}
   
   else if (n==5)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Verde1.iim");
  }
  else if (n==0)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Giallo1.iim");
  }
  
  var m = Math.floor(Math.random()*6);

  if (m==1) 
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Arancio1.iim");} 
   else 
   if (m==2)
   
   {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Marrone1.iim");}
   
   else if (m==3)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Viola1.iim");
  }
  else 
   if (m==4)
   
   {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Rosso1.iim");}
   
   else if (m==5)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Verde1.iim");
  }
  else if (m==0)
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Giallo1.iim");
  }
  
  {iimPlay("C:\\Users\\Zigiggi\\Documents\\iMacros\\Macros\\Gusto\\Procedura.iim");
  

}
}

I just explained that i don't use my/your macros for spamming, i assure, so don't worry about them ;-).
Thanks for the advice of "alert" and "iimdisplay"; my old gwbasic teacher called a similar process the "correction loop" :D .
Now macro seems work perfectly!
Thanks a lot!
Bye!
OK, good to hear... And hum, you should be more "Advanced" than me on JavaScript Syntax I would think, as I've never used any '.js'' Script...

And yep indeed about the "(n==6)" Case, I wanted to mention that this Condition would never be met with your Random Nb which produced [0,5], or you'd need to use 'ceil()' instead of 'floor()', or to do a "+1" if you wanted [1,6], but OK, you found out by yourself...
But the final 'else' without further 'if' would have caught it as well, like your 'else if(n==0)'...

>>>

"gwbasic", oh yeah...!, that's more than 30 years ago for me, oops...! I coded a bit in 'gwBasic' as well but 'Peek/Poke' were not supported I think I remember and defining Functions to avoid endlessly repeating Blocks of Code was too much of a hassle so I didn't use very long and I then quickly moved to 'Turbo Pascal', ah-ah...! 8)
- (F)CI(M) = (Full) Config Info (Missing): iMacros + Browser + OS (+ all 3 Versions + 'Free'/'PE'/'Trial').
- FCI not mentioned: I don't even read the Qt...! (or only to catch Spam!)
- Script & URL help a lot for more "educated" Help...
Post Reply