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! :)
Showing posts with label practice. Show all posts
Showing posts with label practice. Show all posts
Tuesday, September 27, 2016
a/A Practice Problems: Two Sum
Labels:
a/A
,
app academy
,
asmarterwaytolearn
,
cleverett
,
coder girl
,
learn to code
,
practice
,
practice problems
,
ruby
,
womenwhocode
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.
Labels:
code
,
coder
,
coder girl
,
codewars
,
developer
,
full stack academy
,
javascript
,
learn to code
,
learn to program
,
practice
,
practice problems
,
women who code
,
womenwhocode
Monday, May 30, 2016
a/A Practice Problems: Sum Nums
This one is pretty straight forward. The only thing to look out for: for loop starting with num and decreasing each time.
Problem: Write a method that takes in an integer 'num' and returns the sum of all integers between zero and num, up to and including num.
Game Plan:
Step 1:
Step 2:
Step 3:
Loop starts with num and stops when it is greater than 0, or 1.
Step 4:
You could also write sum = sum + i;
Step 5:
Check out past problems here:
Problem: Write a method that takes in an integer 'num' and returns the sum of all integers between zero and num, up to and including num.
Game Plan:
>> function takes in one parameter, num.
>> for loop to go through each number below (and including) num (i--).
>> declare variable and set it equal to 0, will hold value from each iteration of loop.
>> return total sum
>> for loop to go through each number below (and including) num (i--).
>> declare variable and set it equal to 0, will hold value from each iteration of loop.
>> return total sum
Step 1:
function sumNums(num) {
}
}
Step 2:
function sumNums(num) {
var sum = 0;
}
var sum = 0;
}
Step 3:
function sumNums(num) {
var sum = 0;
for (var i = num; i > 0; i--) {
}
}
var sum = 0;
for (var i = num; i > 0; i--) {
}
}
Loop starts with num and stops when it is greater than 0, or 1.
Step 4:
function sumNums(num) {
var sum = 0;
for (var i = num; i > 0; i--) {
sum += i;
}
}
var sum = 0;
for (var i = num; i > 0; i--) {
sum += i;
}
}
You could also write sum = sum + i;
Step 5:
function sumNums(num) {
var sum = 0;
for (var i = num; i > 0; i--) {
sum += i;
}
return sum;
}
var sum = 0;
for (var i = num; i > 0; i--) {
sum += i;
}
return sum;
}
Check out past problems here:
Labels:
a/A
,
app academy
,
code
,
developer
,
for loops
,
javascript
,
learn to code
,
practice
,
women who code
Subscribe to:
Posts
(
Atom
)

