That One Time I Realized My Code Was A Mess And How Arrays Saved Me

Last week, I was trying to figure out where all my cash was disappearing to. Between college, grabbing food, and hanging out, I was constantly broke. Since I am learning how to code, I figured I would build a simple script to track my daily expenses for the week.
Being a bit of a beginner, I started doing things the hard way. I created a separate variable for every single day. I had myMondayExpense, myTuesdayExpense, myWednesdayExpense, and so on.
By the time I got to Sunday, my code looked ridiculous. If I wanted to find out my total spending, I had to manually add all seven variables together in one massive, ugly line of code. If I wanted to print my expenses to the screen, I had to write seven different print statements. It was an absolute nightmare to read and even harder to manage.
Then I learned about arrays, and honestly, it felt like unlocking a cheat code.
What are these things, anyway?
Imagine you have a bunch of video games. You could leave the discs scattered all over your floor, making it impossible to find the one you want. Or, you could get a proper shelf where every game sits in a specific slot, right next to each other.
That shelf is an array.
An array is simply a collection of values stored together in a specific, numbered order, all existing under a single variable name. Instead of having seven different variables for my daily expenses, I just need one variable called myWeeklyExpenses that holds all of them in a neat row.
Creating your first Array
In JavaScript, which is what I have been writing lately, setting up an array is as simple as wrapping your data in square brackets [] and separating the items with commas.
// The old, messy way I started with
let myMondayExpense = 15;
let myTuesdayExpense = 22;
let myWednesdayExpense = 10;
// The array way (so much cleaner)
let myWeeklyExpenses = [15, 22, 10, 35, 12, 40, 5];
Just like that, one single variable name is now holding my entire week's financial damage.
The "Zero" Problem (Accessing Elements)
Here is the part that trips up literally everyone when they start coding: computers do not count like normal humans. We start counting at one. Computers start counting at zero.
Think of the array as a building. The first value isn't on the first floor; it is on the ground floor (index 0). If I want to see how much money I spent on Monday (the first day in my list), I have to ask the computer for index 0.
let myWeeklyExpenses = [15, 22, 10, 35, 12, 40, 5];
console.log(myWeeklyExpenses[0]); // This gives me 15
console.log(myWeeklyExpenses[3]); // This gives me 35 (the fourth item, Thursday)
Changing things up (Updating)
The best part about arrays is that they are flexible. Let's say I check my bank app and realize I forgot about the pizza I bought on Wednesday. Wednesday is the third day, which means it sits at index 2.
I do not need to rewrite the whole list to fix it. I just target that specific slot and overwrite the value.
let myWeeklyExpenses = [15, 22, 10, 35, 12, 40, 5];
// Changing Wednesday's expense from 10 to 25
myWeeklyExpenses[2] = 25;
console.log(myWeeklyExpenses);
// Now it outputs: [15, 22, 25, 35, 12, 40, 5]
How big is the shelf? (The Length Property)
Sometimes you lose track of how much data you actually put into your array, especially if you are building an app where users can add their own information. Instead of counting the items yourself, you can ask the array to tell you its size using the .length property.
let myWeeklyExpenses = [15, 22, 10, 35, 12, 40, 5];
console.log(myWeeklyExpenses.length); // This will output 7
This is incredibly useful because it tells you exactly how many "slots" are currently occupied. Notice that the length is 7, but the highest index is 6 (because we started at 0).
The Magic of Looping (Why Arrays Rule)
This is where the magic really happens. Remember how I complained about adding up all my variables manually? With an array and a simple loop, the computer does the heavy lifting.
If I want to find out my total spending for the week, I can set a myTotal variable to zero, and then loop through the array, adding each day's expense to the total.
let myWeeklyExpenses = [15, 22, 10, 35, 12, 40, 5];
let myTotal = 0;
for (let i = 0; i < myWeeklyExpenses.length; i++) {
myTotal = myTotal + myWeeklyExpenses[i];
console.log("Day " + (i + 1) + " expense: $" + myWeeklyExpenses[i]);
}
console.log("My total damage this week: $" + myTotal);
The loop just says: "Start at index 0, keep going until you hit the end of the array, and add the current value to my total." It would not matter if I was tracking 7 days or 365 days; the code stays exactly the same. That is the true power of an array.
Your Turn (The "Movie Marathon" Assignment)
If you have made it this far, you need to write some code to make sure it actually clicks in your brain. Fire up your code editor. It will take you exactly two minutes.
Create an array called
myFavoriteMoviesand fill it with 5 movies you could watch on repeat.Print your absolute favorite movie (the first one) and your least favorite of the top 5 (the last one) to the console.
Pretend you just watched a mind-blowing new movie. Target index 2 in your array and change that movie to the new one.
Write a simple
forloop to print out your newly updated movie list.





