 everyone let's go over question three from the compound data one workshop. So with these questions we are doing some operations on arrays so we're learning more about what happens when we make calls like data dot length and allocate the output to an integer called result what happens when we do data one data three and all of that. So for each of these questions we've got this array called data which has the values one seven two and nine so I've already done that memory diagram over there on the right so that will help us with each of our questions. So three point one result is being allocated the output of data dot length and it's tempting for questions like these to say that the answer is three because the last index we have here is three but when we're talking about lengths we're talking about the number of items there are and the number of items there are is four it's just because we're numbering them from zero to three that that can get a bit confusing so when I'm saying data dot lengths that's equal to four so whenever you want to access the last item in an array that index will always be data dot lengths minus one because we start counting from zero that last index will be one less than the length so this will be really helpful whenever you're trying to access the last item of an array that you don't know the length of um awesome so for three point one result will be equal to four what about this next one we have result is equal to data one plus data three again it's tempting here to say that we're getting the first item and the third item but in this case we're talking about the indexes so we're looking at least for this one here data one is referring to the item in data at index one so not the first item the item at index one and that there is seven so data one is seven plus data three which is nine so seven plus nine and you get 16 so you should have gotten 16 for 3.2 cool uh 3.3 you've got result is equal to data and its index is data dot length minus one and we already said that data dot length minus one is always the last index in an array so if we have a look here data dot length is three oh sorry data dot length is four minus one and you get three so we're accessing the item at data three and that value is nine so data is the same as data four minus one which is data three and data three is nine awesome when we get to question three point four we've got a loop as part of our answer and this is quite a generic loop that you'll see with a lot of compound data questions or a lot of array questions because what this loop is doing is it's going through every single item in an array where the loop variable is representing the current index so we know that when we have an array we always start our indexes at zero and that continues on until we get to data dot length minus one but here we've said i is less than data dot length so it will never equal data dot length which is exactly what we want it to do so let's use a loop logic table to answer this question just make this a little smaller so what do we need to keep track of we need to keep track of i we need to keep track of our condition which is i is less than data dot length data dot length is four so i'm just going to write data it's less than four and we want to know the value of result at the end so that's my table there sorry it's a bit small and we've got result is equal to zero initially and i is equal to zero our condition is is i less than four zero is less than four is true so what are we doing inside our loop result plus equals data i so we're adding data zero on to result data zero so that's the item at index zero which is one so we're plusing one here and then we get to the end of our loop so we increment i so i is equal to one now we check our condition is one less than four yes result was previously one and now we're doing result plus equals data i i is one so we're accessing the item at one in data index one so that item is seven so we're plusing seven and we get eight again we increment i is two less than four yes result is eight result plus equals data i so result plus equals data two what's data two it's value is two increment i again it's three less than four yes result was ten result plus equals data i so data three and data three is nine then we can't forget this step i is now equal to four we've updated i is four less than four no so when we get to the end of our loop result is equal to nineteen and what this loop is done is it's gone through every single item in our array and added the value of each item to result so what this code is doing is just adding up all the items in our array so that's something that you'll probably do in exams and maybe in assessments it's quite useful and then 3.5 we've got a different style question so i'll just zoom back in on my array here it's the same array we've got the same loop so we know looking at this loop that i is representing the index and so we are going through each item in our array and so for each item in our array we're taking that item and we're multiplying it by two so if we do that with i is equal to zero we've got data zero is equal to data zero times two so that's updating the value so data zero which is here is no longer one it's one times two which is two for data one which is the next one so data one is equal to data one times two data one we said was equal to seven so i can fill that in data one is equal to seven times two so we get 14 and again this loop goes through each item so we're performing that update on each item inside data and that's the array that you'll have in the end there and that's how you do all the questions from question three