Home > Software engineering >  Javascript displaying particular values from multiple possibilities
Javascript displaying particular values from multiple possibilities

Time:06-01

I have a list of projects with different statuses ( Red, Yellow, Green and None) Those ones are assigned to particular program. I need to assign to the variable the most critical colour assigned to the program. If there is at least one red I need to display red( no matter if there are other yellows or greens) If the most critical one is yellow I need to display Yellow, not matter if there are greens. If there are only greens I need to display Green. If there is none assigned I need to display none. Below code is OK for 3 of them, I do not know how to exactly add None to it. All values are strings.

Regards Piotr

function getProjectStatusProgram(program){
var status = '';
var grPrgTask = new GlideRecord('project');
grPrgTask.addQuery('u_program', program);
grPrgTask.query();
while (grPrgTask.next()){
    if (grPrgTask.overall_health == 'red'){
        status = "red";
        break;
    } else if (grPrgTask.overall_health == 'yellow'){
        status = 'yellow';          
    }           
}
if(status =='red' || status =='yellow')
return status;
else
return 'green';

}

CodePudding user response:

Theoretical example, since I do not have all the code in question.

/***
Returns a string with following task status priority:
 1: "red": if atleast one task has status "red"
 2: "yellow": if atleast one task has status "yellow"
 3: "none": if atleast one task has status "none"
 4: "green": if no task has either status "red", "yellow" or "none"
**/
function getProjectStatusProgram(program){
    //REM: Set default status to "green" due to "If there are only greens I need to display Green"
    var status = 'green';

    var grPrgTask = new GlideRecord('project');
    grPrgTask.addQuery('u_program', program);
    grPrgTask.query();

    while(grPrgTask.next()){
        //REM: "If there is at least one red I need to display red( no matter if there are other yellows or greens)"
        if(grPrgTask.overall_health == 'red'){
            status = "red";
            break;
        };

        //REM: "If the most critical one is yellow I need to display Yellow, not matter if there are greens."
        if(grPrgTask.overall_health == 'yellow'){
            status = 'yellow'
        }
        //REM: If current status is still "green" and this tasks status is "none" -> "none"
        //REM: Since it is only "green" if all are "green"
        else if(status == 'green' && grPrgTask.overall_health == 'none'){
            status = 'none'
        }
    };

    //REM: Just return the status
    return status
}
  • Related