Do weekends count? RD1D9-11 #100DaysOfCode

Most people probably relax and take time out on the weekends, not me. I’m busy working looking after our little business.

But that doesn’t mean I didn’t get to do  any code!

Quite the contrary, I sat with my little Google Pixel C tablet (now retired sadly as it is a great little powerhouse tab with a decent keyboard) hooked up to my phone’s hotspot and took FreeCodeCamp lessons in between customers. It’s not ideal really but in actual fact I kind of found it quite good in a way because I got to do a little, then think about it and ruminate on the problems.

Then back to it, do a little more, then serve again and so on and so forth. I churned through quite a few lessons in the front end certificate course actually and came up to my next big challenge. Well it’s probably not that big, but for me it was because lesson 183 is a Golf Code Challenge and I know absolutely nothing about golf, and having to force myself to sit and think about the problem and how I was going to solve it took some real effort.

Here is the challenge description:

In the game of golf each hole has a parmeaning the average number of strokes a golfer is expected to make in order to sink the ball in a hole to complete the play. Depending on how far above or below par your strokes are, there is a different nickname.

Your function will be passed par and strokesarguments. Return the correct string according to this table which lists the strokes in order of priority; top (highest) to bottom (lowest):

Strokes Return
1 “Hole-in-one!”
<= par – 2 “Eagle”
par – 1 “Birdie”
par “Par”
par + 1 “Bogey”
par + 2 “Double Bogey”
>= par + 3 “Go Home!”

par and strokes will always be numeric and positive.

At first I thought I would need to do a whole bunch of else if statements and I started strong thinking about it the right way with my first block of statements:

function golfScore(par, strokes) {
// Only change code below this line
if(strokes <= 1) {
return "Hole-in-one!";
} else {
if (strokes <=2) {
return "Eagle";

But past that things started getting shoogly and I couldn’t get past all of these syntax errors… and my else if statements weren’t working as expected.

I came across the same problem that I lodged my first pull request on GitHub for, with the parentheses not being closed after the final else statement, but now I think I know better and that perhaps it was me that was wrong. Never!

By the time I had coded the solutions to each score as I thought it would be I needed to use seven parentheses at the end to close all the damn open ones I had created and I knew something was wrong.

welcome to ugmo-codeland, population = me

I thought I was so clever using the logical operator && to match stroke with par as the variables but it wasn’t the answer. But I was determined not to look at the solution. I had to figure it out.

It was looking so messy though, I knew I was doing something wrong with all those closing parentheses at the end just to clear the syntax errors, this wasn’t working.

I spent a day thinking about it all day, and that night I woke up at 3am talking in my sleep about JavaScript and problem solving Golf issues so I knew this couldn’t do. I actually got up and had a cuppa to clear my head, I couldn’t stop thinking about it but I didn’t want to turn the lappy on at 3am in the morning and start coding as I would never get back to sleep, and I need my sleep. 

Today I had a bit of a slow day and didn’t get to coding until late in the afternoon and by then my brain was tired and I couldn’t get past where I was at so I had to sneak a look at the hint section goddammit.

As soon as I did I saw my most obvious mistake right away with my else and if statements being on different lines causing all the parentheses dramas, and of course the first line of the hint showed me where I was also going wrong not using both variables in each statement… sigh. Oh well, it was a good learning.

Once I had that first else if statement correct it was just a matter of combing through the problems and putting the right math in place, came pretty easy after that….

My solution that passed all tests.

I ran some tests with different scores and everything passed – huzzah! Bit disappointed with myself for having to result in looking at the hints but oh well, I had a good crack it before I did so still a good outcome. 

Thanks, FCC

Also loaded up CodeKit and and Local by Flywheel and fired up my dev environment for WordPress and played around with the scripts.js files in Atom again to see if I could solve that issue but alas, still no joy there. Hopefully tomorrow I get a bit more solid thinking time to really try and solve that one. I find it’s still good practise just firing things up at this point in my learning journey, helps remember stuff that is still relatively new. Especially CodeKit, I am sure I have barely scratched the surface of that one yet.

Looking forward to the next challenge on FCC too, cracking tool for learning that site is.

Until then, signing out.

Leave a Reply

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