r/ProgrammerHumor • • Jan 16 '14

[deleted by user]

[removed]

1.3k Upvotes

446 comments sorted by

View all comments

1

u/balkonkind Jan 17 '14

needs more design patterns:

var textMap = [
    { division: 3, text: 'Fizz' },
    { division: 5, text: 'Buzz' },
];

for (var number = 1; number <= 100; number++) {
    
    var isDividable = false;
    for (var i = 0; i < textMap.length; i++) {
        if (number % textMap[i].division == 0) {
            isDividable = true;
            break;
        }    
    }
    
    if (isDividable) {
        var consoleText = '';
        for (var i = 0; i < textMap.length; i++) {
            if (number % textMap[i].division == 0) {
                consoleText += textMap[i].text;
            }
        }
        console.log(consoleText);
    } else {
        console.log(number);   
    }

}