This course has been updated! We now recommend you take the Complete Intro to Computer Science course.

Check out a free preview of the full Four Semesters of Computer Science in 5 Hours course:
The "Exercise 6 Solution" Lesson is part of the full, Four Semesters of Computer Science in 5 Hours course featured in this preview video. Here's what you'd learn in this lesson:

- Brian walks through the solution to exercise 6.

Get Unlimited Access Now

Transcript from the "Exercise 6 Solution" Lesson

[00:00:00]
>> [MUSIC]

[00:00:04]
>> Brian Holt: So push, all you're gonna do is, you're just gonna add on to the end, right? So this one's pretty easy, so you're gonna say this.data, this.length, = value, right? That's just gonna add it straight on to the end, and then you're gonna increase length, right? And that's it.

[00:00:27]
>> Brian Holt: And that's all you need to do for push. Something worth mentioning that we're also not worrying about here, when you're allocating memory, you have to allocate blocks of memory at a time, right? So typically, you wouldn't just say, give me 5 spaces, probably what you'd say is, give me 200 spaces.

[00:00:48] And then you can start filling those up and then once those start getting a little too filled, then you'll say okay, give me 400 blocks, right? That's usually the way that you're working with memory. Again, we're in a garbage collected language that we don't have to worry about blocks of memory, like we have no concept of it.

[00:01:04] But that would be something you would check for here as well as in push cuz you're adding things on there you would wanna possibly allocate more memory here. Okay, pop, remember, pop has to give you back what you asked for as well. So you're gonna say this.data,
>> Brian Holt: Of this.length-1, right?

[00:01:28] So that's gonna be the last thing in the array, and then you can do this a couple ways, I choose to just use the delete key word at this.data, this.length-1. Some people don't even know that deletes a keyword in JavaScript, that actually works. It does, you just don't happen to need to use it very much.

[00:01:49] Then you'll say this.length-- cuz you made it smaller. And you're going to return whatever you popped. I guess we should be looking at it from solving.
>> Brian Holt: What did I do here?
>> Brian Holt: Returns, yep, return, thank you. Okay, so that gives me push and pop, right, so those unit tests are passing.

[00:02:18]
>> Brian Holt: So let's do get. This is the easiest one, this.data[index].
>> Speaker 2: We've got a question related to delete.
>> Brian Holt: Sure.
>> Speaker 2: Mark M is asking, for pop, can we just reuse the delete function that we're going to write?
>> Brian Holt: Yeah, you could. Yep, you totally could.
>> Brian Holt: That's probably even smart, it's good code reuse.

[00:02:53] So, we'll even do that here in just a sec, so let's do delete,
>> Brian Holt: const ans, answer, whatever you wanna call that, return value it's called. Anything I know I'm gonna return, I always just call it answer because why not? this,
>> Brian Holt: .data[index], right? And then we're gonna do our collapseTo here, this._collapseTo(index).

[00:03:25] And then return ans;, okay? Then let's do collapseTo. So, here, we're just going to let i = 0.
>> Brian Holt: Sorry, let i = index, rather, i < this.length; i++.
>> Brian Holt: this.data[i] = this.data[i+1], so basically, we're moving everything over 1, right? That's the basic idea here. We're going delete the end.

[00:04:18]
>> Brian Holt: this.data[this.length-1], right? And then we're gonna do a document here.
>> Brian Holt: And that passes all of our tests. So to someone's point earlier, I forgot what his name is,
>> Brian Holt: If you wanted to do it, you could do pop this way, which would be return this.pop(this.length-1), right?
>> Brian Holt: Or is it the cell length, no.

[00:05:11] Sorry, I called it self. Good job, Ryan, delete.
>> Brian Holt: And that would work as well. So if you wanna reuse your code there as well.
>> Brian Holt: Nice.
>> Brian Holt: Any questions about array list?
>> Brian Holt: So, the primary thing to garner from this [COUGH] is that delete is expensive, right?

[00:05:45] That shifting everything over in memory to make sure that everything is in the right place when you go for a get is expensive. So if you need a data structure that you need to be able to delete from a lot, that's not a good idea. However, if you don't delete very much and all you need to do is be able to get super fast, this is the best, like you can't beat this because you essentially know exactly where it's gonna be in memory.

[00:06:11] And that's really powerful. Trade-offs, right? I keep saying that word over and over again, you need to make trade-offs, you need to analyze the problem that you're getting at. And you need to figure out, do I need to delete a lot? Do I need to get a lot?

[00:06:25] Do I need to do both of them a lot? That's what you need to decide. So we're going to look at the other side of the coin now, which is linked lists, which have super easy deletes. However, gets end up being really expensive with linked lists. So if you need to be reading a lot from a linked list, kinda suck, but if you need to be able to delete a bunch or add a bunch of stuff in the middle, then linked lists are awesome.

[00:06:51] Yeah?
>> Speaker 2: There is a question from Jan Heine. Why do you introduce the helper function in the delete?
>> Brian Holt: I like to keep my methods as single purpose as possible. This makes this collapse to now testable, so if I were to go out and write unit tests for my collapseTo method, I could make sure that just that piece is working correctly.

[00:07:17] And so now, if my delete fails, at least I can be assured that my collapseTo is not working and now it's multipurpose, right? You could also gentle purpose this. We don't do insert into, so if I wanted to add another value at index two and your length is ten, you could also make that instead of being able to just collapse, you could also make it expand as well.

[00:07:44] And that would general purpose that method to be able to do that, or you could, rather. So I use helper functions basically. One, cuz they're testable, two, cuz they're smaller and easier to understand. Just good code separation, in my opinion.