Skip to content

Instantly share code, notes, and snippets.

@FrancoB411
Created March 1, 2012 00:01
Show Gist options
  • Select an option

  • Save FrancoB411/1945844 to your computer and use it in GitHub Desktop.

Select an option

Save FrancoB411/1945844 to your computer and use it in GitHub Desktop.
FizzBuzzPlus CodeYear Challenge
//FizzBuzzPlus Challenge from CodeYear
var FizzBuzzPlus = { //create an object called FizzBuzzPlus
isFizzBuzzie: function(num) { // Create function isFizzBuzzie with one argument: an integer
if(num % 3 === 0 && num % 5 === 0) { // return false if the provided integer is a multiple of both 3 and 15
return false;
}
else if ( num % 3 === 0 || num % 5 === 0) { //return true if the provided integer is a multiple of 3 or 15 (not both)
return true; // I know this could be less lines, but this seemed easier to read.
}
else { // otherwise it returns false
return false; // returns: true or false - boolean
}
},
getFizzBuzzSum: function(max) { //takes a maximum integer for search.
var sum = 0;
for(i=0; i<max; i++) {
if(this.isFizzBuzzie(i) == true) { //sums all number which are multiples of 3 or 5 but not both, and less than max.
sum += i;
}
}
//returns: integer sum of all multiples of 3 or 5 but not both, less than max.
return sum;
},
getFizzBuzzCount: function(max){ //same as above, but gets the count rather than the sum.
var count = 0;
for(i=0; i<max; i++) {
if(this.isFizzBuzzie(i) === true) {
count++;
}
}
return count;
},
getFizzBuzzAverage: function(val) { //returns average of FizzBuzzie numbers below the supplied value.
var count = this.getFizzBuzzCount(val);
var sum = this.getFizzBuzzSum(val);
var average = sum/count;
return average;
}
};
console.log(FizzBuzzPlus.getFizzBuzzAverage(100)); //test code, prints average of FizzBuzzie numbers below supplied value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment