Tuesday — Week Five

Today we officially started Javascript. I’ve been simultaneously looking forward to, and dreading this day for quite some time. I have a tiny, tiny bit of experience with Javascript, and it’s used on a crazy amount of websites (like 92% or something!), but it’s also got lots of brackets and semicolons which is scary!

It was funny, the person who wrote some of our materials obviously despised Javascript at the start!

Hilarious desscription

We really hopped straight into it — we did the Fizzbuzz challenge which we’d previously done in Ruby (though the solution is a bit different).

Which one do you think looks less intimidating?

Ruby Tests

require “fizzbuzz”
describe “fizzbuzz” do
it “tests if a number is divisible by 3” do
expect(divisible_by_3?(3)).to eq true
end
it “tests if a number is not divisible by 3” do
expect(divisible_by_3?(1)).to eq false
end
it “tests if a number is divisible by 5” do
expect(divisible_by_5?(5)).to eq true
end
it “tests if a number is not divisible by 5” do
expect(divisible_by_5?(1)).to eq false
end
it “tests if a number is divisible by both 3 and 5” do
expect(divisible_by_3_and_5?(15)).to eq true
end
it “tests if a number is not divisible by both 3 and 5” do
expect(divisible_by_3_and_5?(1)).to eq false
end
it “checks of divisibility of any numbers” do
expect(divisible_by?(1, 1)).to eq true
expect(divisible_by?(4, 2)).to eq true
expect(divisible_by?(6, 3)). to eq true
end
it “checks if numbers aren’t divisible by each other” do
expect(divisible_by?(5, 2)).to eq false
expect(divisible_by?(6, 4)). to eq false
expect(divisible_by?(7, 2)). to eq false
end
it “returns fizz if divisible by 3” do
expect(fizzbuzz(3)).to eq “fizz”
end
it “returns buzz if divisible by 5” do
expect(fizzbuzz(5)).to eq “buzz”
end
it “returns fizzbuzz if divisible by 3 and 5” do
expect(fizzbuzz(15)). to eq “fizzbuzz”
end
it “returns a number if that number is not divisible by 3 or 5” do
expect(fizzbuzz(1)).to eq 1
end
it “returns 0 if the number is 0” do
expect(fizzbuzz(0)).to eq 0
end
end

Ruby Code

def divisible_by_3?(number)
divisible_by?(number, 3)
end
def divisible_by_5?(number)
divisible_by?(number, 5)
end
def divisible_by_3_and_5?(number)
divisible_by?(number, 15)
end
def divisible_by?(number, divisor)
number % divisor == 0
end
def fizzbuzz(number)
return 0 if number == 0
return “fizzbuzz” if divisible_by_3_and_5?(number)
return “fizz” if divisible_by_3?(number)
return “buzz” if divisible_by_5?(number)
return number
end

Javascript Tests

describe(‘FizzBuzz’, function() {
var number;
beforeEach(function(){
number = new FizzBuzz();
});
describe(‘when a number is’, function() {
it(‘divisible by 3, should print Fizz’, function(){
expect(number.isDivisibleByThree(3)).toBe(true);
});
it(‘divisible by 5, should print Buzz’, function(){
expect(number.isDivisibleByFive(5)).toBe(true);
});
it(‘divisible by 15, should print FizzBuzz’, function(){
expect(number.isDivisibleByFifteen(15)).toBe(true);
});
});
describe(‘when a number is not’, function () {
it(‘divisible by 3, should print the number’, function(){
expect(number.isDivisibleByThree(1)).toBe(false);
});
it(‘divisible by 5, should print the number’, function(){
expect(number.isDivisibleByFive(1)).toBe(false);
});
it(‘divisible by 15, should print the number’, function(){
expect(number.isDivisibleByFifteen(1)).toBe(false);
});
});
describe(‘play the game’, function() {
it(‘says Fizz when 3 is passed’, function(){
expect(number.says(3)).toMatch(‘Fizz’);
});
it(‘says Buzz when 5 is passed’, function(){
expect(number.says(5)).toMatch(‘Buzz’);
});
it(‘says Fizzbuzz when 15 is passed’, function(){
expect(number.says(15)).toMatch(‘FizzBuzz’);
});
it(‘says the number when something not divisible by 3 or 5 is passed’, function(){
expect(number.says(1)).toEqual(1);
expect(number.says(4)).toEqual(4);
expect(number.says(13)).toEqual(13);
});
});
});

Javascript Code

var FizzBuzz = function(){};
FizzBuzz.prototype.isDivisibleByThree = function(number) {
return number % 3 === 0;
};
FizzBuzz.prototype.isDivisibleByFive = function(number){
return number % 5 === 0;
};
FizzBuzz.prototype.isDivisibleByFifteen = function(number){
return number % 15 === 0;
};
FizzBuzz.prototype.says = function(number){
if(this.isDivisibleByFifteen(number)){
return ‘FizzBuzz’;
}else if(this.isDivisibleByThree(number)){
return ‘Fizz’;
}else if(this.isDivisibleByFive(number)){
return ‘Buzz’;
}else{
return number;
};
};
What Javascript looks like to me after learning Ruby

I had a very interesting experience. I was a minute late for Meditation, and I walked in and the room of 25 people all applauded and cheered for me like crazy. I swear I’m not delusional! This week we are discussing empathy, and the amazing Dana had apparently said “let’s make the next person to walk in feel great by cheering for them!”. It was kind of uncomfortable, but I felt like Brit Brit!

Today I learned:

  • You can pull up the Javascript console in Chrome by typing cmd-option-j
  • There is no implicit return — you need to write ‘return’ to return anything
  • Javascript has no classes — only objects
  • A hash in ruby is called an object in Javascript

Things I want to focus on:

  • Learning proper Javascript syntax
  • What things I can (and can’t) do in Javascript compared to Ruby

Things I’m struggling with

  • The fact that I took for granted how beautiful Ruby is!
  • All the brackets/semicolons
I ❤ Ruby
Like what you read? Give Dan Pelensky a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.