JavaScript Functions and the Importance of Brackets!

I’ve been cracking on with my JavaScript exercises, and more importantly, feeling SO much more positive about it! We had to go out of town for a couple days, so I was doing some reading and exercises over the weekend, and then came back to it today.

Again, I had the same problem with the exercises- I managed the first two without much fuss, but the third one really stumped me. It got to the point that I was looking at the solution and comparing my code, trying to figure out where I went wrong. I was working on it Sunday afternoon, and eventually I just decided to pack up my laptop and leave it until we got back. I opened it up this afternoon, and it just took me about five minutes to get the code working as it should!

var countChar = function(string, char) {
var x = 0;
for ( var i = 0; i < string.length; i++) {
if (string.charAt(i) == char)
x += 1
return x;

var countBs = function(string) {
return countChar(string, "B");
console.log(countChar("kakkerlak", "k"));

This is the offending bit of code. It’s not really that scary or complicated at all! The functions log the number of occurrences of a character in a string- countBs automatically logs the number of uppercase “B”‘s, while countChar also lets the user specify the character. I reckon that the problems with my code happened because I was trying something new; not using curly brackets everywhere- adding a set of curly brackets and a semi-colon completely fixed it! I’m a big fan of using brackets because I think it makes the code look that little bit more organised, but I also understand that sometimes it’s important to have as few characters as possible, so I’d like to be more comfortable working either way.

I’ve also been brushing up on GitHub basics. I thought it might be a good idea to put my solutions to the exercises on GitHub- I know that I’m not writing the most interesting code at the moment, but I thought that it would be good to have something to show people when it comes to applying for apprenticeships or jobs. On that note, I’m really starting to look at an apprenticeship as something I’d like to pursue. I’ve worked with people doing apprenticeships in hospitality before, and I’m pretty sure that if I’m willing to put in the hard work [and hopefully you can tell that I am!] an apprenticeship could be a great place for me to start!

I’d love to hear from anyone who has experience with an apprenticeship!

One thought on “JavaScript Functions and the Importance of Brackets!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s