My First “Program” Attempt

I finished Codecademy’s JavaScript track a couple of weeks ago, but haven’t yet had the chance to make a post, so here it goes. I’ve been busy and haven’t really done much programming since (aside from a little more dabbling with some additional JavaScript lessons on Codecademy’s site that aren’t part of the track). Since I was falling a bit off the programming wagon, I asked my husband what I should do next. He challenged me to make a dog management program in which I could program in dogs, add dogs, remove dogs, and search for dogs. Hmmm…well, seemed simple enough, but it had been a couple of weeks since I had really been doing much programming, so I was a bit nervous. However, never one to pass on a challenge, I figured I’d give it a go. Plus, I’m pretty sure Codecademy had prepared me enough for a simple program like this, so the next day I attempted my program.

Codecademy has a section where you can use their website to write programs (they have one for Python, Ruby, and JavaScript). I selected JavaScript and started programming away. It was pretty smooth sailing for the most part. I first created a Dog object, which included the properties name, breed, and age), so that I could easily create some sample dogs for my program. After making the object, I created a spikey variable and an echo variable (my two dog entries). I then created a var contacts array which included spikey and echo (obviously…). Since I wanted to be able to print out the names of my dogs, I then added a printDog function that would print to the console the dogs’ names. Next, I created a list function, so that I could quickly print out a list of my “dog contacts.”

Because my husband wanted me to be able to search for dogs, I then added a search function so that I could easily see if a particular dog was in my contacts. The next part of the program was adding an add function so that I could add dogs and then finishing off with a remove function so I could delete animals from my contacts (what can I say? Spikey and I have a tumultuous relationship… 😉 Just kidding.).  The hardest part of the whole program was simply figuring out how to remove a contact, since Codecademy hadn’t gone over this in the tracks.  Because I wasn’t entirely sure how to do this, I did what everyone does – I googled it. Once I had googled it, I quickly realized that I would have to use array.splice, which ended up being simple enough.

Now came the real test: seeing if I had passed the challenge presented to me by my husband. Sure, I thought I had completed the challenge correctly, but would the program actually work?  Well, I decided to either sink or swim and called the hubby over to test it out. Sure enough, it worked! 🙂 Yay. Rock on for a beginning programmer. In retrospect, I know this is a truly simple program, but having never programmed before in my life and only completing 2 weeks of recreational programming (about 1/2 an hour to an hour a day), I felt very proud of myself.  So even though you may feel like banging your head against the wall, don’t…just keep on programming little grasshopper.

You can find my code below. Simple, I know, but good enough for me. 🙂 Now my husband wants me to make modifications to make it more complex….gulp.

Happy Programming!

 

function Dog(name, breed, age) { 
    this.name = name; 
    this.breed = breed; 
    this.age = age; 
    }

var spikey = new Dog("Spikey", "chihuahua", 7);
var echo = new Dog("Echo", "chihuahua", 2);
var contacts = [spikey, echo];

function printDog(dog) { 
    console.log(dog.name + " "); 
}

function list() { 
var contactsLength = contacts.length; 
    for (var i = 0; i < contactsLength; i++) { 
        printDog(contacts[i]);
} 
 }

var search = function(name) {
    var contactsLength = contacts.length;
    for (var i = 0; i < contactsLength; i++) {
        if(name === contacts[i].name) {
            printDog(contacts[i]);
        }
        }
    };

 function add (name, breed, age) {
     contacts[contacts.length] = {
         name: name,
         breed: breed,
         age: age
     };
 }

function remove (name) {
    for (var i =0; i < contacts.length; i++)
   if (contacts[i].name === name) {
      console.log("Is this working?");
      contacts.splice(i,1);
   }
}

indapp

 

Leave a Reply

Your email address will not be published. Required fields are marked *