DEV Community

Discussion on: Algorithm Practice: Fizzbuzz problem

Collapse
 
cyberhck profile image
Nishchal Gautam

don't ever use else,

const getText = i => {
    if (i % 3 === 0 && i % 5 === 0) {
        return 'fizzbuzz';
    }
    if (i % 3 === 0) {
        return 'fizz';
    }
    if (i % 5 === 0) {
       return 'buzz';
    }
    return i;
}
Enter fullscreen mode Exit fullscreen mode

And call this from your iterating code, and print that out,

there's a concept of cognitive load, if you have else on your code, your brain needs to track what's going on, and it'll lead to a very nested sloppy code,

try getting rid of else, keep edgecase on if and happy path on a straight line, your code will be readable. (line of sight)

And don't do this: image