Created
January 5, 2017 16:01
-
-
Save latema/29ad477ccae3095fbae720a8b1ccebfc to your computer and use it in GitHub Desktop.
fizzbuzz grand lux v1.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Fizzbuzz solved by using the rules of mathematics and also Stackoverflow | |
* For 5 players (non-configurable) with autorun for 100 items each | |
* v1.0 | |
* | |
* Copyright 2017 Giosg.com Oy | |
* All rights reserved. | |
*/ | |
// http://www.aaamath.com/div66_x3.htm | |
// http://stackoverflow.com/questions/9138064/sum-of-the-digits-of-a-number-javascript | |
function generatePlayers(numberOfPlayers) { | |
const i = numberOfPlayers; | |
index = 0; | |
let a = arrayOfPlayers = []; | |
while (i > 0) { | |
index = 0; | |
numberOfPlayers--; | |
arrayOfPlayers.push('player' + index); | |
index++; | |
} | |
return arrayOfPlayers; | |
} | |
function printTheNumberOrFizzOrBuzz(n) { | |
let number = n; | |
// find returns the sum of digits of 2nd parameter to "theNumber()" (base10) | |
const isDivisibleBy3 = find(theNumber (10, n)) % 3; | |
const isDivisibleBy5 = find(theNumber (10, n)) % 5; | |
if (!isDivisibleBy3 && !isDivisibleBy5) { | |
console.log(n); | |
} else if (isDivisibleBy3) { | |
console.log('fizz'); | |
} else if (isDivisibleBy5) { | |
console.log('buzz'); | |
} | |
} | |
// "I am getting the correct answer, I just don't fully understand the 2nd function" (slashdot author) | |
// (sufficient proof it works) | |
var theNumber = function digitAdd (base, exponent) { | |
var number = 1; | |
for (i=0; i < exponent; i++) { | |
var number = base * number; | |
} | |
return number | |
} | |
function find(theNumber) { | |
var sum=0; | |
parseInt(theNumber); | |
while(theNumber>0) | |
{ | |
sum=sum+theNumber%10; | |
theNumber=Math.floor(theNumber/10); | |
} | |
return sum; | |
} | |
const players = generatePlayers(5); | |
players.forEach((player) => { | |
console.log('player whose turn now!: ', player) | |
for (int i = 0; i < 99; i++) { | |
printTheNumberOrFizzOrBuzz(i); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment