Highlight first 2 greatest number  (Lu 232 fois) Partager ce sujet Imprimer

1 B


Rakesh Kumar  13 septembre 2021 : 09:02


Sir,


I have say 30 text fields in which user can input numbers, or text or leave it blank


now i want that when the user clicks a button then the first 3 greatest number is highlighted / changed color ignoring the blanks or the text values.


Also, last 3 smallest number to be highlighted / changed color.


Thanks

IP archivée


bebarth  13 septembre 2021 : 21:45

Hi,
Please try that for the 3 smallest numbers:
var theNumbers=[];
for (var i=0; i<30; i++) {
    var f=this.getField("theField."+i);
    f.strokeColor=color.transparent;
    if (!isNaN(f.value) && f.value!="") theNumbers.push([i,f.value]);
}
// Ascending sort
theNumbers.sort(function(a, b){return a[1]-b[1]});
// reverse order sort
// theNumbers.sort(function(a, b){return b[1]-a[1]});
if (theNumbers.length) {
    for (var i=0; i<3; i++) {
        try {
            this.getField("theField."+theNumbers[i][0]).strokeColor=color.red;
        } catch(e) {}
    }
}
For the 3 greatest numbers, use the reverse order sort script line.
@+
 :bonjour:

IP archivée


Rakesh Kumar  14 septembre 2021 : 03:06

Sir,


The script is not working as intended.... this script is highlighting the first 3 entry of an array after sorting it in ascending ..... however i want the first 3 smallest number in the array to be highlighted after sorting... ie. removing the duplicates in the array.


for ex -   original array = [8,3,4,2,2,2,3,5,6,4]   
            after sorting the array will be  = [2,2,2,3,3,4,4,5,6,8]
          so current script is highlighting  2,2,2   but my requirement is to highlight every entry which equal to 2,3,4   .


Hope i was able to explain.
Same for the greatest one.  it should highlight first 3 greatest number after sorting in descending order avoiding duplicates.


thanks.



IP archivée


bebarth  14 septembre 2021 : 09:11

OK, I understood!
I will have a look later in the day.
@+
 :bonjour:

IP archivée


Rakesh Kumar  14 septembre 2021 : 09:21

@Bebarth


please check your email, the issue has been solved and i have sent you the working pdf file on your email.. plz check and let me to know if it can be improved.


thanks

IP archivée


bebarth  14 septembre 2021 : 20:50

Hi,I don't have a lot of things to tell you!
I didn't know your UArr function, so I would have written the script like this:
var arr=[];
for (var i=0; i<30; i++) {
    var f=this.getField("theField." + i);
    if (!isNaN(f.value) && f.value!="") arr.push(f.value);
}
var dv=arr.sort(function(a, b){return b - a});
function uniqueValue(anArray) {
    return anArray.filter((val, ind) => anArray.indexOf(val)===ind);
}
var myarr=uniqueValue(dv);
for (var j=0; j<30; j++){
    var g=this.getField("theField." + j);
    if (g.value == myarr[0]) g.fillColor=color.red;
    else if (g.value == myarr[1]) g.fillColor=color.green;
    else if (g.value == myarr[2]) g.fillColor=color.magenta;
    else if (g.value == myarr[3]) g.fillColor=color.yellow;
    else {g.fillColor=color.transparent;}
}
this.getField("Text1").value="Greatest 4 unique numbers are : " + "\n\n" +"   "+ myarr[0] + "\n"+"   "+ myarr[1] + "\n"+"   "+ myarr[2] + "\n"+"   "+ myarr[3];
I allow myself to share both files.
@+
 :bonjour:

IP archivée


Rakesh Kumar  15 septembre 2021 : 06:04

Sir,


Somehow your script is not working and throwing an error in console ,


[size=78%]ReferenceError: val is not defined[/size]
8:Field:Mouse Up




Thanks

IP archivée


bebarth  15 septembre 2021 : 15:39

Hi,
That's working fine for me! I usually check the script works fine before sharing...
Attached is a new version checking the numbers in both orders.
@+
 :bonjour:

IP archivée


Rakesh Kumar  17 septembre 2021 : 06:53

Thanks for the updated version... but this also not working.............


I will send you video of the same, if that would help in sorting out the issue.


Thanks

IP archivée


bebarth  17 septembre 2021 : 19:35

bonjour,
Je vois qu'il y a eu 4 téléchargements.
Quelqu'un d'autre pourrait-il me dire si le script fonctionne ou pas pour elle/lui ?

Hi,
I see 4 downloads have been done.
Could someone else tell me if the script works fine for her/him or doesn't work?

@+
 :bonjour:

IP archivée


Rakesh Kumar  18 septembre 2021 : 04:26

Sir,


Attached is the video link  from downloading the file to testing it .. and error which console shows.


https://www.dropbox.com/s/pmpgu1bodg0d7f6/issue.avi?dl=0


Please have a look. Hope this would help to solve the issue.


Thanks

IP archivée


bebarth  18 septembre 2021 : 19:06

Hi,
I don't understand why the function doesn't work for you. I did a test on both PC and Mac and the file works fine with both OS.
As your UArr fonction works fine too, you can you it for the file.
I placed the script in an other funtion. That avoids to get it twice in the buttons:
// Document Scriprs
function UArr(a){
    var newArr=[];
    for (var i=0; i < a.length; i++) {
        if (newArr.indexOf(a[i])===-1) newArr.push(a[i]);
    }
    return newArr;
}
function greatORsmall(wichOne) {
    var arr=[];
    for (var i=0; i<30; i++) {
        var f=this.getField("theField."+i);
        if (!isNaN(f.value) && f.value!="") arr.push(f.value);
    }
    if (wichOne=="greatest") {
        var dv=arr.sort(function(a, b){return b-a});
    } else {
        var dv=arr.sort(function(a, b){return a-b});
    }
    var myarr=UArr(dv);
    for (var i=0; i<30; i++){
        var f=this.getField("theField."+i);
        f.fillColor=color.transparent;
        if (f.value == myarr[0]) f.fillColor=color.red;
        else if (f.value==myarr[1]) f.fillColor=color.green;
        else if (f.value==myarr[2]) f.fillColor=color.magenta;
        else if (f.value==myarr[3]) f.fillColor=color.yellow;
    }
    this.getField("Text1").value="";
    if (myarr.length!=0) {
        if (myarr.length>4) var nbNumbers=4;
        else var nbNumbers=myarr.length;
        if (nbNumbers==1) this.getField("Text1").value="The unique number is:";
        else this.getField("Text1").value="The "+nbNumbers+" "+wichOne+" unique numbers are:";
        for (var i=0; i<nbNumbers; i++) {
            try {
                this.getField("Text1").value+="\n\u2022 "+myarr[i];
            } catch(e) {}
        }
    }
}


// Mouse up action scripts
// greatest and smallest buttons
greatORsmall(event.target.buttonGetCaption());

// reset button
for (var i=0; i<30; i++) {
    var f=this.getField("theField."+i);
    f.value="";
    f.fillColor=color.transparent;
Please try the new file and let me know.
@+
 :bonjour:

IP archivée


Rakesh Kumar  19 septembre 2021 : 05:03

Thanks, it is now working for me.


also, i have sent 2 or 3 issue on your email.. plz check and let me know if its possible.


Thanks again.

IP archivée