Showing posts with label codewars. Show all posts
Showing posts with label codewars. Show all posts

Tuesday, September 27, 2016

a/A Practice Problem: Is Power of 2?

Truth be told: I definitely looked for help on this problem. And that's OK!! (At least I think it is.) Part of the whole teaching yourself how to code (or anything really!) is learning from others. That means looking at solutions and understanding how that person got there. Not copy-and-pasting their code into the answer box and saying "DONE!", but writing it out yourself, a hundred times if you have to. I looked at a/A's answer to this (in Ruby) and was able to translate it into a working JS program.

Here's the problem:

# Write a method that takes in a number and returns true if it is a
# power of 2. Otherwise, return false.
#
# You may want to use the `%` modulo operation. `5 % 2` returns the
# remainder when dividing 5 by 2; therefore, `5 % 2 == 1`. In the case
# of `6 % 2`, since 2 evenly divides 6 with no remainder, `6 % 2 == 0`.

My Solution















That first if statement, tells us what to reject right away -- if the number passed in isn't divisible by 2, then we know that two things: that it's definitely an odd number, and that it cannot be a power of 2.

The while loop portion was something I picked up from Codewars. Let me try to dissect and explain: The (!num) portion only, excluding the first exclamation mark, is basically saying num is false. The exclamation mark acts like negation (I think-- please feel free to correct me!). So the outer exclamation mark (!(!num)) is a double negative and is basically saying while num is true.

A while loop still needs the same components as a for-loop: the initiation (num), the condition(while num is true) and termination (num is false).

  • The first if within the loop would break us out of the loop if and when num is equal to 1 (which would be our 2 to the 0 power case. If we get to this case, that means it is a power of 2 and we should return true, which the code states!
  • The else if scenario checks if the remainder of num / 2 is equal to 0. If it is, divide it by 2 again.
  • The last else case will handle the other cases and if the number hasn't passed the first two cases, it's not a power of two, so we should return false.

What do you think? How would you solve this problem?!

Thursday, June 9, 2016

Codewars as a Training Tool


In this post I wanted to stick up for CodeWars and help explain to the novice coder why this site is so fucking awesome.

First, a little backstory. My boyfriend and I are both just starting out in this whole programming world. He was the one who told me about it first and when I first went to the site, I could barely get past the mandatory (but simple) coding challenges you have to get right in order to gain access to the full site.

Fast forward to today and I am on the site at least once a day training. It has become the best learning tool for me thus far. It challenges me every day, reminding me why I like coding. My boyfriend on the other, while he can build out an entire rails app, absolutely hates the site. I think it's because he hasn't given it enough of a chance (which is true), but he says his learning style is different (which also is true). Either way, I happen to think that anyone who uses the system the right way will soon too become a true believer. So, herein this post I present to you my methodology to using the site so that even you can conquer and earn your honor.

1. Select your level.
On the left hand side you will find everything you need to make sure you are doing problems within your range. The levels are from easiest, 8, to hardest, 1. Truth be told, it's best to have gone through the basics so that you can attempt the problems, but even if you are going along as you learn you can customize which problems to do (see below). Start off with 8, and move at your own pace. Some of the problems within are going to be a lot more difficult than some of the others. But give yourself some patience and work through them, absorbing everything you can.

2. Select what areas you want to train.
Once you've selected which level you want to compete in, you can then choose problems that have been tagged with certain areas of concentration. On the left hand side, under Tags, you can select Arrays, Numbers, Bugs, Fundamentals, etc. When I was starting out I found that the best thing that helped me was completing level 8 "bug" problems. They re-familiarized me with syntax and some other basic concepts that helped me to solve some other problems without the worry of that. If you're working through the problems while trying to master one area of a certain language, this will be one of the best ways to use CodeWars to do just that.

3. Don't be ashamed to forfeit honor.
No one likes to lose, obviously. But sometimes, you need to suck it up. There are problems that for some reason will not pass the tests, but once you click submit will actually work. Save for these cases, if you can't figure out why something won't work, it might be time to just unlock the solutions to that problem. It could be something simple you overlooked, but if it's something you can't figure out on your own, unlock the solutions and see how other people solved it. Think of it as learning, not cheating if you happen to be so honorable.

4. Read other peoples' answers.
Whether I submitted my answer or forfeited honor, I always look at other peoples' answers. There will be things in there that look like straight up gibberish. Even when I finished my Javascript book, there were things I had never seen before. Methods that would produce the right code with a fraction of the lines. It was insane. But it's also insanely insightful. Whenever there's something I don't understand I look it up, and you can get the general idea of the way something works once you see it.

Going through the site without any sort of order can be pretty depressing as a noob. You can easily get that "omg-I'm-never-going-to-understand-this-and-I-better-get-back-to-my-dead-end-job" feeling, but I swear with time, patience and a little order CodeWars can and will open all sorts of doors.

Tuesday, June 7, 2016

a/A Practice Problems: Count Vowels

Practice problem number 6 of 21. Here we go.

Problem: Write a method that takes a string and returns the number of vowels in the string. You may assume that all the letters are lower cased. You can treat "y" as a consonant.

This question was a nice and easy surprise after the time conversion mayhem. It's pretty straight forward, but let me explain how I thought to solve this one. I want to be have a function take in a string, and no matter how it is written, I would like it all to be in lowercase (there's a method for that). Secondly, I would like every letter to be separated so that I can write a loop to go through each individually to see if it is any of the vowels. If it is, I'll have a vowel counter and it will go up by one and I will be happy.

