Skip to main content
Participant
Jan 10, 2023 6:49 AM UTC
Answered

combining two variables

Hi,

i have this code that revrites multiple textfields into one. i need to change variable i into a1, a2 or a3 depending ond the value of i.

//final text should look like this

//text10: loan1

//text20: loan2

//text30: loan3

CODE:

a1=("text10");
a2=("text20");
a3=("text30");


var text = "";
for (var i=1; i<=3; i++) {
var s = this.getField("loans"+i).valueAsString;
if (s!="") text+=""+ i +  ": " + s + "\r";
}
event.value = text;

This topic is closed to new replies. Start a new post to keep the conversation going.
Bernd Alheit
Bernd AlheitCorrect answer
Community Expert
Jan 10, 2023 2:49 PM UTC

Try this:

a1=("text10");
a2=("text20");
a3=("text30");

var text = "";
for (var i=1; i<=3; i++) {
  var s = this.getField("loans"+i).valueAsString;
  if (s!="") {
    var txt;
    if (i == 1) txt = a1;
    else if (i == 2) txt = a2;
    else txt = a3;
    text+=""+ txt +  ": " + s + "\r";
  }
}
event.value = text;

6 replies

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
Jan 10, 2023 2:49 PM UTC

Try this:

a1=("text10");
a2=("text20");
a3=("text30");

var text = "";
for (var i=1; i<=3; i++) {
  var s = this.getField("loans"+i).valueAsString;
  if (s!="") {
    var txt;
    if (i == 1) txt = a1;
    else if (i == 2) txt = a2;
    else txt = a3;
    text+=""+ txt +  ": " + s + "\r";
  }
}
event.value = text;
Translate
Participant
Jan 10, 2023 3:23 PM UTC

thanks this one works like a charm.  if variable i will be for example from 1 to 20 the code will build up.. but it should work as well, thanks.

Translate
try67
Community Expert
Community Expert
Jan 10, 2023 9:23 AM UTC

You should put the names of the fields in an array, and then iterate over it in your for-loop.

Translate
Participant
Jan 10, 2023 12:25 PM UTC

do you mean like this? its not working, but i have never worked with arrays before 😞


var fields = ["jedna","dva","tři"];

var text = "";
for (var i=1; i<=3; i++) {
var s = this.getField("PUJCKY"+i).valueAsString;
if (s!="") text+=""+ fields[i].value + ": " + s + "\r";
}
event.value = text;

Translate
try67
Community Expert
Community Expert
Jan 10, 2023 12:32 PM UTC

You need to better explain how this should work. Are there two sets of fields, or just one?

 

Translate