Day 14 ended up being a rest day for me, but I got back into a coding rhythm today.
slice(), and using the spread operator (
...). Here are my notes from today's session:
Quotes & Key Ideas:
- Arrays have a length property which can be accessed with the syntax
- “The fundamental feature of any data structure is, of course, the ability to not only store data, but to be able to retrieve that data on command.”
0). Item within arrays can be accessed using bracket
notation, for example
arrayaccesses the first item in the array called
array. Often, we will assign an element of an array to a variable for easy access (i.e.,
element = array). We can also re-assign (i.e., replace) elements within an array using the bracket notation:
array = "new element";will re-assign the second element with the new string
- Arrays are mutable and can be modified using the methods of
push()adds an element at the end of the array and
unshift()adds an element at the beginning of the array. These methods can accept multiple arguments, including variables.
unshift()have corresponding methods that are nearly functional opposites:
shift(). As you may have guessed by now, instead of adding,
pop()removes an element from the end of an array, while
shift()removes an element from the beginning. The key difference between
shift()and their cousins
unshift(), is that neither method takes parameters, and each only allows an array to be modified by a single element at a time."
splice()allows us to remove any number of consecutive elements from anywhere in an array. This method can take up to 3 parameters; the first parameter represents the index of the array that
splice()is being called upon, the second parameter (optional) represents the number of elements to delete, and the third parameter (optional) represents new elements to be added. "
splice()not only modifies the array it's being called on, but it also returns a new array containing the value of the removed elements"
slice()copies or extracts a given number of elements to a new array, leaving the array it is called upon untouched. It "takes only 2 parameters — the first is the index at which to begin extraction, and the second is the index at which to stop extraction (extraction will occur up to, but not including the element at this index)"
- “ES6’s new spread operator allows us to easily copy all of an array’s elements, in order, with a simple and highly readable syntax. The spread syntax simply looks like this:
..." For example,
thatArray = [...thisArray]will copy all of the elements from
thisArrayinto a new array assigned to
At the beginning of my coding session I was feeling a little sluggish and sleepy, but listening to the Battlestar Galactic song Prelude to War and using an interval timer helped me stay focused.
I really enjoyed today’s challenges because they worked a lot of with arrays and indexing of arrays. I have a pretty solid understanding of how indexing works, but sometimes it takes a bit effort to wrap my head around it in practice. Today’s challenges were full of practicing the indexing of arrays by adding and removing elements.
I also enjoyed some of the logic problems introduced with creating functions that copy arrays based on a given number. These problems are starting to make use of all the previous topics which is nice to see it all coming together. I would imagine it’s similar to when you first start learning to read. Yeah, it’s great to be able to write out letters and spell words, but it’s not until you’re able to read full books that you really get the benefit of all the writing practice.
Overall, today’s session felt great (even if I was a bit drained from the first week of school).
Link to work:
For my progress visit the timeline on my freeCodeCamp Profile.
You can read the full log of my #100DaysOfCode journey on GitHub.