Comparison between two string fields data

kumar37
Kilo Contributor

Hi All,

I Have a requirement to compare the two string fields data and the difference need to be populated in the 3rd field.

1st field name:  user input 

2nd field name: existing data

3rd field name: missing data:

1 st field data :  1,2,3,4,5,6

2nd field data : 2,5,6

 i need to compare the data between 1st field and 2nd field and difference should be populate in 3rd field.

3rd field data: 1,3,4

 

Thanks in advance,

kumar

 

1 ACCEPTED SOLUTION

I created an item in my dev instance and re-did the code.  Try this you will just need to update the variable names as I created them as a, b, and c.

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	//Type appropriate comment here, and begin script below
	var a = g_form.getValue('a').toString().split(',');
	var b = g_form.getValue('b').toString();
	getDifference(a, b);
}
function getDifference(a, b)
{
//	var j = 0;
	var result = [];
	for (var i = 0; i < a.length; i++){
		if (b.toString().indexOf(a[i]) < 0){
			result.push(a[i]);
		}
	}
	
	g_form.setValue('c', result);
}

View solution in original post

30 REPLIES 30

Brian Lancaster
Tera Sage

You could try something like this.

//assuming "b" contains a subsequence containing 
//all of the letters in "a" in the same order
function getDifference(a, b)
{
    var i = 0;
    var j = 0;
    var result = "";

    while (j < b.length)
    {
        if (a[i] != b[j] || i == a.length)
            result += b[j];
        else
            i++;
        j++;
    }
    return result;
}

console.log(getDifference("test fly", "test xy flry"));

Hi bricast,

thanks for your time.

i have tried to use this code with my parameters. but i am fully confused. can you please help me to write the on change script with the mentioned variables in the question.

 

Thanks,

kumar

This is assuming you are doing an on change of 1st field.

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	//Type appropriate comment here, and begin script below
	var a = newValue;
	var b = g_form.getValue('secondndFiled');
	getDifference(a, b);
}
function getDifference(a, b)
{
	var i = 0;
	var j = 0;
	var result = "";
	
	while (j < b.length)
		{
		if (a[i] != b[j] || i == a.length)
			result += b[j];
		else
			i++;
		j++;
	}
	return result;
}

Sorry the return result should be replaced with g_form.setValue('3rd field', result);