JSFiddle - React, Tailwind, and code Playground
by akang2
JavaScript
//Act 5.1 Array Operations
var fruits = ['apple', 'orange', 'pear', 'kiwi', 'banana'];
console.log(fruits);
//The sort() method takes the array and alphabetizes it!
//Right here, I'm assigning the variable "sorted" to the result of fruits.sort(), which is basically saying "Using the method of 'sort()', alphabetize the variables inside 'fruits'.
var sorted = fruits.sort();
//Here I'm logging the result of 'sorted' to the console so I can see if it's correct
console.log(sorted);
//You'll notice that the first value in the array is still 'apple', but the second value is now 'banana' and not 'orange'. This makes sense since the letter 'b' comes before the letter 'o' in the alphabet. Duh.
//The reverse() method takes the array and reverses the values inside it! For example, if we have an array with the values "blue", 13, "lolcat", 7, 1, reversing that would give us 1, 7, "lolcat", 13, "blue"
//Here I'm assigning the variable "reversed" to the result of fruits.reverse(), which is basically saying "Using the method of 'reverse()', reverse the order of the variables inside 'fruits'.
var reversed = fruits.reverse();
//Here I'm logging the result of 'reversed' to the console so I can see if it's correct
console.log(reversed);
//Now it should be easy to answer the questions from 5.2. At least I hope :(