Programming for Absolute Beginners by 2023

Programming for Absolute Beginners by 2023

Author:2023
Language: eng
Format: epub


Chapter 11 GroupinG Values toGether with objeCts and arrays

If you need to access the last element of the my_array array, you can do it with

my_array[my_array.length - 1]. The first element is always my_array[0]. Also, if you want to get rid of elements at the end of the array, you can also set the length property.

To get rid of all elements of the array my_array, just do my_array.length = 0;.

PRACTICE QUESTIONS

1. type out the entire largest_age function. test it by sending it different arrays and making sure it always returns the largest age.

2. instead of creating the array values yourself, have the user type the values.

For the first time around, have the user type in exactly three values. Make sure

these values get converted to numbers before storing them in the array!

3. extend your program so that the user can type in as many values as they want.

remember to include a way that the user can indicate that they are finished

either by asking them if they are done after each one or asking the user to type

a special sentinel value that indicates that they are done.

11.5 Mixing Objects and Arrays

While objects and arrays are pretty powerful in their own right, you can increase the power of both of them by mixing them together. The values held in object properties don’t have to be just numbers and strings—they can be any JavaScript value including other objects or arrays! Likewise, array values don’t have to just be numbers; they can be any value that JavaScript supports, including objects and other arrays. Mixing and matching objects and arrays gives you the flexibility to represent just about any real-world data set.

For instance, let’s say that I wanted to put more information about my children than just their age. I also wanted to include their name and favorite color. In addition, let’s say that I really want the list of children to be on a larger record about me, not just sitting by itself in a variable. How would I write that? Figure 11-4 shows how you would write it the long way.

154

Chapter 11 GroupinG Values toGether with objeCts and arrays

Figure 11-4. Building a Complex Object

As you can see, we start out with a blank object in the variable my_record. We then add a name to the object. Next, we add an empty array to the object, and we name the property children. Now, to access this array, we have to use my_record.children.

Since my_record.children is an array, we can use indexes on it. Therefore, my_record.

children[0] refers to the first value in the my_record.children array. Since that index doesn’t exist yet, it gets created. But what is stored there? It’s a new, blank object!

Therefore, my_record.children[0] now refers to an empty object. What can we do with objects? We can add properties. We can create a name property on this new, empty object by doing my_record.children[0].name = "Jim";. We then set the age in the same way.

At the end of this process, we have an object that has an array of objects.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.