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?!
Showing posts with label cleverett. Show all posts
Showing posts with label cleverett. Show all posts
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
Saturday, August 27, 2016
More Free Resources
I recently got a hold of some questions asked by the schools I want to attend during their preliminary coding challenges. Let's just say some were very, very hard.
I did one set of problems for one school and felt great! I was able to answer all three with very little effort and time, which of course was a huge relief. However, on the other exam I was able to fully answer ONE out of FIVE. I was almost done with the second and was able to write some pseudo code for the third, but by the end of my time I barely understood what the last two were asking from me. It was a huge blow.
Since then I've been researching, practicing, learning everything. From refreshing on my basics from 'A Smarter Way to Learn Javascript', to exploring completely new topics that have helped me solve the problems that once bamboozled me.
All in all, things have been progressing over on my end and I hope they are on yours as well! Today I just have a couple more links I've found while trying to learn on my own.
funfunfunctions: This guy is a little goofy and he codes using ECMA6 which at times is a little confusing since I'm still learning vanilla javascript, but he explains things pretty well and hates fibonacci sequences more than I do (which I didn't think was possible). He has some great videos covering more "basics" of javascript that really help push your code to the next level.
Codesmith.io: Codesmith is a dev bootcamp based out of LA. I discovered them while doing some research on course report and found that they offer **FREE** workshops titled 'JS: The Hard Parts'.
It's an in-person workshop you can sign into and participate in via youtube. You can even pair-program with a virtual partner (for which I am currently taking applications). The workshops are held weekly on Thursday nights and really tackled some of the tough stuff these coding schools would like their applicants to be aware of.
That's all for now! Stay tuned for the debut of an awesome website the boy and I are working on :)
I did one set of problems for one school and felt great! I was able to answer all three with very little effort and time, which of course was a huge relief. However, on the other exam I was able to fully answer ONE out of FIVE. I was almost done with the second and was able to write some pseudo code for the third, but by the end of my time I barely understood what the last two were asking from me. It was a huge blow.
Since then I've been researching, practicing, learning everything. From refreshing on my basics from 'A Smarter Way to Learn Javascript', to exploring completely new topics that have helped me solve the problems that once bamboozled me.
All in all, things have been progressing over on my end and I hope they are on yours as well! Today I just have a couple more links I've found while trying to learn on my own.
funfunfunctions: This guy is a little goofy and he codes using ECMA6 which at times is a little confusing since I'm still learning vanilla javascript, but he explains things pretty well and hates fibonacci sequences more than I do (which I didn't think was possible). He has some great videos covering more "basics" of javascript that really help push your code to the next level.
Codesmith.io: Codesmith is a dev bootcamp based out of LA. I discovered them while doing some research on course report and found that they offer **FREE** workshops titled 'JS: The Hard Parts'.
It's an in-person workshop you can sign into and participate in via youtube. You can even pair-program with a virtual partner (for which I am currently taking applications). The workshops are held weekly on Thursday nights and really tackled some of the tough stuff these coding schools would like their applicants to be aware of.
That's all for now! Stay tuned for the debut of an awesome website the boy and I are working on :)
Labels:
cleverett
,
code
,
coder
,
coder girl
,
coding
,
developer
,
factorial
,
javascript
,
programming
,
women who code
,
womenwhocode
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...
Check out past problems here:
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;
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:
Labels:
app academy
,
asmarterwaytolearn
,
cleverett
,
coder
,
full stack academy
,
javascript
,
learn to code
,
learn to program
,
practice problems
,
programming
,
women who code
,
womenwhocode
Thursday, February 25, 2016
Learn to Program: 99 Bottles of Beer
I recently found myself with some unanticipated extra time on my hands, so I decided to put it to good use and get through another chapter of Learn to Program by Chris Pine.
If you're a beginner, I highly recommend going through his *free* e-book (also offered as an actual paper book). It.is.wonderful. An actual tutorial that helps you to do more complex problems as opposed to the ol' "Hello, world" and "2 + 2" examples--who knew you could do so much!! (Kidding.) Really though, his writing and teaching method is so clear and succinct that it was able to help a noob (me) understand the basics of Ruby AND put it to use.
As promised, I'm going to show you the problem and walk you through my answer. Don't look if you haven't tried this yet yourself!! Apparently that's half the fun...
Here's the prompt:
Here's my program:
I set bottles to 99 because that's the starting point. We start at 99 and count down from there.
Because of the song's repetitive nature, I knew I would want a loop so I didn't have to manually type out every line of the song. I tried while bottles > 0 and while bottles != 0, and both gave me the answer I wanted.
I put the first two lines of the song first, because we have to start at 99.
If you use double quotations you can use the #{ } inside the quotations to call on the variable bottles, but this will not work with single quotations.
* Alternatively you could use string concatenation and write:
puts bottles.to_s + 'bottles of beer on the wall..'
Then I put bottles = bottles - 1 because the line after the first to has to include one less bottle than what the previous bottles variable stored (99).
The next part is an if/else statement we need to embed in our while loop because one bottle, not one bottles.
I proceeded by saying if bottles == 1 then puts "#{ } bottle of beer..."
else (if bottles == anything other than 1(don't need to write)) puts "#{ } bottles of beer..."
Don't forget to put end.
That next little line puts ' ' just makes sure that we have a line break after each iteration of the song so that it's a bit easier to read. It obviously is not necessary.
So now if you try to run this program it will almost work. We took care of everything except the very last iteration of the song. It's should still say "1 bottles of beer on the wall..."
We need one more if statement that will modify this last part of the song, which is what you see above. No else needed.
Whew. That was a lot. I know this may seem pretty easy, and once you get a handle on things it really is... but it was also really easy to get tripped up on some little things. If you have any questions or maybe a better way to do this same thing, send'em my way!
Until next time, friends.
If you're a beginner, I highly recommend going through his *free* e-book (also offered as an actual paper book). It.is.wonderful. An actual tutorial that helps you to do more complex problems as opposed to the ol' "Hello, world" and "2 + 2" examples--who knew you could do so much!! (Kidding.) Really though, his writing and teaching method is so clear and succinct that it was able to help a noob (me) understand the basics of Ruby AND put it to use.
As promised, I'm going to show you the problem and walk you through my answer. Don't look if you haven't tried this yet yourself!! Apparently that's half the fun...
Here's the prompt:
Here's my program:
I set bottles to 99 because that's the starting point. We start at 99 and count down from there.
Because of the song's repetitive nature, I knew I would want a loop so I didn't have to manually type out every line of the song. I tried while bottles > 0 and while bottles != 0, and both gave me the answer I wanted.
I put the first two lines of the song first, because we have to start at 99.
If you use double quotations you can use the #{ } inside the quotations to call on the variable bottles, but this will not work with single quotations.
* Alternatively you could use string concatenation and write:
puts bottles.to_s + 'bottles of beer on the wall..'
Then I put bottles = bottles - 1 because the line after the first to has to include one less bottle than what the previous bottles variable stored (99).
The next part is an if/else statement we need to embed in our while loop because one bottle, not one bottles.
I proceeded by saying if bottles == 1 then puts "#{ } bottle of beer..."
else (if bottles == anything other than 1(don't need to write)) puts "#{ } bottles of beer..."
Don't forget to put end.
That next little line puts ' ' just makes sure that we have a line break after each iteration of the song so that it's a bit easier to read. It obviously is not necessary.
So now if you try to run this program it will almost work. We took care of everything except the very last iteration of the song. It's should still say "1 bottles of beer on the wall..."
We need one more if statement that will modify this last part of the song, which is what you see above. No else needed.
Whew. That was a lot. I know this may seem pretty easy, and once you get a handle on things it really is... but it was also really easy to get tripped up on some little things. If you have any questions or maybe a better way to do this same thing, send'em my way!
Until next time, friends.
Labels:
99 bottles
,
chris
,
chris pine
,
clever
,
cleverett
,
coder
,
coder girl
,
girl
,
learn to program
,
pine
,
program
,
ruby
Subscribe to:
Posts
(
Atom
)




