JS macro don't correctly run if else statement

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.

Moderators: Community Moderators, iMacros Moderators

Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the Google search box (at the top of each forum page) to see if a similar problem or question has already been addressed. This will search the entire contents of the forums as well as the iMacros Wiki.
3. We can respond much faster to your posts if you include the following information:

CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST

Answering your own posts (e.g. attempting to "bump" your topic) drops your topic from the list of unanswered threads, so it may actually receive less views.

JS macro don't correctly run if else statement

by zurello on Wed Feb 07, 2018 2: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 7:38 am, edited 1 time in total.
zurello
 
Posts: 16
Joined: Mon Jul 18, 2016 11:08 pm

Re: Problem with js macro

by chivracq on Wed Feb 07, 2018 7:22 am

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)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
chivracq
 
Posts: 6966
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)

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

by zurello on Wed Feb 07, 2018 7:40 am

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?
zurello
 
Posts: 16
Joined: Mon Jul 18, 2016 11:08 pm

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

by chivracq on Wed Feb 07, 2018 9:36 am

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)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
chivracq
 
Posts: 6966
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)

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

by zurello on Wed Feb 07, 2018 10:06 am

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!
zurello
 
Posts: 16
Joined: Mon Jul 18, 2016 11:08 pm

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

by chivracq on Wed Feb 07, 2018 1: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)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
chivracq
 
Posts: 6966
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)


Return to iMacros for Firefox

Who is online

Users browsing this forum: Google [Bot] and 4 guests

-->