Fizzbuzz function Javascript

I came across this test on codeacademy where you have to write a function that returns “fizz” for a number that is divisible by 3, “buzz” for a number that is divisible by 5 and “fizzbuzz” for a number that is divisible by both 3 and 5. Unfortunately free codeacademy doesn’t really have that much support when one is stuck na google pia haisaidii sana. Hii kitu inaandikwa aje?

very simple

         static void runtest()
        {
            for (int i = 1; i <= 25; i++)
            {
                string szFizzBuzzTestResult = fizzBuzzTest(i);
                if (!(String.IsNullOrEmpty(szFizzBuzzTestResult)))
                    Console.WriteLine(szFizzBuzzTestResult);                    
            }
        }
        static string fizzBuzzTest(int i)
        {
            string szresult = string.Empty;
            if (i % 3 == 0 && i % 5 == 0)
                szresult= (string.Format("{0} = fizzbuzz", i));
            else if (i % 3 == 0)
                szresult= (string.Format("{0} = fizz", i));
            else if (i % 5 == 0)
                szresult=(string.Format("{0} = buzz", i));
            return szresult;
        }
4 Likes

Indeed very simple

Why do I sense some sarcasm, it’s basic modulo arithmetic

:slight_smile:

1 Like

For beginners like me, sio rahisi hivyo. Ukaona vile nilifanya yangu

How did you do it. Post hapa nione.

ungeweka ya user kuweka input yake…But wewe this is java…
not javascript

var x = prompt(“Please enter a number of your choice”);
if (x % 3 == 0)
{
console.log(“fizz”);
}
else if (x% 5 == 0);

{
console.log(“buzz”);
}
else if (x % 15 == 0) {
console.log(“fizzbuzz”);
}
else {
console.log(“number not divisible by both 5 and 3”)
};

which language??

That’s not Java, it’s C#. And what matters Is the logic, modulo works for all programming environments

And by the way your sample code has a bug. Biggest dummy mistake. No offense :slight_smile:

function fizzBuzz() {
var numbers = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
for(var i = 0; i<=20; i++) {
  var fizz = numbers[i]%3===0;
  var buzz = numbers[i]%5===0;
  var fizzBuzz = nmbers[i]%5===0 &&numbers[i]%3===0;
  if(fizzBuzz){
    console.log("FizzBuzz");
  }
  else if(fizz) {
    console.log("Fizz");
  }
  else if(buzz){
    console.log("Buzz");
  }
  else {
    console.log(numbers[i]);
  }
}

}

Javascript and Python

Here is cpp

#include <iostream>

using namespace std;

string fizzbuzz(int testValue)
{
    if ((testValue % 3 == 0) && (testValue % 5 == 0))
        return "fizzbuzz";
      
    if (testValue % 3 == 0)
        return "fizz";
  
    if (testValue % 5 == 0)
        return "buzz";
      
    return "";
}

int main()
{
  
   for (int i = 0; i < 100; i++)
   {
       cout << i << "=" << fizzbuzz(i) << "\n";
   }
  
   return 0;
}

@shadow-walker This is how I would’ve done it.
PS: 0%n == 0 every time.

#include <iostream>

using std::string;
using std::cout;
using std::endl;

string fizzBuzz(int testValue) {
    if ( testValue == 0 )
        return "";

    string result;
    if ( testValue % 15 == 0 ) {
        result = "fizzbuzz";
    }
    else if ( testValue % 3 == 0) {
        result = "fizz";
    }
    else if (testValue % 5 == 0) {
        result = "buzz";
    }

    return result;
}

int main(void) {
   for (int i = 0; i < 100; i++) {
       cout << i << "=" << fizzBuzz(i) << endl;
   }
   return 0;
}
1 Like

Javascript:

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