Copy link to clipboard
Copied
Hi Brothers and Professionals..
I'm Trying to make a Nested for (for repeating numbers) into fields, I have 16 fields named as (SN1-SN2...SN16), i want the user to enter a Number like 20 and i want to Script to Continues Repeating this Number from 20 to 36 (as Example)..i write the following code into Button But when i hit the Button and Enter the Starting Number it Doesn't work at all, even no error message, its my first time to try Nested for twice so i don't know what is the problem, please help me if you can , thanks in advance:
// Dialog Definition
var oDlg = {
strName: "", initialize: function(dialog) {
dialog.load({"usnm":this.strName});
},
commit: function(dialog) {
var data = dialog.store();
this.strName = data["usnm"];
},
description: {
name: "Test Dialog", elements: [ {
type: "view", elements: [
{ name: "Please Enter Starting Number ?", type: "static_text", },
{ item_id: "usnm", type: "edit_text", char_width: 3 },
{ type: "ok_cancel", },
]
},]
}
};
// Dialog Activation
oDlg.strName = "";
if( "ok" == app.execDialog(oDlg)) {
var DesNum = oDlg.strName;
var MaxNum = DesNum+16
for (var i=1 ;i>=16 ;i++){
for (var x=DesNum ;x>=MaxNum ;x++) {
this.getField("SN"+i).value = x;
}
}
}
Use something like this:
for (var i=1 ;i<=16 ;i++) this.getField("SN"+i).value = DesNum + i - 1;
Copy link to clipboard
Copied
Look closely at your first for-loop (line #27)...
Copy link to clipboard
Copied
Because its 16 Text Fields formatted as Numbers, so i make the loop from 1=1 to 16 and the other one should showing continues numbering from the Entered number,, but i didn't get it, what the wrong in line 27
Copy link to clipboard
Copied
Since you're making the same mistake in the line after it I think it's a problem of understanding, not of execution.
The second part of the for-loop definition is not a stop-condition, but a running-condition. When that expression is false, the loop stops, not the other way around, as you have it set up...
Copy link to clipboard
Copied
But How to convert it from running-Condition to Stop Condition ? can you give me an Example ? , thank you
Copy link to clipboard
Copied
Change:
for (var i=1 ;i>=16 ;i++){
To:
for (var i=1 ;i<=16 ;i++){
Copy link to clipboard
Copied
I Make the Change but it didn't work, should i change the other line?
Copy link to clipboard
Copied
Yes...
Copy link to clipboard
Copied
i do the change, and when entering number (1) the calculating start and after long time it fills all the 16 fields with Number (11) , i don't know why!
Copy link to clipboard
Copied
also i tried to enter (17) and after long time it fills all the 16 fields with the same number (17)
Copy link to clipboard
Copied
Use something like this:
for (var i=1 ;i<=16 ;i++) this.getField("SN"+i).value = DesNum + i - 1;
Copy link to clipboard
Copied
Thanks you, but in math real life it shouid only adding DesNum + i, why this happen in javascript?, maybe because there is no way to stop the loop ?
Copy link to clipboard
Copied
The loop stops when i reaches 16...
Copy link to clipboard
Copied
You need to re-think your code. Currently you're just overwriting the value of the same field over and over again, which doesn't make much sense.
Copy link to clipboard
Copied
OK, i will try again and let you know, thanks for your help
Copy link to clipboard
Copied
now it work great and fast!
// Dialog Definition
var oDlg = {
strName: "", initialize: function(dialog) {
dialog.load({"usnm":this.strName});
},
commit: function(dialog) {
var data = dialog.store();
this.strName = data["usnm"];
},
description: {
name: "Test Dialog", elements: [ {
type: "view", elements: [
{ name: "أدخل رقم البداية", type: "static_text", },
{ item_id: "usnm", type: "edit_text", char_width: 2 },
{ type: "ok_cancel", },
]
},]
}
};
// Dialog Activation
oDlg.strName = "";
if( "ok" == app.execDialog(oDlg)) {
var DesNum = oDlg.strName;
for (var i=1 ;i<=16 ;i++){
this.getField("SN"+i).value = (i+DesNum/1)-1;
}
}
Copy link to clipboard
Copied
That looks better, although dividing by one does not really make any difference... If you're doing it to convert the DesNum string to a number then you should do it like this, instead, in line #25:
var DesNum = Number(oDlg.strName);
Copy link to clipboard
Copied
Thank you , now i know how to convert string to Number