Learn With Me | Intro To Elixir Lesson Two - Immutability Homework

Elixir Web Development JavaScript
Transcript

English (Auto-generated)

So so I just watched the second video in the intertidal lecture series about mutability and I'm going to go ahead and do the homework that he suggested we do. Um and hopefully I don't suck at it. Uh Let me just do results. Alright, so for a you won't give it a key value data structure. I'm gonna use javascript for everything. Just F Y. I um let me actually capitalize the keys. So we're going to do it over here. Let me get rid of this actually, you know what? Let me just make a new file that just says um homework dot Js and a Okay. So we're going to give it a key value structure, capitalize the keys. So let's go ahead and copy this one here to let info. Let me open the terminal as well. Okay, so we want to capitalize things so reassigning this one of the object will overwrite it. I'm pretty sure. So if we do let info one, it's equal to info and then we were to do info dot name is equal to john console dot log For 1.4. Mhm. Um We'll do where my hair node cd listen to dance. Yes. We've drawn with the capital there. Okay. So what we want to do is make it so that this won't capitalize it. So I feel like a quick solution for this would just be to actually redo the object. So if we were to do info, it's equal to name, john career programmer, city Vancouver. We should be able to complement logged out info that name and have that be capitalized but also console dot log out info one dot name and have a bit lower case. So let's see. Yay. So that's lesson one solved. It's very kind of like a quick solution, but because we're reassigning and not changing it, it's not mutating it. So that one's kind of simple. Um, as far as homework. All right. So what's the second one? So given a string such as I'm gonna actually just copy the string over capitalized Every letter whose position is divisible by three, assume the first letter has a position of zero notice if you mutate the original string or not. Okay. So we're going to do um, let's do what string equals this. We want to capitalize. So let me read this again, capitalized every letter whose position is divisible by three. So if we were to do console dot blog. Mm hmm. Spring one. Uh, just go ahead and comment these out. We have. Okay, perfect. So what we're gonna do is I'm gonna write a function. So let's write but capitalize upgrades. Pick up. I was like, why can I not spell string? We're taking a string. Um, and then what we're gonna do is we're gonna find we're gonna inside the index and we're going to say if string got now if string index five by three. Thank you. Zero console dot log I think next let's say capitalists string string. So what happens here? I realize his if string index Oh, I need to obviously iterate through the entire thing. Yeah, strength index is divided by three string. I okay. So we're gonna do, Let's start with index equals zero. For what? I? Mm hmm. Actually we don't need index at all. But I is equal to zero. I is yes. Listen or equal to string dot length. Alright. Plus plus. And we're going to move all this stuff up here. Let's try that again. Plus Plus. I'm doing something fatally wrong. I know I am. It's just been so long since I've written anything like this. 24. How many characters you have here? 12,345,678,910. Okay. 23456789 10, 11, 12, 13, 14, 15, 16 To one day to 23. Um but includes zero. So I don't want equals um string dot I it's like string Oh, because I'm saying oh my gosh, duh. If I divided by three freaking brain, W divided by three is equal to zero string. I okay. There we go. I wasn't using the was using the wrong thing. Not divided. I wanted the remainder. Okay, so that should be correct. So we have w so one 0123456. Yeah. Okay. So perfect. So now what we want to do is we're going to do, we're gonna take this string. We're gonna do a string but here at I thought to upper case. Oh look and then we're gonna do the console strength. It is. I did something wrong there. Is it not to uppercase? Thought it was to uppercase. Okay. Next.2 uppercase. Uh maybe I just can't car at Oh, so let's do this flett string, that new string. Uh huh. Mhm mm hmm. Um the upper case is not a function. I just spelling it wrong. Mm hmm, yep, yep. I am. Okay. So now what we're gonna do is we're going to say so if we were to now, is that this would probably, this will reassign it. So we're gonna do console dot blog. Thank you. Oh, this doesn't reassign it. String. Car I to uppercase, whoops. I don't want to do it in the loop. So what if we did let new string? Mhm. It's so, yes, it's beautiful. It's complicated when you do this, but this is only in my brain is working right now. So what we're gonna do is we're going to stay string dot char at upper case um you know, let's just console. All aggressive. Um and yeah, okay. W Okay. And then we're going to say else Ministering is equal two strength. Yeah, if we were to do string high, it's equal to and then else and then we console dot log string. That would probably mutate it. The word strength does not look like english to me anymore. So that doesn't mutate. It's the string. I is equal to console dot log during high Nice. Equal tail. Mm hmm. Okay. So what new strength? No, not yet. So new string else. New string I equal to string. Hi yes. String. It's a property of zero of undefined. Yeah. Oh um okay. Mm hmm. Signing it every time. Yeah. So if we did consider a new string that will be changing it. But of course now if we also don't do this and then cause of that log string and but obviously oh it did change strings interesting in truth. No, I didn't. I just can't read so I didn't change string. I was like that doesn't make any sense. And then we have to capitalize there. But if I wanted to change it it would be an entirely different process. But I wanted to do it without mutating it. Um I think if we wanted to change it. So this is gonna be beautiful canceled detail for a second here. And these so we could say, I wonder if this would make, I don't think this would work. Return strength. Oh and I need to console dot log it because of the fact that we're on the terminal. Yeah. So that doesn't mutate anything. So string care at to uppercase. Yeah. Mhm Right. Mm hmm. String I and Tara. I feel like that should work. Mm hmm string. I shrink hair at I two. Uppercase apparently cannot mutate this data. If I try um we're going to say. Yeah. So it's getting the correct one and then it is equal to stream carrot. I to uppercase. So, if we did this, I see. So it is look strength just reassigning this. Oh, so because it's just going to do that specific character. But I feel like process what can I type valid left hand side assignment string. The index of pie is going to be equal to this. I feel like that should work. But it's just not doing it. Mhm. But when we did it for new string, it works perfectly. So it's like we literally can't mutate it with this. Maybe it's not immutable function to uppercase to method does not change the original string. How does he read? Okay, so that would be why two local upper case. It's not changed the original string. Okay, well, I'm gonna leave it at that. Um because it's just an exercise and we know that it isn't immutable there on this video is way longer than I thought it was gonna be and spent a long time building around javascript. But the important thing was is we figured out that they're both not or how not to mutate it both ways. Let's do this one here before I saved there. And we'll confirm again that the original string does not get mutated per the upper string to uppercase there and assign it to new string. And even if we sign it to the new string, it's not and it's not changing it. So um yeah, to replicate seems to be an immutable um function there. All right, let me know if you give way easier time with the homework than I do because apparently my brain likes to take its time with it.
184 Views 2 Likes 0 Comments

Do the homework from lesson two with me! (In which the real lesson was I should always read docs when I'm rusty).

Comment
Leave a comment (supports markdown format)