Imacros - Looping multiple macros specific number of times using Javascript - javascript

I play multiple Macros one after another, multiple times using a simply loop in javascript .js file as below
var i;
for (i = 1; i <= 10; i++)
{
iimSet("loop", i);
iimPlay("1.iim");
iimPlay("2.iim");
iimPlay("3.iim");
}
but within the loop, i want each macro to be played specific number of times.
Say 1.iim 4 times, 2.iim 4 times and 3.iim full 10 times
How can i do that?
I tried adding "if" statement within the "for" loop for each macro
Var i1;
if(i1 <=4)
{iimPlay("1.iim");
i1++;}
But that didn't work :(
TIA

I would go with the "If" this way:
var i;
for (i = 1; i <= 10; i++) {
iimSet("loop", i); //this will be run 10 times
if(i<=4) {
iimPlay("1.iim");//this will be run during the 4 first loops
}
if(i>4 && i<=8) {
iimPlay("2.iim");//this will be run during the 4 next loops
}
iimPlay("3.iim"); //this will also be run 10 times
}

Related

Print even number using for loop without using if condition

I have an easy drill to print all the even numbers between 1-1000.
I want to set the condition in the line of the loop and not below it..
This is what I've tried:
for (let i = 1; i <= 1000 && i % 2 == 0 ; i++) {
document.write(i + " ");
// I dont want the condition here !!!
}
I searched the forum and tried this too:
for (let i = 1;( (i <= 1000) && (i % 2 == 0) ); i++) {
document.write(i + " ");
}
It looks like the same code I think, but there is nothing in the console when I run the code..
The test condition in the loop header determines whether the loop will continue to iterate. Because the first value of i is 1, and 1 is not even, the loop body is never run.
The whole test expression must be true (well, "truthy") for the loop not to stop. Therefore, you cannot place the evenness test in the loop header. It must be a separate test inside the loop body.
Now, you could do this without a test by starting the iteration at 2 instead of 1 and adding 2 on each iteration. Then you don't need to test for evenness at all:
for (let i = 2; i <= 1000; i += 2)
document.write(i);

javascript is there a way to increment a for loop with a variable

