Reflections
JavaScript and Ruby each have their ways of iterating through loops. Ruby has a couple methods of looping.
num_array = [1,2,3,4,5] for number in num_array puts number if number > 1 end => 2 3 4 5 num_array.each do |num| puts num if num > 1 end => 2 3 4 5
Here, the first method used to iterate through the elements in the array is with the "for number in num_array." I define number as each element and it does some code for each element in the array until the end of the array. It puts number if the number is greater than 1. The second method used here is using the .each method. It executes the block of code after the "each" for each element in the array. This produces the same result. The .each method is definitely more widely used in Ruby for looping.
JavaScript is done a little bit differently.
var num_array = [1,2,3,4,5] for (var i=0; i < num_array.length; i++){ if (num_array[i] > 1){ console.log(num_array[i]) } } => 2 3 4 5
Here in JavaScript, I used the for construct. JavaScript also require var before defining the variable. The for syntax is "for (var i = 0; condition; increment){block of code to execute during each loop.} I made i=0 and incremented "i" by 1 after each loop and it would keep on looping only "i" was less than the number of elements in the array where "i" is the index. The block that was run on each iteration was to print out to the console the element if the element was greater than 1.
Ruby seems to have a couple more methods built in for looping running blocks within arrays with access to index and the elements. The above-mentioned methods are the most basic way to iterate though the array with JavaScript and Ruby. Ruby simplifies the syntax while JavaScript requires more brackets and semicolons.