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?!
Wednesday, September 28, 2016
a/A Practice Problem: Third Greatest pt.1
Labels:
app academy
,
asmarterwaytolearn
,
cleverett
,
code
,
coder
,
coder girl
,
coding
,
developer
,
learn to program
,
practice problems
,
programming
,
women who code
,
womenwhocode
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).
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?!
Labels:
a/A
,
app academy
,
asmarterwaytolearn
,
cleverett
,
coder
,
coder girl
,
codewars
,
coding
,
javascript
,
learn to program
,
practice problems
,
programming
,
ruby
,
women who code
,
womenwhocode
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! :)
# 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! :)
Labels:
a/A
,
app academy
,
asmarterwaytolearn
,
cleverett
,
coder girl
,
learn to code
,
practice
,
practice problems
,
ruby
,
womenwhocode
Subscribe to:
Posts
(
Atom
)