Skip to content
Advertisement

Fizz Buzz in PHP

I am trying to solve this :

Given the input number, n, return a string containing every value from 1 to n, inclusive. But first, you must follow the rules listed below.

Between each value, add a newline (“n”). Do not have any trailing newline after the last value.

Rules:

For each value, you must follow the following rules:

If the value is a multiple of 3, print the string “Fizz” If the value is a multiple of 5, print the string “Buzz” If the value is a multiple of both 3 & 5, print the string “FizzBuzz” Otherwise, print the integer value

Codes:

JavaScript

ERRORS:

JavaScript

Should fulfill these test :

JavaScript

Advertisement

Answer

You are almost there. You need to loop $n times, to concatenate the strings/numbers together with newline and to return the final string:

JavaScript

Output for first 15 numbers (because 15 is the first one you can divide on both 3 and 5):

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement