g Create an array of five animals. Use a for loop to display the values stored in that array. Add two more animals to the end of that array. Sort the array and display the sorted array on the screen.

Respuesta :

Answer:

The most common approach to accessing an array is to use a for loop:

var mammals = new Array("cat","dog","human","whale","seal");

var animalString = "";

for (var i = 0; i < mammals. length; i++) {

animalString += mammals[i] + " ";

}

alert(animalString);

Discussion

A for loop can be used to access every element of an array. The array begins at zero, and the array property length is used to set the loop end.

Though support for both indexOf and lastIndexOf has existed in browsers for some time, it’s only been formalized with the release of ECMAScript 5. Both methods take a search value, which is then compared to every element in the array. If the value is found, both return an index representing the array element. If the value is not found, –1 is returned. The indexOf method returns the first one found, the lastIndexOf returns the last one found:

var animals = new Array("dog","cat","seal","walrus","lion", "cat");

alert(animals.indexOf("cat")); // prints 1

alert(animals.lastIndexOf("cat")); // prints 5

Both methods can take a starting index, setting where the search is going to start:

var animals = new Array("dog","cat","seal","walrus","lion", "cat");

alert(animals.indexOf("cat",2)); // prints 5

alert(animals.lastIndexOf("cat",4)); /

Answer:

animals = ["Dog", "Lion", "Goat", "Zebra", "Cat"]

           for animal in animals:

                       print(animal)

x = animals.insert(5, "Lizard")

y = animals.insert(6, "Bat")

z = sorted(animals)

print(z)

Explanation:

The question can be solved using various back-end coding language like python, java, JavaScript etc. But I will be writing the code with python.

The first question said we should create an array or list of five animals.

animals = ["Dog", "Lion", "Goat", "Zebra", "Cat"]  → I created the five animals in a list and stored them in the variable animals.

for animal in animals:  I used a for loop to iterate through the list print(animal)  I used print statement to display the values stored in the array or list.

x = animals.insert(5, "Lizard")  I added an animal, lizard to the end of the array

y = animals.insert(6, "Bat")  I added another animal , Bat to the end of the array.

z = sorted(animals)  I sorted the array according to alphabetical number.

print(z) I displayed the sorted array .