0

I got the code below from a Javascript Library:

function func_1()
{
    if ( null == decimal ) 
    {
        return dPR || (dpr(2) ? 2 : dpr(1.5) ? 1.5 : dpr(1) ? 1 : 0);
    }
}

I'm particularly interested in the code inside the if statement.

How else can I write this so that it's easier to read?! As it is now I can't understand it.

Thanks.

2
  • Give meaningful names to the constants and don't use some random acronyms. Commented Nov 14, 2012 at 23:50
  • Post all your code. I believe it is missing some definitions. Commented Nov 14, 2012 at 23:51

3 Answers 3

4

it's equivalent to:

if (dPR) {
    return dPR;
} else {
    if (dpr(2)) {
        return 2;
    } else {
        if (dpr(1.5)) {
            return 1.5;
        } else {
            if (dpr(1)) {
                return 1;
            } else {
                return 0;
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
if (dPr) {
    return dPr;
} 
else {
    if (dpr(2))
        return 2
    else if (dpr(1.5))
        return 1.5;
    else if (dpr(1))
        return 1;
    else 
        return 0;
}

1 Comment

Thanks David. This is actually closer to what my brain wants to process as regards the question.
0
if ( decimal == null ) {

    if ( dPR ) {

        return dPR;

    } else {

        if ( dpr(2) ) {

            return 2;

        } else if ( dpr(1.5) ) {

            return 1.5;

        } else if ( dpr(1) ) {

            return 1;

        } else return 0;

    }

}

1 Comment

This is not correct, you do have a double else-branch in your code.

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.