Showing posts with label asmarterwaytolearn. Show all posts
Showing posts with label asmarterwaytolearn. Show all posts

Wednesday, September 28, 2016

a/A Practice Problem: Third Greatest pt.1

This is the first part of this question/answer series because the answer I came up with was very easy only because I was able to use methods to help me solve it quicker than I think they (a/A) wants. They didn't explicitly say it in this problem, but in previous challenges it said to refrain from using methods like .reverse, which I used here. SO because of that I'm going to show you my short code and talk about .sort, and then another day I'll post a longer more confusing answer that shows your know how to harness the power of loops and variables with the wrath of Aphrodites. Or something. Idfk.

The problem:
# Write a method that takes an array of numbers in. Your method should
# return the third greatest number in the array. You may assume that
# the array has at least three numbers in it.

My Solution:







SO. Let's talk about .sort! Here's my wacky explanation, but please avail yourself to some of the links at the end of this post.

.sort is an array method. It'll work beautifully on arrays containing words and will alphabetize it the way you would. However, it will not do that with numbers. the computer would consider 100 before 2 because 1 is less than 2. So when it comes to numbers, we use this comparison function that you see in there.   return a-b; will sort your numbers from least to greatest, while return b-a will sort your numbers from greatest to least. To avoid saying the wrong thing and causing someone out there (even if it's me at a later date) a massive headache because what I said was gobbledegook, this is as far as I'll go.

Next we want to reverse the array, because when we do that we can easily pull out the third number which by then should be the third greatest!

Make sure to check out these links:
http://www.w3schools.com/jsref/jsref_sort.asp
http://www.javascriptkit.com/javatutors/arraysort.shtml


How would you solve this?!

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?!

a/A Practice Problems: Two Sum

It's been a minute. A New York minute, if you will. Cheeeese.  Today we're back with another App Academy practice problem.  Here it is:

# Write a method that takes an array of numbers. If a pair of numbers
# in the array sums to zero, return the positions of those two numbers.
# If no pair of numbers sums to zero, return `nil`.

A couple things beforehand: nil in javascript would be null, so I return null. I came to a working answer through trial and error, but I'm pretty sure there is a much better way to solve this. I tried a couple other ways, but nothing worked so any advice out there would be more than welcome!!


I started the first loop off at 0 and the second loop at 1 because there's no need to add the first index to itself.  Just remember the way nested loops work : the second loop will finish an entire iteration (from 1 to less than nums.length before the first loop moves on to i = 1.

That last line there was my test case, so don't mind that! :) Usually when I solve problems, I write everything out. I'm working on being more concise with my code, which is why I didn't declare an array and then push the values to it (even though I definitely tried this a billion times). But I gotta say, I kinda liked just returning i and j in an array.

That's all for today.
Happy coding! :)

Wednesday, June 8, 2016

a/A Practice Problems: Nearby AZ

Practice Problem 8 of 21 and the first medium! It's exciting stuff happening 'round these parts. Without further adieu...

Write a method that takes a string in and returns true if the letter "z" appears within three letters **after** an "a". You may assume that the string contains only lowercase letters.

This is a little trickier, but still employs the same methods and logic that we've already seen. A couple loops and if statements.

1.  Declare our function with one parameter, string.

function nearbyAZ (string) {
}

2.  I wanted to declare a variable, index1, that would hold the place of the position in which the letter "a" appears. This will be helpful later when determining if there's a "z" within the next three letters.

function nearbyAZ (string) {
   var index1 = 0;
}

3. We want to loop through all the letters in order to see if there's an "a".  The code block within the loop will be an if/else statement. If there's an "a", store it's index number in the variable, index1, and break out of the loop. Else, I want to increase index1 by one with each iteration of the loop. This way we will store the correct position number.

function nearbyAZ (string) {
   var index1 = 0;
   for (var i = 0; i < string.length; i++) {
      if (string[i] === "a") {
         index1 = i;
         break;
      }
      else {
         index1++;
      }
   }
}

4.  After the previous loop, I want another variable, index2, which will store the value of index1 + 1. This will be where our loop will start, and it should end at the third letter after the first a: index1 + 3. 

function nearbyAZ (string) {
   var index1 = 0;
   for (var i = 0; i < string.length; i++) {
      if (string[i] === "a") {
         break;
      }
      else {
         index1++;
      }
   }
   var index2 = index1 + 1;
   for (var j = index2; j <= index1 + 3; j++) {
   }
}

5. Start the loop at index2 because we are only concerned with the first three letters after the letter "a". Likewise, we'll want to stop the loop once it reaches that third letter. An if statement is the last part required to return true or false if there's a "z". 

