WDI Fundamentals

WDI Fundamentals Unit 8

Your Turn Your Turn

Conditionals Exercise


  1. Write the code to perform the actions listed below in the "JavaScript" panel in the JS Bin editor.
  2. If you don't see the JS Bin below, please refresh the page.
  3. Click "Run" to run the code in the "Console" panel.
  4. After clicking "Run", to check variable values, type the variable name in the "Console" panel on the right and hit enter/return.

JS Bin on jsbin.com

Let's get started!

This exercise may be familiar to some of you — your job is to write some code that, depending on the value of x, assigns one of several values to result:

  1. If x is evenly divisible by both 3 and 5 (for example, 15 or 30), set result to "fizzbuzz".
  2. Otherwise if x is evenly divisible by 3 (for example, 3, 6, or 9), set result to "fizz".
  3. Otherwise if x is evenly divisible by 5 (for example, 5 or 10), set result to "buzz".
  4. If x is not evenly divisible by either 3 or 5 (for example, 7), set result to x.

Don't remember how to determine if something divides evenly? Review the modulus (%) operator.

To test your code, set a value for x in the "JavaScript" panel and click "Run" in the "Console" panel. Then type the variable name result into the "Console" panel and hit the enter/return key. Did you get the result you expected? Try out several different values for x, just to be sure.

Stuck? Check out the solutions to see what you can do.

On to the next lesson!