Question

In: Computer Science

Activity 12: Array What is an Array? An array is a special variable, which can hold...

Activity 12: Array
What is an Array?
An array is a special variable, which can hold more than one value at a time.
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
var car1 = "Saab"; var car2 =
"Volvo";
var car3 = "BMW";
However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?
The solution is an array!
An array can hold many values under a single name, and you can access the values by referring to an index number.
Syntax: var array-name = [item1, item2, ...];    
Example:   var cars = ["Saab", "Volvo", "BMW"];

Arrays are allocated using the new keyword.

Array indexes start at 0 and extend to the size of the array minus 1. To assign a value to an element of an array open and close brackets are used.

The size of an array can be increased dynamically by assigning a value to an element pass the end of the array. Array can be created that initially has no elements at all. In addition, they are not of a fixed size but can grow dynamically.

The array, pictures, initially has no elements. After "Mona Lisa" has been assigned the array has 36 elements. The unassigned elements are set to Undefined.
The length property of arrays returns the number of elements in the array.





Tasks 1

Create an array of 5 animals. Use for loop to list values in the array.
1. Go through the lecture slides and understand arrays and multidimensional arrays.
2. Define a function max() that takes two numbers as arguments and returns the largest of them. Use the if-then-else construct available in Javascript.


Solutions

Expert Solution

For tasks 1, the array and the required function has been coded below, Some comment lines are also used in the code to state what the code is doing. In case of any query, you can mention it in the comment section. HAPPY LEARNING!!

For getting the output, you need to put the javascript code under html tags and save it with the extension .html and open it with any browser.

CODE:

      
<html>    
        <script>
                var animals = new Array("Cat","Dog","Human","Elephant","Horse");   // created the array of animals dynamically using new keyword
                var listed_animals = "";
                for (var i = 0; i < animals.length; i++) {
                   listed_animals += animals[i] + " ";      // accessing the values of array by for loop and concatenating in a single string separated by space
                }

                document.write("<b>Listed animals are: </b>");
                document.write(listed_animals);               // written the listed animals on the page


                function Max(x,y) {                   // function definition
                  if (x>y){                           // checking if x is greater than y
                        return x;
                        }
                  else if (x<y){                        // if x is less than y
                        return y;
                        }
                  else{                                  // else both are equal
                        return "Both are equal";
                        }
                }
                
                document.write("<p>The biggest in 12 and -34 is"+Max(12,-34)+"</p>");

        </script>
 </html>

OUTPUT(SCREENSHOT OF OUTPUT IN CHROME BROWSER):



Related Solutions

swift language declare a Swift array variable that can hold instances of any class type
swift language declare a Swift array variable that can hold instances of any class type
Explain how a full array in C can be expanded to hold more values. In your...
Explain how a full array in C can be expanded to hold more values. In your answer make sure you mention the function calls required, and anything else that is necessary to perform this task. Give a small example where an array of 100 integers is resized to be able to now store 200 integers.
Using the Python Program. Can an array hold a mixture of types i.e. int, float, string,...
Using the Python Program. Can an array hold a mixture of types i.e. int, float, string, array within the same array? Show a code segment to remove the last element from a 15 element array named myStuff[]. Please explain the code.
What are the various investments that a firm can hold and what are the benefits of...
What are the various investments that a firm can hold and what are the benefits of each one of them
In C Programing Create a stack using an array. Define the index variable of the array...
In C Programing Create a stack using an array. Define the index variable of the array name to be stacktop. Initially set stacktop to -1. Next create two functions push and pop. Both take as input 3 items: a pointer to the stack in memory, stacktop, and maxstack. Make sure to inc stacktop by one and also remember that stacktop[0] is the bottom of the stack and by stack rule cannot be accessed until all the other items are popped....
what is the concept/technique/tool for determining which of a company's activity (s) that can be outsourced?...
what is the concept/technique/tool for determining which of a company's activity (s) that can be outsourced? Provide details
Write a program in c++ to do the following: 2. Create an array to hold up...
Write a program in c++ to do the following: 2. Create an array to hold up to 20 integers. 3. Create a data file or download attached text file (twenty_numbers.txt) that contains UP TO 20 integers. 4. Request the input and output file names from the user. Open the files being sure to check the file state. 5. Request from the user HOW MANY numbers to read from the data file, up to twenty. Request the number until the user...
In C++ Write a program that dynamically allocates a built-in array large enough to hold a...
In C++ Write a program that dynamically allocates a built-in array large enough to hold a user-defined number of test scores. (Ask the user how many grades will be entered and use a dynamic array to store the numbers.) Once all the scores are entered, the array should be passed to a function that calculates the average score. The program should display the scores and average. Use pointer notation rather than array notation whenever possible. (Input Validation: Do not accept...
As the activity level increases, which of the following will decrease? A. Variable cost per unit...
As the activity level increases, which of the following will decrease? A. Variable cost per unit B. Fixed cost in total C. Fixed cost per unit D. Variable cost in total
What are ways that gender and age can be variables that potentially can hold someone back...
What are ways that gender and age can be variables that potentially can hold someone back or get in the way? What stereotypes did you grow up with and how did you find that potentially limiting? What are some ways to decrease any limitations self-imposed or learned through families/friends to set yourself free of any gender or age barriers?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT