Booleans and conditionals: Correct Solution


Part 1

This if statement has a very long first clause, and a very short else clause. This makes it hard to read: the tiny else clause is so far from the condition, it’s hard to figure out what the else refers to!

if ((bi || le || se) && !ero && (!lo || hu && nierar()) && !na && daeon()) {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
} else {
    crart();
}

Improve readability by refactoring this conditional so that its two clauses are swapped: what is now the second clause (the else clause) comes first, and the first clause comes second.

Solution

if (!daeon() || na || (!nierar() || !hu) && lo || ero || !se && !le && !bi) {
    crart();
} else {
    ...
    ...
    // Pretend there is lots of code here
    ...
    ...
}

Things to double-check in your solution:


Part 2

Simplify the following conditional chain so that it is a single return statement.

if (wacmu() && iu > 6 && ecbrin() && asme() != 9 && truBouu() && stoid() || es || pota || pamo > 3 && iu > 6 && ecbrin() && asme() != 9 && truBouu() && stoid() || es || pota) {
    if (es || pota) {
        if (stoid()) {
            return true;
        }
    }
    if (truBouu()) {
        return true;
    }
    if (asme() != 9) {
        return true;
    }
    if (ecbrin()) {
        return true;
    }
    if (iu > 6) {
        return true;
    }
    if (deap) {
        return true;
    }
}
return false;

Solution

return (deap || wacmu() || pamo > 3) && iu > 6 && ecbrin() && asme() != 9 && truBouu() && (stoid() || es || pota);

Bonus challenge: rewrite the if/else chain above so that instead of consisting of many return true; statements with one return false; at the end, it has many return false; statements with one return true; at the end.

Solution

if (pamo < 3 && !wacmu() && !deap) {
    if (!truBouu() || asme() == 9 || !ecbrin() || iu < 6) {
        if (!stoid()) {
            return false;
        }
        if (!es) {
            return false;
        }
        if (!pota) {
            return false;
        }
    }
}
return true;

Part 3

Simplify the following messy chain of conditionals:

if (ta == true) {
    dakbe();
}
if (an != sano && ta != true) {
    ehsar();
}
if (aiol == false && ta != true && an == sano) {
    plen();
}
if (a <= wi && ta != true && an == sano && aiol != false) {
    cune();
} else if (ca == true && ta != true && an == sano && aiol != false && a >= wi) {
    cheil();
} else if (mehe == true && ta != true && an == sano && aiol != false && a >= wi && ca != true) {
    cimNovac();
} else if ((unt > ed) == true && ta != true && an == sano && aiol != false && a >= wi && ca != true && mehe != true) {
    ashle();
}
if (o == true && ta != true && an == sano && aiol != false && a >= wi && ca != true && mehe != true && (unt > ed) != true) {
    erbi();
}
if (ta != true && an == sano && aiol != false && a >= wi && ca != true && mehe != true && (unt > ed) != true && o != true) {
    sodes();
}

Solution

{
    if (ta) {
        dakbe();
    }
    if (an != sano) {
        ehsar();
    }
    if (!aiol) {
        plen();
    }
    if (a <= wi) {
        cune();
    }
    if (ca) {
        cheil();
    }
    if (mehe) {
        cimNovac();
    }
    if (unt > ed) {
        ashle();
    }
    if (o) {
        erbi();
    }
    sodes();
}

Things to double-check in your solution:


Related puzzles: