Skip to content

Instantly share code, notes, and snippets.

@luiznasciment0
Created December 16, 2021 20:21
Show Gist options
  • Save luiznasciment0/97273711afd6c679ebd306abfdec7bef to your computer and use it in GitHub Desktop.
Save luiznasciment0/97273711afd6c679ebd306abfdec7bef to your computer and use it in GitHub Desktop.
how I usually solve fiz buzz hacker rank challenge
/*
* Complete the 'fizzBuzz' function below.
*
* The function accepts INTEGER n as parameter.
*/
function fizzBuzz(n) {
for (let i = 1; i <= n; i++) {
// const isMultipleOfThreeAndFive = i % 3 === 0 && i % 5 === 0;
const isMultipleOfThreeOnly = i % 3 === 0 && i % 5 !== 0;
const isMultipleOfFiveOnly = i % 5 === 0 && i % 3 !== 0;
const isNotAMultipleOfThreeOrFive = i % 3 !== 0 && i % 5 !== 0;
if (isNotAMultipleOfThreeOrFive) {
console.log(i);
} else if (isMultipleOfFiveOnly) {
console.log('Buzz');
} else if (isMultipleOfThreeOnly) {
console.log('Fizz');
} else {
console.log('FizzBuzz');
}
}
}
@guivictorr
Copy link

my implementation

const fizzBuzz = (n) => {
  if (n % 3 === 0 && n % 5 === 0) {
    return 'FizzBuzz'
  } else if (n % 5 === 0) {
    return 'Buzz'
  } else if (n % 3 === 0) {
    return 'Fizz'
  }

  return n
}

const max = 100

for (let i = 1; i <= max; i++) {
  console.log(fizzBuzz(i))
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment