JSFiddle - React, Tailwind, and code Playground
by akang2
JavaScript
//Act 6.1 Looping over an array
var fruits = [ 'apple', 'banana', 'orange', 'pear', 'strawberry', 'pineapple' ];
//Right here I'm assigning the variable 'sorted' to the result of sorting the fruits array into alphabetical order. Why did I do this? To make it easier for me so I don't have to write 'fruits.sort()' all the time. I can instead write 'sorted'
var sorted = fruits.sort();
//Here I'm creating a 'for' loop that begins the incrememter (i) at 0, compares it to the length of the 'sorted' variable, and then increases it's value by 1 (i++)
for(i = 0; i < sorted.length; i++) {
//Here i'm assigning the variable 'fiveChar' to the comparison of whether or not sorted[i].length is greater than 5. If it is, then it returns 'true'; if not then it returns 'false'.
var fiveChar = sorted[i].length > 5;
//Here I'm creating an 'if' statement and checking to see if 'fiveChar' is true. Remember, if fiveChar is true, that means sorted[i].length is greater than 5. If it IS, then I have it display the value of sorted[i] to the console.
if (fiveChar){
console.log(sorted[i]);
}
}