I have a function -- let's call it test(arg1,arg2), called from program1, which does a number of things and is working correctly. Within test there is a loop:
for(j=1;j<=top;j++) {
stuff happens based on j
}
I would like to call test(arg1,arg2) from a different program, say program2. Everything about test is the same for these two programs except the for loop. For program2 I need that loop to be
for(j=2;j<=top;j+=2) {
stuff happens based on j
}
Otherwise everything else is exactly the same.
The second argument, arg2 tells us whether the script was called from program1 or program2. But I can't figure out how to write a variable "for" statement. I tried an if statement based on arg2
var jstart = 1 or 2
var jincr = '++' or '+=2'
and then wrote the loop as
for(j=jstart;j<=top;j jincr) {
This did not work, although it is an approach that works in other languages.
Can someone suggest I way I can do this without writing an entirely separate script for the two cases?
As simple as that
jstart = 1 // or 2
jincr = 1 // or 2;
for(j=jstart;j<=top;j += jincr) {
The most reusable way would be to put your loop in a function that accepts increment as an argument:
function doStuff (inc) {
for(var j = inc; j <= top; j += inc) {
// stuff happens based on j
}
}
// Program 1
doStuff(1)
// Program 2
doStuff(2)

Define function based on global variable in javascript [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 7 years ago.
I'm trying to print out a number every 5 seconds:
for(i=0; i<10; i++) {
setTimeout(function() {
console.log(i)
}, 5000 * i);
}
But instead I print 10 every 5 seconds because the loop finished and the global variable i is ten. Is there a way to avoid this happening?
You can use bind to create a closure:
for (i = 0; i < 10; i++) {
setTimeout((function(num) {
console.log(num);
}).bind(this, i), 5000 * i);
}
If ES6 Is allowed you could use a let declaration.
for (let i = 0; i < 10; i++) {
setTimeout(function () {
console.log(i)
}, 5000 * i);
}
The setTimeout function in javascript doesn't work like time.sleep(n) in python. The code will not pause and continue to run after 5s. It will set the body of the function in a queue that will be served in the time you picked. Since you loop 10 times, it will set all of your orders which are basically print i in a queue and serve them all together. To avoid this you can make a use of your loop in something that looks quite like this
setTimeout(function(){ console.log(i)}, 5000*i);
which will schedule each if the print 5s away from the last one !

Nested Javascript for loops with break and label statements

I'm kinda new to Javascript and currently going over the book Professional Javascript for Web Developers and I came across this code which uses a break statement to exit the current loop and jump to a label named outermost.
Now I understand what break and labels do but I can't wrap my head around why the value ends up being 55 at the end?
Ok so the for loop with var i will loop 4 times then at 5 it breaks out to label:outermost and same with j so the first iteration i = 4 and j = 4 and num = 2. I guess this part confuses me.. at what point does the code stop. My first instinct if I were to code this from scratch is to have an outside variable and set the condition on that. But with the below code I don't get where the control structure lies and the final value. Appreciate any help or to be pointed in the right direction, thanks.
var num = 0;
outermost:
for (var i=0; i < 10; i++) {
for (var j=0; j < 10; j++) {
if (i == 5 && j == 5) {
break outermost;
}
num++;
}
}
alert(num);
This nested loop is emulating an odometer. i is the 10's digit, j is the 1's digit. Every time the 1's digit changes, num is incremented; at the start of each iteration, num contains the odometer's value.
The loop stops when both i and j are 5. At that point, the odometer would read 55, and that's what is in num.
When i was 0 to 4, the innermost loop is executed 50 times. When i = 5, the innermost loop is executed just 5 times until it reached i==5 && j==5 and jumped out. So it's total of 55 times.

how restricted is recursion in javascript?

I guess its to stop browsers getting nailed all the time by duff code but this:
function print(item) {
document.getElementById('output').innerHTML =
document.getElementById('output').innerHTML
+ item + '<br />';
}
function recur(myInt) {
print(myInt);
if (int < 10) {
for (i = 0; i <= 1; i++) {
recur(myInt+1);
}
}
}
produces:
0
1
2
3
4
5
6
7
8
9
10
10
and not the big old mess I get when I do:
function recur(myInt) {
print(myInt);
if (int < 10) {
for (i = 0; i <= 1; i++) {
var x = myInt + 1;
setTimeout("recur("+x+")");
}
}
}
Am I missing something or is this how you do recursion in JS? I am interested in navigating trees using recursion where you need to call the method for each of the children.
You are using a global variable as loop counter, that's why it only loops completely for the innermost call. When you return from that call, the counter is already beyond the loop end for all the other loops.
If you make a local variable:
function recur(int) {
print(int);
if (int < 10) {
for (var i = 0; i <= 1; i++) {
recur(int + 1);
}
}
}
The output is the same number of items as when using a timeout. When you use the timeout, the global variable doesn't cause the same problem, because the recursive calls are queued up and executed later, when you have exited out of the loop.
I know what your doing wrong. Recursion in functions maintains a certain scope, so your iterator (i) is actually increasing in each scope every time the loop runs once.
function recur(int) {
print(int);
if (int < 10) {
for (var i = 0; i <= 1; i++) {
recur(int+1);
}
}
}
Note it is now 'var i = 0' this will stop your iterators from over-writing eachother. When you were setting a timeout, it was allowing the first loop to finish running before it ran the rest, it would also be running off the window object, which may remove the closure of the last iterator.
Recursion is very little restricted in JavaScript. Unless your trees are very deep, it should be fine. Most trees, even with millions of elements, are fairly wide, so you get at most log(n) recursive calls on the stack, which isn't noramally a problem. setTimeout is certainly not needed. As in your first example, you're right that sometimes you need a guard clause to guarantee that the recursion bottoms out.

Categories

Resources