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 learn to code. Show all posts
Showing posts with label learn to code. 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
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
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.
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.
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.
4. Lastly, an if statement that says if the original string and newString are the same will return true, else it will return false.
Next up the mediums! **tingles**
Check out past problems here:
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 = "";
}
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];
}
}
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;
}
}
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:
Labels:
a/A
,
app academy
,
asmarterwaytolearn
,
code
,
coder
,
coder girl
,
coding
,
developer
,
full stack
,
full stack academy
,
learn to code
,
learn to program
,
practice problems
a/A Practice Problems: Count Vowels
Practice problem number 6 of 21. Here we go.
Problem: Write a method that takes a string and returns the number of vowels in the string. You may assume that all the letters are lower cased. You can treat "y" as a consonant.
This question was a nice and easy surprise after the time conversion mayhem. It's pretty straight forward, but let me explain how I thought to solve this one. I want to be have a function take in a string, and no matter how it is written, I would like it all to be in lowercase (there's a method for that). Secondly, I would like every letter to be separated so that I can write a loop to go through each individually to see if it is any of the vowels. If it is, I'll have a vowel counter and it will go up by one and I will be happy.
1. Declare your function with one parameter.
2. Let's convert myString to all lower case and split all the letters apart. You can put it all in one statement. We can also set it equal to the same variable and we don't need to declare it with var, since it's already implicitly declared in the parameter. We also need to declare another variable, vowelCount, and set it equal to 0. This is the variable that will increase if the loop finds any vowels.
3. Now we just need a loop to go through all the letters individually.
4. If we were to put vowelCount++ as the code block for our loop, it would could every letter and increase the total by the wrong amount. We only need vowels, so we need an if statement. I know there's a shorter way of doing this, but I don't know what that is so this will have to do.
The last thing to do is return the vowelCount at the end there. Make sure you remember to do this outside of the for loop, right before you end the function.
Until next time!
Check out past problems here:
Problem: Write a method that takes a string and returns the number of vowels in the string. You may assume that all the letters are lower cased. You can treat "y" as a consonant.
This question was a nice and easy surprise after the time conversion mayhem. It's pretty straight forward, but let me explain how I thought to solve this one. I want to be have a function take in a string, and no matter how it is written, I would like it all to be in lowercase (there's a method for that). Secondly, I would like every letter to be separated so that I can write a loop to go through each individually to see if it is any of the vowels. If it is, I'll have a vowel counter and it will go up by one and I will be happy.
1. Declare your function with one parameter.
function vowels(myString) {
}
}
2. Let's convert myString to all lower case and split all the letters apart. You can put it all in one statement. We can also set it equal to the same variable and we don't need to declare it with var, since it's already implicitly declared in the parameter. We also need to declare another variable, vowelCount, and set it equal to 0. This is the variable that will increase if the loop finds any vowels.
function vowels(myString) {
myString = myString.toLowerCase().split("");
var vowelCount = 0;
}
myString = myString.toLowerCase().split("");
var vowelCount = 0;
}
3. Now we just need a loop to go through all the letters individually.
function vowels(myString) {
myString = myString.toLowerCase().split("");
var vowelCount = 0;
for (var i = 0; i < myString.length; i++){
}
}
myString = myString.toLowerCase().split("");
var vowelCount = 0;
for (var i = 0; i < myString.length; i++){
}
}
4. If we were to put vowelCount++ as the code block for our loop, it would could every letter and increase the total by the wrong amount. We only need vowels, so we need an if statement. I know there's a shorter way of doing this, but I don't know what that is so this will have to do.
function vowels(myString) {
myString = myString.toLowerCase().split("");
var vowelCount = 0;
for (var i = 0; i < myString.length; i++){
if (myString[i] === "a" || myString[i] === "e" || myString[i] === "i" || myString[i] === "o" || myString[i] === "u" ||) {
vowelCount++;
}
}
return vowelCount;
}
myString = myString.toLowerCase().split("");
var vowelCount = 0;
for (var i = 0; i < myString.length; i++){
if (myString[i] === "a" || myString[i] === "e" || myString[i] === "i" || myString[i] === "o" || myString[i] === "u" ||) {
vowelCount++;
}
}
return vowelCount;
}
The last thing to do is return the vowelCount at the end there. Make sure you remember to do this outside of the for loop, right before you end the function.
Until next time!
Check out past problems here:
Labels:
codewars
,
coding
,
full stack
,
full stack academy
,
girl
,
girl coder
,
grace hopper
,
hackathon
,
javascript
,
learn to code
,
learn to program
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:
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.)
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.
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.
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:
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;
}
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;
}
}
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;
}
}
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:
Labels:
a/A
,
app academy
,
asmarterwaytolearn
,
coder girl
,
javascript
,
learn to code
,
learn to program
,
mark meyers
,
program
,
women who code
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
Sunday, May 29, 2016
a/A Practice Problems: Longest Word
This problem was a tricky one. Despite its "easy" rating, I struggled with it for a little thinking of the most roundabout ways of answering, including multiple nested for loops-- which as you can guess was just a joy ride. Maybe possible, but that way would have taken me forever to write and would have probably been very error prone. The hardest part for me turned out to be knowing how to set it up. How could I solve this so that I didn't need to go through every character of a string? Well, you don't want to go through every character necessarily (my first mistake), but rather every word. But I'm getting ahead of myself...
Problem: Write a method that takes in a string. Return the longest word in the string. You may assume that the string contains only letters and spaces. You may use the 'split' method to aid you in your quest.
Game Plan:
>> Function that has one parameter (string) in this example.
>> How can you quickly separate each word? Split method. The result of which will return an array of all the words. You'll want to set this equal to some variable for easy access later.
>> For loop to go though each word to determine if it's the longest.
>> This is where I got sort of lost-- I knew I needed to do all the above steps, but I didn't know how to keep track of which word was longest. If you had one independent variable to compare each word to, wouldn't that be easier than making some sort of March-Madness bracket system where you compare every word to each other? Methinks yes. So in order to do that, I'll set a variable equal to 0. The way the loop will work : compare the first word's length to 0, if the word is bigger (which it will be since it will be at least one letter character), i'll save the length of that word to the variable. As the loop progresses it will compare each word to this variable, saving the larger of the two.
>> if statement to compare lengths
>> Return longest word
Step 1:
function longestWord(string) {
}
Step 2:
function longestWord(string) {
var words = string.split(" ");
}
Make sure there's a space between your quotes so that it knows where to split the string. If you didn't include the space, you would get each individual character at each index.
Step 3:
function longestWord(string) {
var words = string.split(" ");
var longest = 0;
}
We declare this variable, so that we don't need to compare each word to each other, but rather one independent variable.
Step 4:
function longestWord(string) {
var words = string.split(" ");
var longest = 0;
for (var i = 0; i < words.length; i++) {
}
}
Remember when you split the string, it results in an array of all the words so we can use the array's length.
function longestWord(string) {
var words = string.split(" ");
var longest = 0;
var longIndx = 0;
var longIndx = 0;
for (var i = 0; i < words.length; i++) {
if (words[i].length > longest) {
longest = words[i].length;
longIndx++;
}
}
}
The tricky part-- if the length of the word at index i is greater than longest (0), then we change the value of longest to the length of that word. AND since we want to keep track of which word we are on, we can set a marker of sorts at 0 (longIndx) that will increase if the word at position i is greater than longest. The value will let us know the index number of the longest word so that we can call it later.
function longestWord(string) {
var words = string.split(" ");
var longest = 0;
var longIndx = 0;
for (var i = 0; i < words.length; i++) {
if (words[i].length > longest) {
longest = words[i].length;
longIndx++;
}
}
return words[longIndx];
}
Try it yourself!
Check out past problems here:
Labels:
app academy
,
developer
,
fullstack
,
fullstack academy
,
girl coder
,
grace hopper
,
javascript
,
learn to code
,
programming
,
womenwhocode
Wednesday, May 25, 2016
a/A Practice Problems: Reverse a String
When I had first learned about App Academy I quickly tried to learn everything I could to solve those damned practice problems. Short story shorter, I could not wrap my mind around the first one. I came back to it the other night, however, and with all my new fandangled Javascript skills I was able to take on more than a couple. SHABOOYAH. What really helps me when I am learning something new is seeing the process someone went through to solve the challenge at hand, in this case reversing a string without using the reverse method. Because I found so few resources like that initially, I thought to do it myself and hopefully be that source for someone else.
Check out other problems here:
Note, App Academy is a Ruby/Rails school, but if I read correctly they'll allow you to answer challenges in whichever language you choose -- obviously I'll be doing all these in JS.
Your mission: "Write a method that will take a string as input, and return a new string with the same letters in reverse order"
Disclaimer: This is my methodology, there are many other ways to do this; most likely there better ways to do this. But I'm a noob, so shut it.
Step 1: Write out what you need to do. It's like a game plan or blueprint to getting the answer. It helps me to really think about what the question is asking so that I can answer appropriately. This one is pretty straightforward.
>> have to be able to accept input (function via arguments/parameters)
>> pinpoint last letter of string (1 less than length)
>> run through & return letters from last character (loop with descending counter)
>> return reversed string
Step 2: Define the function. Only rules that apply are the same for that of variables. (no numbers, symbols to start off; contains only letters, numbers, $ and _). Set up function to accept one parameter, "word" in this example.
function reverseString (word) {}
Step 3: Find last character of string. I set the value equal to a new variable, lastChar so that it's easier to access. Remember indexes start with 0 (hence the minus 1).
function reverseString(word) {
var lastChar = word.length - 1;
var lastChar = word.length - 1;
}
Step 4: For loop with descending counter. Instead of setting i = 0, I set it to lastChar so that it would start with the last letter of the string; i >= 0 tells the loop to stop once it reaches the index 0, first letter of the string; i-- tells the loop to decrement by one each time.
}
Step 5: Concatenate. If we were to give ourselves a test case, say reverseString("abc");, and if we were to console.log(word[i]) we would see that each letter would come back in reversed order. Hooray! But, (boo!) it wouldn't come back in one string, "cba". To fix this, I thought do declare another variable (empty this time) and then add each letter to it, aka concatenate it into one. The code block inside the for-loop is telling the function to add each letter to the variable newString. I'm not sure why you would use bracket notation for a string when they're reserved mostly for arrays, but I know it works and that they operate similarly and that's the best explanation I can give.
}
Step 6: Return newString. Now that we have all the working parts, the only thing left to do is to return the reversed string. Remember to put this after the function.
function reverseString(word) {
var lastChar = word.length - 1;
for (var i = lastChar; i >= 0; i--) {
}
Step 5: Concatenate. If we were to give ourselves a test case, say reverseString("abc");, and if we were to console.log(word[i]) we would see that each letter would come back in reversed order. Hooray! But, (boo!) it wouldn't come back in one string, "cba". To fix this, I thought do declare another variable (empty this time) and then add each letter to it, aka concatenate it into one. The code block inside the for-loop is telling the function to add each letter to the variable newString. I'm not sure why you would use bracket notation for a string when they're reserved mostly for arrays, but I know it works and that they operate similarly and that's the best explanation I can give.
function reverseString(word) {
var lastChar = word.length - 1;
var newString = "";
var newString = "";
for (var i = lastChar; i >= 0; i--) {
newString += word[i];
// same as newString = newString + word[i];
// same as newString = newString + word[i];
}
Step 6: Return newString. Now that we have all the working parts, the only thing left to do is to return the reversed string. Remember to put this after the function.
function reverseString(word) {
var lastChar = word.length - 1;
var newString = "";
var newString = "";
for (var i = lastChar; i >= 0; i--) {
newString += word[i];
}
return newString;
return newString;
}
If you were to type this into your console or whatever environment, you should get "cba". There you have it, guys! Comment with any questions or if you have any techniques that would solve this quicker, I'd love to read it. Good luck!
Check out other problems here:
Labels:
app academy
,
codergirl
,
full stack
,
full stack academy
,
grace hopper
,
javascript
,
learn to code
,
loops
,
practice problem
Subscribe to:
Posts
(
Atom
)

