Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to simplify if statement with 3 arguments

Need to simplify this function; all inputs are 0/1:

export const handleStepComplete = (userSave: number, concur: number, signature: number) => {
    if (userSave === 0 && concur === 0 && signature === 0) {
        return {completed: false, active: true, error: false};
    }
    if ((userSave === 1 && concur === 0 && signature === 0) ||
        (userSave === 1 && concur === 1 && signature === 0)) {
        return {completed: true, active: true, error: false};
    }
    if (userSave === 1 && concur === 0 && signature === 1) {
        return {completed: false, active: false, error: true};
    }
};

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Just a draft, but you could structure it something like this.

export const handleStepComplete = (userSave: number, concur: number, signature: number) => {
    return {
        completed: (userSave && concur && signature),
        active: (userSave && !signature && !concur),
        error: (signature && !concur)
    }
};

This does not handle all cases, and neither does your code. The logic is probably not correct too, but you get the point and can build on it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading