Day 7: More JavaScript

“The temptation to quit will be greatest just before you are about to succeed.” — Chinese Proverb

Another day of JavaScript, but this time it was not drawings and animations. This time it was ‘For’ loops. I have been bouncing back and forth between two online training programs, Khan Academy and Codecademy. The Khan Academy lesson plan on JavaScript has been focused on learning the basics with shapes to lead to animations. The Codecademy lesson plan was a bit different. Apparently, I had started the original lesson two years ago and never finished so the ‘resume’ feature dropped me in the center of ‘For’ loops. I could barely remember how to create a function or pass a parameter for it to return a value. I was lost and preceded to divert my focus out of frustration.

“The person who removes a mountain begins by carrying away small stones.” — Chinese Proverb

Seeing that I needed to refresh my memory. I went back to the beginning of the lesson plan to re-learn the basics. Below are some of the introduction elements…

Writing Comments: Use ‘//’ to start comments when in JavaScript. Comments allow for other users and yourself to understand and maintain your code.

JavaScript Benefits: JS is used to make websites respond to user interaction, build apps, access information on the internet, organize data and much more.

Confirm Message: Use ‘confirm(“<add string of text here>”);’ to generate a prompt to select Cancel/ OK.

Printing Out: Use ‘console.log(“<add data here>”);’ to print out information from the console.

Substrings: Use “<add text here>”.substring (<starting point>, <ending point>) to display portions of text.

Types of Comparisons:

~ ‘===’ -> This means Equal To (e.g. common mistake is to use ‘=’, but this means to assign or get rather than to compare two elements. Also ‘==’ doesn’t compare the data type of two elements which may cause some unexpected results in your programs.)

~ ‘!==’ -> This means Not Equal To

Variables: Think of it like a placeholder for some value (e.g. ‘var myName = Johnny Quest;’). ‘myName’ is the variable and ‘Johnny Quest’ is the value that the program gets when it sees ‘myName’ variable.

All of this eventually led me to the ‘Functions’ tutorial, which brough the pieces together.

Functions within JavaScript (Examples)

Using variables and functions allows you to follow the D.R.Y (e.g. Don’t Repeat Yourself) principle. I must admit I struggled with the below example for at least 20minutes before I went back over my notes and found the error…

Ex. (Food Demand)

var foodDemand= function (food){

| console.log(“I want to eat” + “ “ + food);

}

foodDemand(tacos);

The function is supposed to return the string “I want to each tacos”. However, I missed an element. I forgot to place “ ” around the tacos and kept getting the error variable tacos undefined. After relaxing a bit, I realized how silly the mistake was that I needed tacos to be identified as a string and fixed the error. It is the simple things. Later on the lesson plan covered Global variables(e.g. variables that are defined outside of a function and can be used anywhere) and Local Variables (e.g. variables that are defined inside of the function and can only be used within the function). Ultimately I ended the lesson with a function to discuss sleep. That being said, having a healthy sleep habit is a great start to learning new skills.

Ex. SleepCheck

(1)var sleepCheck = function (numHours) {

(2)| if (numHours) >=8) {

(3)|| return “You’re getting plenty of sleep!”;

(4)| }

(5)|else {

(6)||return “You could use some rest soon!”;

(7)| }

(8)}

(9)sleepCheck (10)

The breakdown is as follows for the function. Line 9 calls the function ‘sleepCheck’ and passes the parameter of ‘10’. Line 1 is where the variable ‘sleepCheck’ is declared as a function. The variable ‘numHours’ is a placeholder that is passed ‘10’ when the function is called. Line 2 is an if statement to compare if the value passed, e.g. 10, is greater than 8. If it is the program returns the string “You’re getting plenty of sleep!”, e.g. line 3, and stops. If not, the program returns the string “You could use some rest soon!”, e.g. line 6, and stops. The curly brackets ‘{}’ are used to contain the repeatable code for the function. Each line uses ‘;’ to suggest the end of the line of code. Think of it like a period for sentences.

Well, with all that complete it is time to move to this crazy thing called ‘For’ Loops.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.