Hey there stranger!

Sign up to get access.

Solve The Errors, Learn For Loop

About this Tutorial

Learn how to debug a For Loop. These are common errors that happen when coding in Apps Script, specifically around the For Loop. This is Part 1 and 2. Get the goal of capture the flag exercise and I give you hints to each one.

Video Transcript

00:00 Alright, this is gonna be a fun video because this is part one and two of a three part video in order for you to learn the for loop.
00:08 So in other videos I've done here on BetterSheets we go through the for loop. We go through a problem you might have or you don't have or someone has.
00:17 We'll build a for loop step by step and then we will. Then see the for loop and you'll see how it works.
00:24 But I wanted to get one video and one process and one set of projects. And this sort of fun game, a capture the flag sort of game, an error hunting game.
00:35 In order to give you the ability to do this nine times, see a real in order to give you the ability to do this nine times, see a real, For loop that actually works and then debug it.
00:44 Because in the real world and when you're actually creating a for loop, you're going to mess it up the first few times that you do it.
00:51 And even after years of knowing how to do the for loop, adding other elements to it and if statement of, an array function to it, you're going to have errors.
01:00 So I have set up a for loop and then created nine errors. So the game is this. I'll give you the game now.
01:11 I will have a spoiler and then I will give you some hints. In another video, I will give you the solutions.
01:18 So if you're watching. In this video though, you will in the description somewhere here be able to get a copy of this and play for yourself.
01:27 So let me set up the game. It is capture the flag. You will be having to capture nine essentially flags and the first one you'll get one flag.
01:38 What it is is we have set up literally. Column A is all zeros except for if you have in one there is one one.
01:48 In the B column it will say the flag. And then in the get two there will be two and in get three there will be three.
01:58 So this is a very common issue where you're just sort of going through, a lot of data and in one column you need to pick out something.
02:04 And then in the B column you need to get whatever is related to that. So here you see the one, you see the flag.
02:11 This is what should be here. And I might give you a little in one of the hints I might solve one for you or show you what it looks like.
02:19 But when you fix the error you, you will get a flag here and it will be done. There are three easy ones where I think that the error here will focus your attention properly to where you will find the error and you will be able to fix it very easily.
02:37 Not very easily, but easily. The medium ones, there is more problematic errors and then there is maybe even no error at all.
02:49 There it is. There is no error here. So how do you solve that? I'll give you some hints later in this video.
02:55 Then there's 7, 8, 9, which have much harder problems to solve and, It looks great. It looks, Oh, line 82.
03:05 It looks like it's easy to find. Check again. You'll see it is much harder these three problems. So you have in the Apps Script, so as long as you get this script, as long as you get this sheet, go to Extensions, Apps Script, load up the app, Apps Script, you have two files here, code and hints
03:25 . If you do not want to use the hints, don't worry. You do not have to use the hints. I won't show you them here.
03:29 I will give you a spoiler break, and then we'll go through the hints. The code looks like this, and it has a function.
03:35 There are two arrays of data that we're getting. We're getting them all from get1, get2, get3. You can read that.
03:43 We're getting values. We're getting the variable return data. We're creating that as a blank array. Then we're going through a for loop here.
03:52 We have a for loop, the length of the array of data. We're finding where it says one, and then we're pushing to the array e- each time that there is a one we're going to push whatever is in the flags column here, flags array, we'll push that over to the array, and then we'll 
04:15 return that, which is going to be the flag. So for one, forget one flag, the answer is going to be the flag once.
04:22 Forget two, you're going to have two flags here, and I bet made room for them here so you don't have some other error like to not enough space to deal with this.
04:34 There is only one error. If you get other errors than the one that's presented in this video, then there's something else.
04:42 Wrong not, but there's usually, there's only one thing wrong with each of these. As long as you fix it, it'll be fixed.
04:50 Alright. Now, this is your chance. If you want to go into this and you want to solve these nine problems get to capture these nine flags go for it, pause this video.
05:02 Almost. That was never am I ever saying stop the video, don't continue, but this is your spoil alert. This is your chance right now.
05:10 From this moment on, we are going to be talking about the hints. Not the solutions, but I will be giving you a hint as to where or how to solve this.
05:20 That doesn't mean I have the solution here for you. I have. We'll have the solution. We'll actually walk through this step by step the errors in another video.
05:28 So get prepared. That was it. That was your spoiler. You have plenty of time to stop the video. We are now going to get the hints.
05:38 So the hints are over here in the files hints. Again, at any moment, if you're like, Oh, I don't want to get.
05:43 The hint for the next ones, you can stop the video more than welcome to and continue. All right. Let's look at goal one, get one flag.
05:51 The idea is we only need this one thing, the the two to show the flag. And it says, if we hover over here, it says I is not the.
06:03 Find line five. So over on code, go to line five and somewhere here there is a problem. It should be I.
06:12 Something here should be I. I mean, it literally tells you that in the says I is not defined. Cool. That's your hint for hint one for two.
06:24 The error is L is not defined. Well, we don't have, we shouldn't have any L's, right? Line 17. So go over here to code line 17 and there might be an L that's not an I.
06:39 We need all eyes. We're using the for loop eyes. I equals zero here, I plus plus. So there's an L somewhere here, line 17.
06:49 And if you change a tool, an I should be fixed. We got another reference error here. T is not defined.
06:59 So somehow we have mistyped, probably mistyped something. Line 29, go over to code, line 29, and you'll find your answer.
07:09 You'll find the, oh, not, not your answer. You'll have to figure out how to fix it. But that's your hint for the third one.
07:16 Number four. So the error is, array result was not expanded because it would overwrite data in F 19. So look at that right there.
07:26 Our dean D 16, sorry. We should probably change that D 16, D 16 which means that there are more than four results.
07:36 So, so we're not just getting the flags that we need. Don't worry. This is. We have not a flag issue that there are more answers than we imagined because look at this.
07:50 We have for the flags. So that's not the issue. The error is in the code. And let's look at that again.
08:03 It might have to. Do with a malformed if statement whenever you see this sort of in a for loop, it's going to go through and it's going to be pushing some data to an array.
08:13 The if statement might be malformed here. So it might not be a problem with the for loop itself, but rather what's inside of it.
08:21 The if statement. Alright, let's go to five. Moving on to hint number five. Right. This is a little weird. This is reference does not exist.
08:34 What does that mean? Well, we might be able to Google that, but this is just quite an abstract error, but it should give you a clue that.
08:44 Variables might not be written correctly. So not just I L T. I'm going to give you the hint to specifically look at how the push function is written.
08:55 What is pushing into what? How is that array being created? The actual creations that are a there might be an issue there.
09:03 And if you. It's that push you'll solve this problem. Alright, number six. Number six. What happened to number six? There is no error.
09:16 See the hint. So there is no error. So the function is working perfectly as it's written. Which is problematic and does happen.
09:24 Times where the error is not necessarily that is a functional real error that the code is written wrong and the computer knows it.
09:31 The computer is running as if it is doing doing perfectly fine. One thing you can do when you're trying to solve these kinds of problems is use logger.log to see just what's going on here.
09:43 Like, what did I write? And what, what did it, what does it expect? Versus, what do I expect versus what is actually happening inside the code?
09:53 So I've already written for you inside the code some logs for this one. If you go to the six here, I've given you some logs here already.
10:03 Go up to run, get the, six flags, run this, you will have to authorize it. It's gonna, this will be in your will make a copy of this into your Google Drive and you'll have this in your Google Drive.
10:14 So it'll ask you to authorize. Go ahead and do that. And then you'll be able to see the logs and you'll see what is the problem.
10:20 There's, there probably is a problem with the data coming in. Into the for loop. Okay. That's your hint there. Gave you a little extra there with the logs, right?
10:35 Seven. What is going on with seven? Alright, we're now in the hard section. This is old. This is the hard section.
10:42 So if you're not able to figure these out, do not worry. They are hard and they happen all the time.
10:51 Alright, we need seven flags here. Return data is not defined. Line 82. Well, the hint there must be something along with this variable.
11:02 Like the return data. It's not defined.. In actuality, there's something completely missing. There's literally a whole line of code that is not in here.
11:11 You'll have to write that you'll have to define the variable return data before you use it. This happens a lot.
11:17 And I'll repeat this in the solution video, but basically the error says return data is not defined line 82, but the error is not on line 82.
11:25 The actual errors before that or the fact that something is missing. So sometimes our errors are not on the line that they tell us they are on.
11:33 They're somewhere before that. And that's something you'll have to understand that sometimes the error error is wrong where it is defining the line that it's on.
11:44 Okay. Number eight. So that's. That's your hint there. So I haven't solved it for you, but you'll have to figure out what's going on and solve it.
11:50 Number eight. Reference error, I is not defined line 92. So looks like nobody even defined what I is. And in the for loop, the I is, it could be any of.
12:07 Could literally could be any letter or any variable there, but pretty typically for for loops, we just use I we start with I, then we use J to K.
12:16 But in this one it looks like there, it hasn't been written at all. So you got to rewrite the entire for loop.
12:22 All three definitions. I have to write them there. Looks like it's just three eyes. All right. Now moving on to goal number nine.
12:34 You need nine flags. We have one. So this is hard because many times our code will run perfectly fine, especially when the code has nothing.
12:43 We know, okay, something's wrong. We did. Something wrong, but when the code has something here, we're like, we might not even know that there's a problem.
12:54 And so, yeah, there's no error. There is one flag, but there's not nine. So the error is the fact that the answer is different than what our expectation is.
13:04 I'll look at the variable. And imagine how creating a blank array at various points, various points might affect the final answer.
13:14 You'll have to move one line of code from somewhere to somewhere to solve this. So that's your hint there. The last one, it's difficult and I will admit that I make this mistake.
13:27 I have made this like twice in the workshop that I run spreadsheet automation when helping people with the for loop.
13:35 I've literally made this mistake twice and we were like, what is going on? This is a completely fine written for loop.
13:41 It's the for loop is fine. The if is fine. It was this. It was the problem. So I wanted to share this with you.
13:49 Make sure you conquered this and understood that sometimes when we get the answer, but it's not what we expect. We should probably dive in deeper and see what is going on.
13:59 So again, watch another video here. There is video for solutions if you want. I don't want to go through the solution.
14:07 Spoil them for you here, but I hope these hints have helped you solve these nine goals. Get these flags. Go and fix these errors.
14:16 Get the script get the script and the sheet down below. Copy it to your Google Drive and Mary hunting. Have a happy hunting.
14:27 Have a, Happy error hunting. Bye.