The assignments listed here should take you approximately 2 hours.
To start this assignment, click the button in the upper right-hand corner that says Fork. This is now your copy of the document. Click the Edit button when you're ready to start adding your answers. To save your work, click the green button in the bottom right-hand corner. You can always come back and re-edit your gist.
Documentation of a language, framework, or tool is the information that describes its functionality. For this part of the practice tasks, you're going to practice digging into documentation and other reference material.
NOTE: The linked documentation for each question below is a good starting place, but you should also be practicing your Googling skills and sifting through the results to find relevant and helpful sites.
-
In your own words, what does the Ruby array drop method do? As you're explaining, be sure to provide an example. Your answer:
The drop method is a ruby array method that is used to drop the first
nelements of an array. It returns the remaining elements of the array. If0is given, no elements are dropped. Ifnis greater than or equal to the number of elements of in the array, an empty array is returned. For example:arr = [12, 4, 2, 6, 0, 4] arr.drop(3) => [6, 0, 4]
-
What did you Google to help you with this task, and how did you pick your results?
I googled "drop method ruby". The results that came up included apidock.com, rubycuts.com, and the Ruby documentation. I opened all three to see the differences and formed an explanation off the most straight-forward one, the Ruby documentation.
-
In your own words, what does the Ruby string split method do? As you're explaining, be sure to provide an example. Your answer:
The split method is used to divide strings based on the input delimiter and create an array with the results. This method has two optional inputs:
- A pattern, which is used to divide the string into array elements. It can be a string or regular expression (regexp). Every time an instance of the input pattern is found in the string, the array element is terminated and the next element is created. If nothing is input for the pattern, the string is separated by whitespace, " ".
- A limit, or integer of any sign that is used to stop the splitting of the string. If only four array elements are desired, the limit is input as
4and the fourth array element contains the remainder of the string, even if more instances of the delimiter exist within it. If no limit is input, trailingnullfields are omitted from the created array. If the limit is a negative integer, there is no limit to the number of array elements created and trailingnullfields are included in the output array.
# With no input pattern or limit str = "hey there sunshine" str.split => ["hey", "there", "sunshine"] # Input pattern but no limit nums = "1,,2,3,,4,," nums.split(",") => ["1", "", "2", "3", "", "4"] # Input pattern and limit nums.split(",", 4) => ["1", "", "2", "3,,4,,"] # Input pattern and negative limit nums.split(",", -1) => ["1", "", "2", "3", "", "4", "", ""]
-
What did you Google to help you with this task, and how did you pick your results?
I googled "ruby split method", which returned the Ruby documentation, apidock.com, and a number of blog articles. I looked through a few before focusing on the documentation, which was the most straight-forward. The examples used in the blog articles were helpful though. I also googled "ruby regexp" and used the Ruby documentation to find out what the regular expression class is in Ruby.
-
In your own words, what does the JavaScript array slice method do? As you're explaining, be sure to provide an example. Your answer:
The JavaScript slice method is similar to the Ruby drop method, where elements from the input array are removed and the new resulting array is returned. However, the slice method has two integer input values,
beginandend, between which the indexed elements are included in the output array. It is important to note that theendindex is not included in the resulting array. If noendvalue is input, the remainder of the array is included in the ouput array.var fruits = ["apple", "banana", "grape", "pear", "kiwi", "orange"] // With begin input but not end console.log(fruits.slice(3)) // expected output: Array ["pear", "kiwi", "orange"] // With begin and end input console.log(fruits.slice(2, 5)) // expected output: Array ["grape", "pear", "kiwi"]
-
What did you Google to help you with this task, and how did you pick your results?
I googled "javascript slice method", which returned the JavaScript documentation, w3schools.com, and freecodecamp.org results. After hearing that w3schools can be outdated, I looked into freecodecamp.org, which was helpful at clarifying the questions I had from looking at the documentation.
Imagine that you're taking your favorite board game and turning it into a computer-based game.
-
Name of board game: Ticket to Ride
-
Use the space below to categorize game data into each of the following data types. You should have a minimum of two pieces of data for each category.
- String data: route_name, player_name
- Integer and/or float data: route_length, num_cards, player_score
- Boolean data: route_complete?, has_longest_train?
- Array data: routes_completed, routes_to_finish
- Hash or Object data: cards_in_hand (
{ "pink": 2, "blue": 1, "black": 5, "wild": 1, "red": 2 }), player_scores ({ "eric": 47, "josh": 72, "amy": 82, "michelle": 59 })
-
Create a list below of three real-life situations where iteration is used. For each situation, explain why it would be an example of iteration.
-
Putting away clean laundry. This is iteration because the actions of picking up, folding, and putting away are repeated for each clean article of clothing.
-
Making meatballs. This is iteration because the actions of rolling into a ball and placing into a pan are repeated for each small portion of meat.
-
Planting seeds in a garden. This is iteration because the actions of digging a small hole, placing the seed inside, and burying the seed are repeated for each seed.
-
Create a list below of three programming situations where iteration would be used. For each situation, explain why it would be an example of iteration.
-
Normalizing an array of name strings that users have input to have the first letter capitalized and the rest lowercase. This is iteration because the actions of downcasing and capitalizing the first letter are repeated for each name in the collection.
-
Calculating and storing the maximum heart rate of an individual from an array of ages. This is iteration because the actions of plugging the age into the maximum heart rate equation and storing the result in the new array are repeated for each age in the collection.
-
Changing the background of a web page every 1 second through an array of hex color codes. This is iteration because the actions of changing the background to the hex color and waiting for 1 second are repeated for each hex color in the collection.
The following code examples each contain a mistake. Describe the problem for each.
| Original | Mistakes | Problem |
|---|---|---|
| students.each do |student| puts "Welcome, #{student}" end |
students.each do |student| puts "Welcome, #(student)" end |
The problem is that parentheses are used instead of curly braces for string interpolation. Ruby would not recognize that interpolation is the intended result and would literally print out "Welcome, #(student)" instead of replacing it with student names. |
| .main-content { font-size: 12px; border: 3px solid black; font-family: sans-serif; } |
.main-content { font-size: 12px; border: 3px solid black; font-family: sans serif; } |
The problem is that the font specified in the CSS font-family property is not hyphenated. |
| log(2, (1022 * ((score - min(score) over ()) / ((max(score) over ()) - (min(score) over ()))) + 2)::numeric) | log(2, (1022 * ((score - min(score) over ()) / ((min(score) over ()) - (min(score) over ()))) + 2)::numeric) | The problem is that the first term in the denominator reads min(score) instead of max(score). This would result in the same number being subtracted from itself, or "0", in the denominator, leading to an error. |
| arr.product(arr).reject { |a,b| a == b }.any? { |a,b| a + b == n } | arr.product(arr).reject { |a,b| b == b }.any? { |a,b| a + b == n } | The problem is that the reject method's condition that "a == b" is replaced by "b == b", which would always trigger the rejection since "b == b" is always true. |
| class Cat attr_reader :color, :name def initialize(data) @name = data[:name] @color = data[:color] end end |
class Cat attr_reader :color, :name def intialize(data) @name = data[:name] @color = data[:color] end end |
The problem is that "initialize" is spelled wrong in the class declaration. This would lead to an error message. |
- Watch this video and follow each step to modify your own bash profile. As mentioned in the video, you will need this snippet below:
# get current branch in git repo
function parse_git_branch() {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
if [ ! "${BRANCH}" == "" ]
then
STAT=`parse_git_dirty`
echo "[${BRANCH}${STAT}]"
else
echo ""
fi
}
# get current status of git repo
function parse_git_dirty {
status=`git status 2>&1 | tee`
dirty=`echo -n "${status}" 2> /dev/null | grep "modified:" &> /dev/null; echo "$?"`
untracked=`echo -n "${status}" 2> /dev/null | grep "Untracked files" &> /dev/null; echo "$?"`
ahead=`echo -n "${status}" 2> /dev/null | grep "Your branch is ahead of" &> /dev/null; echo "$?"`
newfile=`echo -n "${status}" 2> /dev/null | grep "new file:" &> /dev/null; echo "$?"`
renamed=`echo -n "${status}" 2> /dev/null | grep "renamed:" &> /dev/null; echo "$?"`
deleted=`echo -n "${status}" 2> /dev/null | grep "deleted:" &> /dev/null; echo "$?"`
bits=''
if [ "${renamed}" == "0" ]; then
bits=">${bits}"
fi
if [ "${ahead}" == "0" ]; then
bits="*${bits}"
fi
if [ "${newfile}" == "0" ]; then
bits="+${bits}"
fi
if [ "${untracked}" == "0" ]; then
bits="?${bits}"
fi
if [ "${deleted}" == "0" ]; then
bits="x${bits}"
fi
if [ "${dirty}" == "0" ]; then
bits="!${bits}"
fi
if [ ! "${bits}" == "" ]; then
echo " ${bits}"
else
echo ""
fi
}
export PS1="\u\w\`parse_git_branch\`$ "
If you have any questions, comments, or confusions from the any of the readings that you would an instructor to address, list them below:
@johnktravers overall nice work on this, remember with arrays to always start with a collection, and to perform an operation for each item in that collection.