With arrays, do you HAVE to refer to the last or second to last item with -1? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

With arrays, do you HAVE to refer to the last or second to last item with -1?

I tried to call for the false by putting in 4 instead of - 1 like in the example, but it suddenly didn't work... why? arr = [5, "Dave", 15.88, false] puts arr[0] #5 puts arr[1] #"Dave" puts arr[4] #false

9th Jun 2017, 11:28 PM
AJ Sellarole
AJ Sellarole - avatar
4 Answers
+ 14
The size of the array is 4 But the index starts at 0 So 0 is 5, ¹ is "Dave", 2 is 15.88 and 3 is false To get false, use arr[3] [Edit] fixed typo
9th Jun 2017, 11:31 PM
Jafca
Jafca - avatar
+ 13
@AJ Oops, it was a typo. Fixed now, thanks
9th Jun 2017, 11:32 PM
Jafca
Jafca - avatar
+ 2
Oh right I forgot, wait you mean the index starts at 0 right?
9th Jun 2017, 11:32 PM
AJ Sellarole
AJ Sellarole - avatar
+ 1
thank you
9th Jun 2017, 11:33 PM
AJ Sellarole
AJ Sellarole - avatar