|

FizzBuzz CodeYear Fun #schoolstech

With todays announcement from Mr Gove it seems a good point to reflect on my first steps on a year long coding journey. I spent my Tuesday evening completing the first week of lessons on the brilliant CodeYear. It took me about an hour and a half and was a great little introduction to Javascript. First week covers defining variables, basic arithmetic, and moves on to if/then/else/while statements.

It’s an interesting learning model, there are hints at each stage and I didn’t find myself stuck on too many occasions. If you teach ICT or Maths then I’d thoroughly recommend you take a look at it. It was quite a challenge and I’ll be interested to see how far students could get without a teacher to help them. If they do get stuck, some good Googling skills would help them find a way forward pretty quickly. It’s not a replacement for a skilled teacher thought, but that’s a conversation for another post!

The final bonus challenge is to write a FizzBuzz program that writes out a set of consecutive numbers, but replaces multiples of 3 with “Fizz” and multiple of 5 with “Buzz” and of course, multiples of both with “FizzBuzz”. It’s a great little challenge that the Maths teacher in me loved!

I’ve been encouraging staff and students at school to join me on this journey so it’ll be interesting to see how many are up for the challenge.

Here’s my final FizzBuzz code in case you’re interested or stuck:

// Ask user how far we should Fizz Buzz for
var Total = prompt("How far shall we fizz buzz?");

// for the numbers 1 through to Total,
for (i=1; i<=Total; i++) { 

  // if the number is divisible by 3, write "Fizz"
  if ( i % 3 === 0 ) { 
    // unless the number is also divisible by 5, then write "FizzBuzz"
    if ( i % 5 === 0 ) {
    console.log("FizzBuzz");
    }
      else 
        console.log("Fizz");
  }

  // if the number is divisible by 5, write "Buzz"
  else if (i % 5 === 0 ){
    console.log("Buzz");
  }

  // otherwise, write just the number
  else {
    console.log(i);
  }
}

Has anyone written this in a neater, purer way? I’d love to see it if you have.

EDIT:

I have to include this, a solution in a tweet by Martyn Colliver:

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *