Skip to content

Instantly share code, notes, and snippets.

@greggnakamura
Last active August 29, 2015 14:05
Show Gist options
  • Save greggnakamura/2e1524ccb74dcaf6b086 to your computer and use it in GitHub Desktop.
Save greggnakamura/2e1524ccb74dcaf6b086 to your computer and use it in GitHub Desktop.
CodeEval: FizzBuzz (Solved)
// user input
var input = '3 5 10\n2 7 15\n5 10 50';
// split on newline
var inputSplitOnNewLine = input.split('\n');
// get and set 'A', 'B', and 'N' values
// call fizzBuzz function
for (var i = 0; i < inputSplitOnNewLine.length; i++) {
var inputString = inputSplitOnNewLine[i]; // current 3 numbers
var inputSplit = inputString.split(' '); // split on spaces
var firstInput = inputSplit[0]; // value 'A'
var secondInput = inputSplit[1]; // value 'B'
var thirdInput = inputSplit[2]; // value 'N'
fizzBuzz(inputString, firstInput, secondInput, thirdInput);
}
// Print out the series 1 through N replacing numbers divisible by 'A' by F,
// numbers divisible by 'B' by B and numbers divisible by both as 'FB'
function fizzBuzz(inputString, firstNum, secondNum, Count) {
var stringOutput = '';
for (var i = 1; i <= Count; i++) {
if (i % firstNum === 0 && i % secondNum === 0) {
stringOutput += 'FB ';
} else if (i % firstNum === 0) {
stringOutput += 'F ';
} else if (i % secondNum === 0) {
stringOutput += 'B ';
} else {
stringOutput += i + ' ';
}
}
// output
console.log('Input: ' + inputString + '\nOutput: ' + stringOutput);
}
//Trying to write the shortest code as possible
function fizzBuzz(firstNum, secondNum, Count) {
for(var i=0;i++<Count;console.log((i%firstNum>0?'':'Fizz')+(i%secondNum>0?i%firstNum>0?i:'':'Buzz')));
}
fizzBuzz(3, 5, 100);
function fizzBuzz(firstNum, secondNum, Count) {
for (var i = 1; i <= Count; i++) {
if (i % (firstNum * secondNum) === 0) {
console.log('FizzBuzz');
} else if (i % firstNum === 0) {
console.log('Fizz');
} else if (i % secondNum === 0) {
console.log('Buzz');
} else {
console.log(i);
}
}
}
fizzBuzz(3, 5, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment