Iterating Over an Array in Java and JavaScript


I have learned that while Java and JavaScript are different, they share similarities. Someone in the Code Newbie Slack informed me of the history of JavaScript and how the creators had borrowed some functionality from Java. That’s for another post on another blog.

But while I was reading my textbook chapter on arrays, I noticed that you can iterate through an array in Java much like you can in JavaScript.

Arrays in Java are initialized completely differently as you must specify a data type and the slots you allocate in the array cannot be changed, as such, it is immutable. Here is an example of an array in Java:

int[] myArray;
myArray = new int[10];

This declares the array then initializes it with 10 int spots.

An array in JavaScript looks like this:

var myArray = ["Jerri", 69, "Tiffany", 37, "John", 39];


How Interating Through Arrays in Java and JavaScript Are Similar


So, I first got a taste of iterating through an array in JavaScript while working the Bonfires at Free Code Camp, which use algorithms to help you better understand the ins and outs of the language. One of the ways you can iterate through an array in JavaScript is by a for loop and a .length method depending on what you need. Here is the syntax of an algorithm I wrote in order to find out if a word was a palindrome or not:

function palindrome(str) {
 str = str.toLowerCase().replace(/[W_]/g, "");
 for(var i = 0, len = str.length - 1; i < len/2; i++) {
 if(str[i] !== str[len-i]) {
 return false;
 return true;


Breaking It Down

The first part of this algorithm is turning all the letters in the string to lowercase and then using regex to get rid of capital letters, spaces, and punctuation[2].

The next part checks each index of the array to see if it is the same later, going inwards until it reached half the length of the array. If they are the same letter, then it’s a palindrome and will return true.

Same with Java…in a Way

The for loop in a Java array iteration looks the same when you just break it down into the method body. For instance:

public static void showArray(int|) array)
 for (int i = 0; i < array.length; i++)
 System.out.print(array(i] + " ");

Here, when the showValue method is called, an element of the array is passed to the method. It will loop through and print out each value of the elements of the array. Here we are printing it out. In the JavaScript example, we were cycling through the array to test for similarities.

The parallel? You can cycle through, and get elements/indeces of arrays with a for loop.

  1. You need to have the same data type in a Java array. If you look at the JavaScript array, I have number data types and strings. In the Java example I have 10 ints, which are numbers up to 231–1. Can’t mix data types in Java arrays.  ↩
  2. I used regex because there was a test that had to be run that included numbers and odd punctuation that had to return false.  ↩

Moving on Up While Feeling Stupid



So I actually did go to bed and get up at a great hour, but I screwed off with the new Music update for a good hour. It cut into my coding time but I finally settled in around 1 PM, a full four hours after I woke up and one after I scheduled to code.

So I continued to tackle while loops. The idea of scope was covered. But writing my own for, do, and while loops I kept forgetting to declare a condition in a variable. Then I also forgot about global and local scope. So my loop looked something like this:

var count = 1;
for (var i = 0; i &lt; 10; i++) {
 while(count &lt; 3) {
  console.log(&quot;What the fuck am I even doing&quot;);

I was so frustrated with the fact I felt like I was flying blind that I had a total give up console.log in the loop (pardon the expletive).

I didn\’t understand why this executed. So I turned to the Code Newbie Slack team and after a long while with me looking like an idjit, I came to realize the loop I wrote was awful. Awful because it didn\’t take into consideration the scope, local and global.

So the first line, my condition, is in global scope. This screws up everything inside the while loop. Once count increments from 1-3, it stays at 3 and doesn\’t reset because I didn\’t explicitly set the condition locally. Also, the forloop loops around 10 times and fails 9 because I\’ve declared countoutside the forloop. So for count which starts at 1 and increments by 1 to 3 until the loop fails, which it fails at 9 times, according to the for loop. The console.log prints out the string according to the count twice and then runs 9 more times and fails. It gets stuck at 3 and never resets…still thinking about why there.

Here is what the loop should look like, to include local scope:

for (var i = 0; i &lt; 10; i++) { var count = 1; while(count &lt; 3) { console.log(&quot;What the fuck am I even doing&quot;); count++ } }

Feeling stupid much?
(Thinking about this while Rihanna sings \”Bitch better have my money\” on Beats1, Ha. Maybe I should turn it down…)

So next post will be about Arrays and Objects because yes, I am there finally!!! I have an idea for my languishing account. Arrays and objects change everything.

Yeoman and JavaScript, etc





(Currently laying in bed, terrible sinus headache today, writing this on my iPad in Editorial. If it\’s ridden with typos I\’ll fix them once I get up and head to my Mac).

So, my Code Newbie pal and former guest on the podcast Mark Pearyer (pronounced Pierre) and I, as well as a few other Code Newbies are working on a hangman app. Mark presented this nice looking boilerplate for the app. He said that he made the boilerplate with Yeoman generator. If you don\’t know what a boilerplate is check out this article.

I sat down and watched the Yeoman intro Yeoman intro video and immediately became excited about the possibilities for my own apps and websites.

Downloaded yo, bower, set up a test app, and run the grunt server. I\’m hooked. I plan on using Yeoman to build two apps I have an idea for.

Fun with For Loops and Arrays

I love loops. I can see how effective they would be when repeating tasks and functions. The most logical part of JavaScript for me is looping. Not sure why. In Free Code Camp\’s Codecademy track they\’ve grouped arrays and for loops together. It\’s strange when I think about JSON and JavaScript arrays and how they differ, where in JSON you can\’t mix data types (for example, integers and strings aren\’t allowed in the same array in JSON but can be in the same array in JavaScript). Anyway we built simple for loops with arrays like this:

var names = [&quot;Tiffany&quot;, &quot;John&quot;, &quot;Joe&quot;, &quot;Rick&quot;, &quot;Jerri&quot;];
for (var i = 0; i &lt; names.length; i++) {
console.log(&quot;I know someone called &quot; + names[i]);

The variable i refers back to the names in this case. Since we have an array and counting in the array begins at 0, the second part of the for loop is going to calculate the number of names in the names variable, or the length of the array. Then in the last part of the for loop, the loop is coming back around and counting up to the next name. It will do this because the ++ means to increase by 1. It will do this for the length of the array, and for as long as i is less than the length of the array. The console.log prints out the string and concatenates the string plus the names in the array.


So I\’m sharing, again, what I\’m listening to on Spotify. On the Code Newbie Discourse we talked about music to code by and I found some awesome wub wub dubstep to kill code by. I\’ll share my favorite here (NSFW):