1714660951

Series of exercises on manipulating arrays in JavaScript.


1. **Create an Array**: Create an array called `fruits` containing the names of different fruits. ```js let fruits = ["apple", "banana", "orange", "grape", "kiwi"]; ``` 2. **Accessing Array Elements**: Access and log the third element of the `fruits` array. ```js console.log(fruits[2]); // Output: orange ``` 3. **Adding Elements**: Add a new fruit, "pear", to the end of the `fruits` array. ```js fruits.push("pear"); console.log(fruits); // Output: ["apple", "banana", "orange", "grape", "kiwi", "pear"] ``` 4. **Removing Elements**: Remove the second fruit from the `fruits` array. ```js fruits.splice(1, 1); console.log(fruits); // Output: ["apple", "orange", "grape", "kiwi", "pear"] ``` 5. **Finding Index of an Element**: Find the index of "grape" in the `fruits` array. ```js let index = fruits.indexOf("grape"); console.log(index); // Output: 2 ``` 6. **Looping Through Array**: Loop through the `fruits` array and log each fruit to the console. ```js fruits.forEach(fruit => { console.log(fruit); }); ``` 7. **Sorting Array**: Sort the `fruits` array in alphabetical order. ```js fruits.sort(); console.log(fruits); // Output: ["apple", "grape", "kiwi", "orange", "pear"] ``` 8. **Reversing Array**: Reverse the order of elements in the `fruits` array. ```js fruits.reverse(); console.log(fruits); // Output: ["pear", "orange", "kiwi", "grape", "apple"] ``` 9. **Slicing Array**: Create a new array `citrus` by slicing the `fruits` array from index 1 to index 3. ```js let citrus = fruits.slice(1, 4); console.log(citrus); // Output: ["orange", "kiwi", "grape"] ``` 10. **Combining Arrays**: Create a new array `moreFruits` containing additional fruits and then concatenate it with the `fruits` array. ```js let moreFruits = ["pineapple", "mango"]; let allFruits = fruits.concat(moreFruits); console.log(allFruits); // Output: ["pear", "orange", "kiwi", "grape", "apple", "pineapple", "mango"] ``` You are free to add more operations for this exercise in the comments

(0) Comments

Welcome to Chat-to.dev, a space for both novice and experienced programmers to chat about programming and share code in their posts.

About | Privacy | Terms | Donate
[2024 © Chat-to.dev]