JSFiddle - React, Tailwind, and code Playground

by Darby Rathbone

JavaScript

/* ------------------------------------------------------------------------------------------------
CHALLENGE 7

Write a function named fizzbuzz that takes in an array of numbers.

Iterate over the array using forEach to determine the output based on several rules:
  - If a number is divisible by 3, add the word "Fizz" to the output array.
  - If the number is divisible by 5, add the word "Buzz" to the output array.
  - If the number is divisible by both 3 and 5, add the phrase "Fizz Buzz" to the output array.
  - Otherwise, add the number to the output array.

Return the resulting output array.
------------------------------------------------------------------------------------------------ */

const fizzbuzz = (arr) => {
  let output = [];
  arr.forEach(function(e){
  	if (e%3==0 && e%5==0)
    	output.push('Fizz Buzz');
    else if (e%3==0)
    	output.push('Fizz');
    else if (e%5==0)
    	output.push('Buzz');
    else
    	output.push(e);
  });
  return output;
};