1

How should I write following javascript function in a better way?

  function showIssueCount(issueSummary){
    if(issueSummary["CRITICAL"] >= 1){
      return {value:issueSummary["CRITICAL"], class:"CRITICAL" };
    }else if(issueSummary["MAJOR"] >= 1 ){
      return {value:issueSummary["MAJOR"], class:"MAJOR" };
    }else if (issueSummary["MINOR"] >= 1){
      return {value:issueSummary["MINOR"], class:"MINOR" };
    }else{
      return {vaue:0, class:""}
    }
  }
1
  • the issueSummary object will be of following type. {"CRITICAL": 1, "MAJOR": 1, "MINOR":1} Commented Dec 10, 2015 at 8:42

1 Answer 1

2

something like this

function showIssueCount( obj )
{
    var priority = [ 'CRITICAL', 'MAJOR', 'MINOR' ];
    for( var counter = 0; counter < priority.length; counter++ )
    {
        if ( obj[ priority[ counter ] ] >= 1 )
        {
            return { value : obj[ priority[ counter ] ], class: priority[ counter ] };
        }       
    }
    return {value:0, class:""}
}
Sign up to request clarification or add additional context in comments.

3 Comments

You made a little mistake, the return is an object, you used [ ].
@ShanShan thanks for pointing it without downvoting :) . made the changes
No problem, I was about to post the same code but you were faster so upvote it was :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.