Created
March 2, 2020 05:20
-
-
Save harrisonmalone/d27f5101a585016222910dafcb6b6c0a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Arrays | |
# 1. Make an array of 3 different colours. | |
# 2. How do I access "blue" using an index. | |
# 3. Store the string blue (which you accessed with an index) in a variable. | |
# 4. Create an array called four_letter_animals with the following animals (in this order): | |
# "calf", "duck", "elephant", "goat", "lamb", "lion", "mule", "dog". | |
# Add "puma" to the end of the array. Insert "joey" after "goat" and before "lamb". Delete "dog". Reverse the order of the array. Replace "elephant" with "foal". Add "bear" to the end of the array. Reverse the order of the array again. | |
# 5. Create an array with 10 animals you'd find at the zoo. | |
# 6. Access the 3rd, 5th, and last animal using indexes (remember indexes start at 0), store these strings in variables that match the string value. There is a ruby method that allows you to access the last position in an array. Use that instead of a hard coded index number. | |
# 7. Using the variable containing "blue" you created in question 3 create a control flow structure whereby if the variable `colour` is "blue" print out "blue dub a deee dub a diii", else print out "some other colour". | |
# 8. How do we insert a new string into our animals array? There are many ways to do this - use a method that you haven't used already. | |
# 9. Find a way to remove the first element of the array (again, there are several options here, so search the internet for a solution). | |
# 10. The zoo is cutting down on costs and needs to now transfer some animals to another zoo. Find a way to remove the final five items in the animals array. | |
# 11. The owner of the zoo won the lottery and can now bring back those same five animals. How do we do this ruby an array method rather than just hard coding a new animals array? Use a range of methods in your answer. | |
# 12. The zoo owner has now lost track of how many animal types are in her zoo. Find out how many animals are in the array using a ruby array method. | |
# 13. The zoo owner wants to know if their zoo contains monkeys. Find a way to check if the array contains monkeys using a ruby array method. | |
# 14. The zoo owner wants to have their animals sorted in alphabetical order because they're trying to be more organized. Achieve this using an array method. | |
# 15. Create a variable `some_letters` which is a range of letters from 'a' to 'e' using the ruby range syntax. Create another variable `alphabet` which is a range of letters of the whole alphabet (from 'a' to 'z'). Go to https://ruby-doc.org/core-2.6.1/Range.html to see how this is done (I always forget how ruby ranges work so I'll always start with the docs). | |
# 16. Ask the user for three or more grocery items, store them in an array. It's OK to ask three times. Show the list to the user. Can’t remember how to use a specific ruby feature/syntax? That’s normal! Google + Ruby docs. | |
# Try using the `.length` method to inform the user how many items are in their list. | |
# Sort the list alphabetically before showing it to the user. | |
# Are you using `gets.chomp multiple` times? A 3.times loop might tidy your code. Have a go at using this loop. | |
# If the user tries to add 'Ice Cream', secretly replace it with 'Broccoli'. | |
# Now ask the user for quantities too. Show a nice looking list back to the user. | |
# Loop Drills | |
# In these drills you may need to print out some useful information to confirm that things are working as you hope. You can print something to see the loop running, and you also might like to print the 'counter' that you are using to track the loops. | |
# 1. Create a while loop that runs 9 times. | |
# 2. Create a while loop that runs 3 times. | |
# 3. Create a while loop that runs 22 times. | |
# 4. Create a while loop that runs 5 times and ends when the counter is equal to 0. | |
# 5. Create a while loop that runs 8 times and completes when the counter is equal to 16. | |
# 6. Create a while loop that runs 4 times and finishes when the counter is equal to -2. | |
# 7. Create a while loop with a counter that starts at 0, ends when the counter is at 9, and runs three times. | |
# 8. Create a while loop with a counter that starts at 0, that ends when the counter is at 100, but that only runs 5 times. | |
# 9. Create a while loop with a counter that starts at 0, that ends when the counter is at -15, and runs only 3 times. | |
# 10. Create a while loop with a counter that starts at 14, that finishes at 49, and runs 5 times. | |
# More Loops | |
# 1. Set a variable of `num` to 5 (integer) then run this code. | |
# while(num == 5) | |
# puts "hello world" | |
# end | |
# What's happening here? Why isn't the loop stopping? What kind of loop is this? How can we stop this loop? | |
# 2. Assign 4 to the variable `num`. Run the same code in question 1 again. What's happening this time? | |
# 3. Now let's write our first working while loop. We need a counter and a way to increment the counter. Set a variable `counter` to equal 0. Create a while loop with the condition of that counter is less than 5. In the body of the while loop puts "Counter is currently #{counter}". After that we need to increment the counter by 1. We have two options syntax wise to increment. What are they? | |
# If you're getting: | |
# "Counter is currently 0." | |
# "Counter is currently 1." | |
# "Counter is currently 2." | |
# "Counter is currently 3." | |
# "Counter is currently 4." | |
# You're done! | |
# 4. Change the while loop so that the final line thats printed is "Counter is currently 9." | |
# 5. If the counter is 3 instead of "Counter is currently 3", I want you to print "Special number 3!" | |
# 6. Create an array of five food items. Using a while loop iterate through the array and print each item to screen. | |
# 7. Keep the same while loop from above with one new addition: when the iteration gets to your favorite food (pick an item from those you added), and print that "<this> is my favorite", but instead of <this> use string interpolation to specify the item. | |
# 8. Create a counter and set it to 0. Generate a random integer between 1 and 100. Create an empty array with the variable name of `bool_array`. Create a while loop that loops until the counter is greater your randomly generated number. In the loop body, if the counter is odd push `false` into the array, and if the counter is even push `true` into the array. | |
# 9. Create an array variable named `coding_languages` with four different programming languages in it. Call `.each` on this array, and print out each language from the array. | |
# 10. Repeat question 6 and 7 from the while challenges using `.each`. This time we won't need a counter, but use this inbuilt array method. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment