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
Subscribe to:
Post Comments
(
Atom
)

No comments :
Post a Comment