Array Methods Exercise

Create a new JavaScript file and put these two arrays at the beginning. You will write a single function that performs many operations on them.

var fruit = ["banana", "apple", "orange", "watermelon"];
var vegetables = ["carrot", "tomato", "pepper", "lettuce"];

After every command, use console.log() to inspect your arrays. A good way to do that is to do something like:

console.log("fruit: ", fruit);
console.log("vegetables: ", vegetables);
  1. Remove the last item from the vegetable array.
  2. Remove the first item from the fruit array.
  3. Find the index of "orange."
  4. Add that number to the end of the fruit array.
  5. Use the length property to find the length of the vegetable array.
  6. Add that number to the end of the vegetable array.
  7. Put the two arrays together into one array. Fruit first. Call the new Array "food".
  8. Remove 2 elements from your new array starting at index 4 with one method.
  9. Reverse your array.
  10. Turn the array into a string and return it.

If you've done everything correctly, the last step should print the following string to the console:

3,pepper,1,watermelon,orange,apple
A bigger picture

It's important to memorize what those arguments represent. Also take time to memorize what a method does and returns. Those three things will set you up to pass the Level 1 assessment. Look in the Level 1 curriculem for the study sheets for these methods. It will be helpful for this assignment!

If a method doesn't change something about the array, it just may share functionality to a string method! Unlike an array, strings are immutable. But they are still iterable, like an array.

Your friends list, ingredients on a cooking website, posts on a feed, and just about every list is an array.

Being able to remove, add, reverse, and otherwize work with these is a big deal. Practicing code challenges like this one is a good way to get good.

Reminders for if you get stuck

These methods return something
These methods often change something about the array
These methods take arguments this arguments are values