Programming - Fizzbuzz

by Zacc206

JavaScript

console.clear();

var fb = (function(){
	
  function fizz(){
    console.log("Fizz");
  }

  function buzz(){
    console.log("Buzz");
  }

  function fizzBuzz(){
    console.log("FizzBuzz");
  }
 
  var option = {
  	// Most basic if/else conditional but logic is not semantically as accurate as it could be
    // Assumes you can be in only one of four states
  	one: function(){
      for(var i = 1; i <= 100; i++){
        if(i % 3 === 0 && i % 5 === 0){
          fizzBuzz();
        } else if(i % 3 === 0){
          fizz();
        } else if(i % 5 === 0){
          buzz();
        } else {
          console.log(i);
        }
      }
    },
    // Removed else conditionals to use only if statements, semantically communicating that 
    // conditions are not mutually exclusive
    two: function(){
    	for(var i = 1; i <= 100; i++){
      	var result= '';
        if(i % 3 === 0){
          result += 'Fizz';
  			}
    		if(i % 5 === 0){
        	result += 'Buzz';
        }
        if(i % 5 !== 0 && i % 3 !== 0){
        	result = i;
        }
				console.log(result);
			}
		},
    // Almost the same as variant two above, except that only one condition needs to be
    // evaluated at the end to determine if the result is not divisible by 3 or 5
    three: function(){
    	for(var i = 1; i <= 100; i++){
      	var result= '';
        if(i % 3 === 0){
          result += 'Fizz';
  			}
    		if(i % 5 === 0){
        	result += 'Buzz';
        }
        if(result === ''){
        	result = i;
        }
				console.log(result);
			}
		}
	}
  
  return {
  	option: option
  }
})();

//fb.option.one();
//fb.option.two();
fb.option.three();