function nearbyAZ (string) {
   var index1 = 0;
   for (var i = 0; i < string.length; i++) {
      if (string[i] === "a") {
         break;
      }
      else {
         index1++;
      }
   }
   var index2 = index1 + 1;
   for (var j = index2; j <= index1 + 3; j++) {
      if (string[j] === "z") {
         return true;
      }
   }
   return false;
}


Check out past problems here:

Tuesday, June 7, 2016

a/A Practice Problems: Palindrome

We have made it to the last of the "easy" problems. Rejoice!  Here we go...

Write a method that takes a string and returns true if it is a palindrome. A palindrome is a string that is the same whether written backward or forward. Assume that there are no spaces; only lowercase letters will be given.

 We know that we need a function that will take in a single parameter. We also want to declare an empty string that we can use to store the new word we will form by running a loop adding each letter from the string in reverse order. Lastly, we need a check to see if the original and the new string are the same, a palindrome, and return either true or false. Easy peasy.

1. Construct your function.

function palindrome(string) {
}


2. I declared a string that would hold the index position of the last letter of the string so as to use the variable in the loop instead. This isn't necessary. But you will want to declare a variable newString, which will be the original string in reversed order.

function palindrome(string) {
   var lastIndex = string.length - 1;
   var newString = "";
}


3. The loop starts with the index of the last letter, lastIndex, which we defined previously. It will continue so long as i is greater than or equal to 0, and will decrease by one each time. The code block inside the loop will concatenate each letter and form the new string in newString.

function palindrome(string) {
   var lastIndex = string.length - 1;
   var newString = "";
   for (var i = lastIndex; i >= 0; i--) {
      newString += string[i];
   }
}


 4. Lastly, an if statement that says if the original string and newString are the same will return true, else it will return false.

function palindrome(string) {
   var lastIndex = string.length - 1;
   var newString = "";
   for (var i = lastIndex; i >= 0; i--) {
      newString += string[i];
   }
   if (string === newString) {
      return true;
   } else {
      return false;
   }
}


Next up the mediums! **tingles**

Check out past problems here:

Monday, June 6, 2016

a/A Practice Problems: Time Conversion

Back at it again today! My plan is to continue solving these practice problems till I feel that "Oh hell yes!" sense of accomplishment. Let's get on with it.

Your mission: Write a method that will take in a number of minutes and returns a string that formats the number into "hours:minutes"

In keeping with tradition,  think about how to solve this problem before doing any actual coding. I promise it's easier than Phoebe trying to teach Joey guitar cords without a guitar. #friends

The problem is asking us to convert any number of minutes given into the hours:minutes format. In order to do this there will have to be some math involved. Maybe set an hour counter equal to 0 and every time there's at least 60 that counter should go up by one. There should conversely be another counter that starts with the number passed in and decreases by 60 each time. Then it's just an issue of formatting, concatenating variables with strings and whatnot.

1. Let's go ahead and define our function with one argument. It is already provided in Ruby, but for Javascript it would be:

function time_conversion(minutes) {

}


2. I defined two new variables here: hours, which I set equal to 0, and min, which I set equal to the parameter, minutes. (This last step might be repetitive, but it was how I solved the problem and it worked so I'm keeping it for now.)

function time_conversion(minutes) {    
   var hours = 0;
   var min = minutes;
}


3. Every time min is greater than or equal to 60, I want hours to increase by 1 and I want min to decrease by 60. To do this, I used a while loop. Inside the while loop, I want it to do all the math I explained above. Remember to include the counter (i -= 60) or you'll find yourself in an infinite loop. The same three parts of a for-loop are contained in a while loop, just the formatting is different.

function time_conversion(minutes) {
   var hours = 0;
   var min = minutes;
   var i = min;
   while (i >= 60)
      hours++;
      min -= 60;
      i -= 60;
   }
}


4. Almost done! Just need to handle some special cases-- when there is less than ten minutes, you want it to read "06" or "0-whatever" not just "6". The second case you want it to handle is when there are 0 minutes, you want it to say "00". Sounds like an if statement.

function time_conversion(minutes) {
   var hours = 0;
   var min = minutes;
   var i = min;
   while (i >= 60)
      hours++;
      min -= 60;
      i -= 60;
   }
   if (min === 0) {
      min = "00" 
      return hours + ":" + min;
   } 
   else if (min < 10) {
      min = "0" + min;
      return hours + ":" + min;
   }
   else {
      return hours + ":" + min;
   } 
}


This problem sent me for a loop. Yes, pun intended. It literally sent me into an infinite loop multiple times. Despite having to come back to this problem on different days to give it another shot, that little victory kinda made it all worth it. It's a pretty awesome feeling of accomplishment figuring these challenges out, even if it may not be the most concise code or employ best practices.. I have to actually know how do it first, amirite? Here's to the small victories!

P.S. I move to New York in TEN DAYS. (!!!!!!!!!!!!!)

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!