Project Weekend Continues w/ Part 2 of Codecademy Javascript!
by James • May 20, 2012 • Project Weekend
Hello again, everyone! If you did not join us yesterday, then hop on over to Part One of Learning Javascript with Codecademy where we introduced you guys to a really cool web site that incentivises learning to program with cool badges and achievements like in an Xbox game that you can share with your friends across Facebook and Twitter and compare progress.
Pull up a new tab and follow along!
Editors and Arrays
The lessons started out with getting us acquainted with the editor window where we set a variable to correspond with the day of the week like this:
// declare and set the variable named "today"
// to today's weekday name
var today = ("Sunday");
This was rather simple, and with that, let’s move on! This unit is 6 or 8 parts I believe.
Here the plot thickens a bit as we use the array[field] to pluck a certain data field out of an array of data. This could be anything, like finding the median number in a data set. No problem; I used the hint, I’ll admit, but moving right along…
It asks me, at this point to just go ahead and make my own array with the numbers 1, 2 and 3. No problemo, we fill it in like so:
var numbers = [1, 2, 3];
For now this unit seems to be pretty easy, and I am still having fun. I’m a bit hungry and a lot decaffeinated, but attentive none the less.
Total Time Elapsed Today: 15 Minutes including setup.
This next part was a little bit tricky, as I tried to add my own comment to it and I got an error because of said comment. No problem, Moving right along, we set the array to be 3 days of the week. Monday, Tuesday, and Wednesday, like this:
What If?
This all seems pretty routine so far, very basic. This is what I think will be the big draw with people who are just getting started with coding and this will definitely engage an audience for an extended amount of time. I’m loving it so far. Let’s move on to the next exercise.
Here we are introduced to the triple-equals operator [===] which basically means, identical or equal to.
We are asked to change the first variable to 7 from 10 which confirms the ‘if’ and allows us to see the message.
In this next segment it gets a bit more complex and we test out the same exercise before, except with a string confirming whether or not someone’s name is Sam or not. Check out how we did! We passed! For all of you that are following along with us, give yourself a pat on the back and crack open an ice cold Dew to celebrate; we only have a few more exercises to go!
We are now learning how to prompt the user for input. We use the same example of the “Are You Sam?” exercise and learn to set the prompt response to a variable, in this case “name”
The next one asked us to look for the number “6″ while asking the user to guess which number the program is thinking of between 1 and 10. We are supposed to fix the “Else if” statement to make it look for 6. For some reason this took me a few tries, as I kept just adding “6″ inside of the () but what should have been in there was this:
var number = prompt("Guess what number I'm thinking of between 1 and 10!");
if(number === "7") {
console.log("You got it!");
// Change the following line.
} else if (number === "6") {
console.log("Close! Try guessing a little higher.");
} else {
console.log("You were way off! Sorry...");
}
Obviously you could see what the problem was. I was just trying to do this:
var number = prompt("Guess what number I'm thinking of between 1 and 10!");
if(number === "7") {
console.log("You got it!");
// Change the following line.
} else if ("6") {
console.log("Close! Try guessing a little higher.");
} else {
console.log("You were way off! Sorry...");
}
This next exercise is a bit of a departure from the other, in that there is not much code in the window, and once again we are just replacing one variable for another, like this:
I’m having a bit of trouble with this one..
// Assign a number to 'number'.
var number = ;
if( number === 5 ) {
console.log( "your number is 5" );
}
else if ( number > 5 ) {
console.log( "your number is greater than 5" );
}
else if ( number < 5 ) {
console.log( "your number is less than 5" );
}
I don't quite know what I am doing wrong in this exercise. I'm getting a bit hungry, and would like to stop after this unit, so I'll keep pushing, and go for the hint button, which hopefully can offer some insight.
Success!
Just as soon as I posted that I could not figure it out, I checked out the comment where it says to assign a number to the number variable, and this was the part that I was missing, so now the code looks like this:
// Assign a number to 'number'.
var number = 4 ;
if( number === 5 ) {
console.log( "your number is 5" );
}
else if ( number > 5 ) {
console.log( "your number is greater than 5" );
}
else if ( number < 5 ) {
console.log( "your number is less than 5" );
}
In this next part we look at formatting our code:
Wow that took no time at all! It was so simple as just correcting the parentheses and curly braces switcharoo!
Alright that was great guys! I'm feeling completely motivated now that we finished this unit together! Thank you for joining me this weekend! Next weekend there is no telling what we will get into together!
Always remember to Share, Like, and Subscribe, and if you have any questions or comments, you can leave them in the comments below! I always answer everyone, so do not be shy!
Ah who am I kidding, Let's FINISH THIS!!
Bringing It Up (And Down)
In this unit we will work with the ++, --, and for loops for incrementing and de-incrementing numbers stored as variables.
While You Wait
Here in this unit we learn about using While loops to increment the count of something in a loop.
Here is me working on the 2nd or 3rd part as I contemplate whether to have tacos or pizza for lunch.











