高级前端手写代码【Q683】FizzBuzz,是否能被3或5整除

FizzBuzz,是否能被3或5整除

更多描述 输入一个整数,如果能够被3整除,则输出 Fizz

如果能够被5整除,则输出 Buzz

如果既能被3整数,又能被5整除,则输出 FizzBuzz

//=> 'fizz'
fizzbuzz(3);
 
//=> 'buzz'
fizzbuzz(5);
 
//=> 'fizzbuzz'
fizzbuzz(15);
 
//=> 7
fizzbuzz(7);

Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 702

Author 回答者: shfshanyue

function fizzbuzz(n) {
  if (n % 5 === 0 && n % 3 === 0) {
    return "fizzbuzz";
  } else if (n % 3 === 0) {
    return "fizz";
  } else if (n % 5 === 0) {
    return "buzz";
  }
  return n;
}

Author 回答者: heretic-G

function fizzbuzz(n) {
    let str = ''
    if (n % 3 === 0) {
        str += 'Fizz'
    }
    if (n % 5 ==0 0) {
        str += 'Buzz'
    }
    return str || n
}
 
 

Author 回答者: borraken

const fizzbuzz = (num)=>${num%3?'':'fizz'}${num%5?'':'buzz'}||num

Author 回答者: Yinzhuo19970516

function fizzbuzz(n) {
  if (n % 5 === 0 && n % 3 === 0) {
    return "FizzBuzz";
  } else if (n % 5 === 0) {
    return "Buzz";
  } else if (n % 3 === 0) {
    return "Fizz";
  } else {
    return n;
  }
}

Author 回答者: Hazel-Lin

function fizzbuzz(number) {
  return number % 3 === 0 && number % 5 === 0
    ? "fizzbuzz"
    : number % 5 === 0
      ? "buzz"
      : number % 3 === 0
        ? "fizz"
        : number;
}

Author 回答者: Ghaining

function fizzbuzz(n) {
  const fizz = n % 3 === 0 ? "fizz" : "";
  const buzz = n % 5 === 0 ? "buzz" : "";
  return fizz + buzz || n;
}