1. Declare your function with one parameter.

function vowels(myString) {
}


2. Let's convert myString to all lower case and split all the letters apart. You can put it all in one statement. We can also set it equal to the same variable and we don't need to declare it with var, since it's already implicitly declared in the parameter. We also need to declare another variable, vowelCount, and set it equal to 0. This is the variable that will increase if the loop finds any vowels.

function vowels(myString) {
  myString = myString.toLowerCase().split("");
  var vowelCount = 0;
}


3. Now we just need a loop to go through all the letters individually.

function vowels(myString) {
  myString = myString.toLowerCase().split("");
  var vowelCount = 0;
  for (var i = 0; i < myString.length; i++){
     
  }
}


4. If we were to put vowelCount++ as the code block for our loop, it would could every letter and increase the total by the wrong amount. We only need vowels, so we need an if statement. I know there's a shorter way of doing this, but I don't know what that is so this will have to do.

function vowels(myString) {
  myString = myString.toLowerCase().split("");
  var vowelCount = 0;
  for (var i = 0; i < myString.length; i++){
      if (myString[i] === "a" || myString[i] === "e" || myString[i] === "i" || myString[i] === "o" || myString[i] === "u" ||) {
        vowelCount++;
      }
   }
   return vowelCount;
}


The last thing to do is return the vowelCount at the end there. Make sure you remember to do this outside of the for loop, right before you end the function.


Until next time!

Check out past problems here:

Friday, May 20, 2016

My 3 Favorite Free Resources to Learn Javascript

When I started this blog (a whole 4 posts ago), I was focused on learning Ruby. Since then I've switched gears because the school I will be applying to focuses solely on, you guessed it, Javascript. I hope to visit Ruby again in the future, but for now I'm all bout those curly braces. Woo.

I've had some success in learning the language. Thanks to the interwebs and all the free resources I've been using, I feel pretty good about where I'm at so I wanted to share what's helped me.

  1.  A Smarter Way to Learn by Mark Myers.
    This book was recommended to me by a friend and has since become the basis of all my understanding. The way Myers explains things is pretty straightforward and everything builds on itself almost perfectly. Every chapter-- only 89 --  lasts a couple of pages, 5 maybe at most and once you're done there are practice problems associated with each chapter to test your newly acquired knowledge. While the book focuses on many different concepts, it does so in pretty basic ways, but this is to ease you into the language and help you to actually understand the fundamentals.

  2. Codewars
    When I first heard of Codewars I was eager to sign up. Be aware, there are two problems you have to answer right to actually become a part of the site, but once you are it's great! It works like Karate in that you start at the beginner levels denoted by higher numbers (8 kata) and work your way down (or up) to 1, at which time you can be a self-proclaimed Jedi coding wizard. You can target what you want to work on: fundamentals, algorithms, logic, loops, etc, in every level and go from there. You earn honor or "kata" for every question you answer right and it's a great way to test or refine your skills. As far as negatives go, it can be really self-defeating. Sometimes the challenges themselves aren't written correctly and so you can't get the points even though your answer is right. Other times it serves as a humble reminder that you might be a long way from where you want to be. But don't get discouraged! If you can't solve a problem and it's eating away at you (a common thing 'round these parts) forfeit the kata and read some solutions (only visible when you forfeit or answer correctly). See what others have posted and you might find that the answer was well within your reach, and you will start getting better. Remember: wax on, wax off.

  3. YouTube
    My biggest struggle with learning to code on my own was a lack of resources out there after the plethora of pure beginner sources. I was having a hard time translating what I had learned into actual working programs. Step in YouTube: the world's best guidebook for any and all things. While I have had some trouble finding a video that explains the exact problem I'm working on, I have found some really helpful tutorials that lead me the right way. The user, kudvenkat, has been one of my personal favorites, but there are seriously endless amounts of people out there willing to help. Another personal fave: LearnCode.academy.
Of course this list is not exhaustive, and as such I have a few others I'd like to shout out: 
Honorable mentions: w3schools, devtools and stackoverflow.

My hope is that this helps someone else out there, but know there are so many other people and resources willing to help if this particular list doesn't work for you. Go and get on with your bad self.

Happy Friday, friends!