JavaScript "Challenger" Site

I’ve not managed to check this site out yet, but it’s an interesting idea. This could come in handy for those doing the JavaScript Programming for the Web course on the May Learning initiative.

http://rileyjshaw.com/challenger/

One for @Adam maybe?

Unfortunately the site doesn’t work properly.

I decided to try one question.

1 of 1fizzbuzz in a tweet

Write a function called fizzbuzz that accepts a single argument n. fizzbuzz should return “Fizz” if n is a multiple of 3, “Buzz” if n is a multiple of 5, “FizzBuzz” if n is a multiple of both 3 and 5, and n if n is a multiple of neither 3 nor 5.

My answer (which gives the right results but apparently doesn’t meet the criteria):

function fizzbuzz (n) {
  return (n%3)? ((n%5)? n:'Buzz'): (n%5)? 'Fizz' : 'Fizzbuzz';
}

The page displays the following at the right of the input area showing green background for a pass and red background for a fail (but incorrectly shows the third item in red.

Program must be valid JavaScript.
Program must fit within a tweet (max 140 characters).
Program must correctly implement a fizzbuzz function.

It seems that the site is incapable of recogising that the code used performs the task as specified. Presumably the site tests for a specific answer rather than testing if the entered code actually does what is required. There are plenty of longer alternatives to this code that would still allow it to fit in the 140 character maximum so presumably the site authors are expecting one of those longer answers

Hey felgall,

fizzbuzz should return “Fizz” if n is a multiple of 3, “Buzz” if n is a multiple of 5, “FizzBuzz” if n is a multiple of both 3 and 5, and n if n is a multiple of neither 3 nor 5.

Your solution fails because it returns ‘Fizzbuzz’ rather than ‘FizzBuzz’.

thanks - stupid typo

Thanks mate, will be in today’s. Very interesting!

The FizzBuzz challenge can also be solved as follows:

❯ cat fizzbuzz.js

function fizzbuzz (n) {
  var word = ''

  if (n % 3 === 0) word += 'Fizz'
  if (n % 5 === 0) word += 'Buzz'

  return word || n
}

It is correct, easy to follow, and fits in a tweet.

❯ wc -c fizzbuzz.js
     131 fizzbuzz.js

That solution (that has one and a half times as much code as is needed) is probably what the question is looking for as the answer.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.