# SOLUTIONS! Solve The Errors, Learn For Loop

SPOILER ALERT! In this video are the full solutions to the For Loop problems you can solve. If you haven't tried yet, copy the sheet and make an attempt to debug all 9 For Loops.

Sheet Resources

Video Transcript

00:00 Alright, before we go too far, double make sure you're watching the right video. This is the solutions video. If you have not tried yet, go to the other video or go down below copy the sheet to your drive and go ahead and give it a try yourself.
00:18 If you're giving it a try and you're like, oh my God, I can't. I cannot figure all of these out.
00:23 This is the place to find the answer. So in the last video, I set up this idea. We have nine flags together or nine goals here.
00:31 We're getting one flag, two flags, three flags. We have a four loop. We are running through. The four loop has been misappropriated.
00:39 It has been bugged. We, We have to debug it and we're gonna have to read these through these errors or get through a lack of errors and figure out what is the solution.
00:48 Fix these errors. So first off, again, make sure you make a copy in your Google Drive. So I'm gonna make a copy.
00:54 I'm gonna call this solutions. And I probably will put the original, link down below, not the solutions sheet. I know that might be dirty.
01:07 That might be weird to you. But you gotta watch this video to see the solutions. I'm gonna walk through them and walk through what problems occur.
01:16 Again, in the other video and in the hints, if you got through all the hints, I mentioned, that this last one, this 9-1, has gotten me twice in the last year.
01:26 So these are real errors that happen and I hope that by going through these exercises, you can see how we debug and how these errors can actually give us a lot of information as to what to, You'll see they run us dirty, they do us dirty, they don't tell us exactly what's wrong, they just say return
01:49 date is not defined. Okay, what does that mean? Oh my god, well we'll go through that in this video. This is the solutions video.
01:55 I'm gonna actually call this solutions video alert. Spoil alert. From this moment on, we are going, We are going to be solving these step by step.
02:04 We have nine goals. The first three are easy, the second three are maybe a little bit harder, and the last three are harder.
02:13 So, let's open our Apps Script and get going. Alright, let's go through the error first. Error I is not defined.
02:21 Alright, let's go through the error first. Let's go to line five. Great. Go to line five. There is an F here.
02:27 It's I. It should be I equals zero, I data, length, I plus plus. So, that is the first solution here.
02:34 And let's just Command S save and see if we have the answer. We have the answer. The flag, it is done.
02:41 We have, Capture the first flag. Second one, I is, is that an I or an L? This is hard. Okay, this is not hard, but this happens.
02:51 Sometimes you're like, a lowercase I is definitely easy to find. But an uppercase I and a lowercase L are very easy to get.
03:02 Sometimes easy to see. It's hard to see the difference. Alright, there it is. We need an I there. Command S, let's see if we solve that.
03:12 There it is. Boom, we're two out of nine done. T is not defined line 29. Probably same thing. 29 I equals zero I is less than data dot length.
03:25 T, that T looks more like an I than that L did. Right? Alright, let's change this to an I. Command S.
03:33 In C does this change the, we're done. Great. We got the first three. Easy, PZ done, right? If you got those, great.
03:41 If you didn't get those. And you were watching this video for those, feel free to stop the video and go try the other ones too.
03:48 It's alright. It's totally fine. The for loop is a bit tough if you never coded before. Alright, let's look at number four.
03:55 Array result was not expanded because of an overwrite D16. What was our hint here? I actually forgot what. What is our hint?
04:04 What did I give you as a hint? Oh, it might be a malformed if statement. That's what it is. So let's look at that.
04:12 Here's get four flags. Four is totally fine. We don't have a heart. It's all eyes if data is equal. This is a tough problem because it doesn't look wrong.
04:27 It should be double equal sign. The equal sign is used as a, is to give, like the word is, is.
04:37 Data is this. It's to assign a variable. But you need two equal signs to. See if two things are equal.
04:44 So let's see if that fixes it. Command s. We have four flags. Great. Let's move on. Reference does not exist.
04:52 Wow. That is a, that is a weird one. Quite abstract. Ah. So it's a variable that's not written correctly.. Is it?
05:07 No. I mean, I know what it is because I made the error, but looking at this if statement. Okay. So if data is equal to one, we are doing flags dot push, but we're pushing return data in.
05:23 To flags here and we're returning the return data. This is backwards. So this is actually should be return data is the original array and then we're what we're pushing into that each time is flags.
05:37 There see not exactly. That's what we need to actually push the flags into the array of return data not the return the flags is pushed into array.
05:52 So this is a very common thing to mess up if you've never coded before and you don't understand the dot push function.
05:59 You can always reverse those two and see but actually let's. Alright we fixed it. Yep. Number six. Goal six. We have no error.
06:09 Oh. What is our hint? If you haven't gotten a hint yet, hints were very helpful. I think. So there's no error.
06:16 The function is working perfectly. Oh, I, I wrote some logs. That's what I did. Alright, so we wrote some logs.
06:23 Let's. Go through those logs and just see what is going on. Like one of the problems one of the ways to solve problems I find is just to see what's going on.
06:33 Logger.log stuff that everything from the beginning to the end. The first two things we can log are the data and the flags that we are getting.
06:43 What does. Six flags. Alright, we're going to run. We need to authorize this. We need to review permissions. We need to authorize it.
06:51 Allow it. Alright, great. Okay, so we're getting all these zeros and some ones here and we're only getting one. No flag here.
07:06 So let's look at these spreadsheet data dot get range dot get values. Ah, we're getting dot get value here. So that is the problem.
07:15 If we just change dot get value to got that get values. Very tiny problem. Very common that we're. We're going to misappropriate get value for get values.
07:28 We want to get the values of this whole range, not just one value of the range. We want to get all the values in the range.
07:35 So let's see if that fixed it. It did. We're fixed. Cool. Moving on to number seven. Return data. It is not defined in line 82.
07:46 Return data is not defined. You're right. It is not defined. But the error is not in line 82. It's the problem is that we have no return data before.
08:02 So one thing if you here's actually one. Way I found to understand it is I do command F. For return data.
08:11 And you see from the function to here, there's nothing. So that's why it's not defined is because we haven't defined it here.
08:18 Look, look at this one. It has variable return data equals to, to curl. Not curly brackets, square brackets. So we need variable return data equals, and this is a blank array.
08:32 This is not curly brackets. These are square brackets. You can also do new array. In this case, that will work.
08:40 But I like just doing this square brackets because it's sort of just to. Two keystrokes, two really simple. Did we solve it?
08:50 Was it seven? Yeah, that was seven. Okay. We solved seven. Done. Awesome. Two more to go. See the error we're getting here.
08:59 Reference error. I is not to find line 93. Let's go to line 93. Oh, we just haven't written the for loop.
09:07 So we just need to write a for loop. If you've written a for loop before, this is going to be actually easy.
09:11 But if you have never written a for loop before, you're like, oh crap. I have to write it again. I have to actually write it out.
09:16 I equals zero. I always have to do it. You always have to do this for memory, right? Is I is less than.
09:25 Data dot length. Semi colon, make sure you have the semi colons here. And then I plus plus. I think that's it.
09:33 That might have been an easy one. You might have gotten that. If you got through it, if you didn't give up, you're like, oh, I'm going to get one of these.
09:39 That might have been the easiest one to get, even though it's in the hard section. There it is. It's done.
09:43 Here's the hardest one. We got, because we have the answer. Everything's here. Get nine flags. Everything's here. We got our array.
09:56 We got the pushing. We got the return. What is wrong with this? I mean, I know what's wrong with this because I set this up.
10:04 But let's look at the. Hint. There's no error. In fact, there's one flag. You'll have to move one line of code from one way from one place to look at the order of variables and imagine how creating a blank array of various points might affect the final answer.
10:17 Blank array. So it has something to do with this. If. So we're looking at the. Eight all length and we're creating this is this variable return data equals a blank array is a creation.
10:32 We're creating it. So if we're looping through that, we're creating it every single time. So this. Actually should be before the for loop.
10:45 It is in the other one. So you can easily cheat by not cheap. You can get the hint is look at all the actual ones that work, right?
10:53 The variable return data equals a blank array and then the for loop happens. And so let's see if that solved it.
11:01 It solved it. But I will tell you this. This last one. Has been the main of my existence twice. It has happened to me twice within the past year.
11:11 During workshops live, we were creating for loops and we're like, why is this not working? This is why it wasn't working.
11:17 Exactly this. We just put the vera, the new array inside the for loop in the wrong place. We needed to put it beforehand.
11:25 And. Solved to now you are an ace at four loops and debugging them, right? That's a lot of different errors and a lot of different errors solved.
11:35 Again, down in the description, you're not going to get the solution to this script but you will get the original if you, if you go below and get the sheet, you'll not get this.
11:46 The solution sheet you will get the the, the sheet so you can do these, you can go through these problems and debug them.
11:54 Even though you just watch the solutions, go through it, do the typing, figure it out. That's going to make learning so much easier is actually doing it.
12:02 Thanks so much for watching. Bye.

Better Sheets