Today's first course is called Introducing Conditional Statements. When creating conditional statements, do not put a semicolon at the end, after the closing curly brace. That's just how it works. When entering text that should appear on screen into Javascript code, do so like this:
var answer = prompt("What programming language is the name of a gem?");
if ( answer === "Ruby") {
document.write("<p>That's right!</p>")
} else {
document.write("<p>Sorry, that was incorrect, try again!</p>")
}
Notice how the html tags are inside of the quote marks. If they are outside of the quote marks, javascript will register an error and the text will not appear on screen. Alternatively, you can leave out the html tags and simply leave the text, with quotation marks on the side, which will work, but then none of your html or css code will be applied to it. When inside an If/Else statement if the condition (the code inside the parentheses) needs an = sign, use ===, not =. That's just the way it works inside the condition, even though outside of the condition, = is correct.
All conditional statements, no matter how complex, end up with a yes or no answer, or, as computers like to call it, true or false. Here's a list of comparison operators in Javascript:
> Greater than
>= Greater than or equal
< Less than
<= Less than or Equal to
== Equal to
=== Strict Equal to
!= Not Equal to
!== Strict Not Equal to
Here's a few examples and clarifications:
(100 >= 100) is true, because 100 is equal to 100, and >= means greater than OR equal to.
("apple" < "bear") is true, because the first letter of the word apple, a, comes before the first letter of the word bear, b. So, letters that come after other letters are greater than the letters before them. Note that letter strings will always be greater than numbers, so if you compare a number and a letter, the letter is greater than the number.
("3" == 3) is actually true, because the == converts the "3" string to a number, making the two values equal.
("3" === 3) is false, because === will not convert the "3" to a number.
("" == 0) is true, that is, an empty string is == to 0.
("" === 0) is not true, that is, an empty string is not === to 0.
(165 === 165.9) is false, because the two numbers are not strictly equal.
("javascript" === "JavaScript") is false, because JavaScript is case sensitive.
!=, the ! point is called a not operator. It is better to use the !==, which is the strict not equal operator.
I wrote this guessing game for class today:
var userNumber = prompt("Let's see how good of a guesser you are! Guess a whole number from 1 to 6!");
var computerNumber = Math.floor(Math.random() * 6) + 1;
userNumber = parseInt(userNumber);
console.log(computerNumber);
if (userNumber === computerNumber) {
document.write("<p>You got it, " + userNumber + " is correct!</p>");
}
else {
document.write("<p>Sorry, the number was " + computerNumber + "!</p>");
}
It's neat, the user selects an integer from 1 to 6, the computer does the same, and then we find out if the user's selection and the computer's selection matched. Again, conditions evaluate to either true or false. We went over several values in Javascript, which were strings, numbers, and Booleans (true and false). It's self-explanatory, but true evaluates to true, and false evaluates to false.
I started reading a book titled "Eloquent Javascript," by Marijn Haverbeke, it's available for free online, which is amazing. The author says that:
"The art of programming is the skill of controlling complexity. The great program is subdued -- made simple in its complexity."
I liked that quote. I learned that ECMAScript and JavaScript can be used interchangeably, as ECMAScript is a standard document written to describe the way the JavaScript language should work to make sure everyone using JavaScript is on the same page. This standardization is referred to as the ECMAScript standard. I learned that in addition to web browsers, some databases, such as MongoDB and CouchDB, use JavaScript as their scripting and query language. Several platforms for desktop and server programming, such as the Node.js project are providing an environment for programming JavaScript outside of the browser. The book covers Node.js on chapter 20, and I'm really looking forward to that. I like the way the author writes.
I learned about bits, which are composed of 0's and 1's. Here's the number 13 in binary:
00001101
Neat, huh? This is what it actually means:
0 0 0 0 1 1 0 1
128 64 32 16 8 4 2 1
So that means we need one 1, one 4, and one 8, to get 1 + 4 + 8 = 13. Sweet! In order to organize bits in a Javascript environment, we use values. All values are made up of bits, but they play different roles. There are six basic types of values in JavaScript: numbers, strings, Booleans, objects, functions, and undefined values. The book went over math and numbers in Javascript.
2.998e8 is the same as 2.998 x 10(to the 8th power) = 299,800,000.
When creating an if statement, you must begin with if () {}, and there does not need to be an else statement, but if there is, you must end with it, and any else if () {} statements must be after the if () {}statement and before the else {} statement, if any. As soon as one condition is true, any other else {} or else if () {} statements do not run.
I went to another blog and reviewed the order of operations (PE MD AS), and the blog noted that multiplication and division and division are treated as "from left to right," and so are addition and subtraction, in other words:
4 - 2 + 1 = 3
The above does not equal 1, as you would think if you did the addition before subtraction and started on the left instead of the right. Another way to look at it is to separate the numbers into their individual values and then combine them:
(+4),(-2),(+1) = (3)
Either way, just remember it's left to right, not right to left, and PE MD AS is a useful tool.
var answer = prompt("What programming language is the name of a gem?");
if ( answer === "Ruby") {
document.write("<p>That's right!</p>")
} else {
document.write("<p>Sorry, that was incorrect, try again!</p>")
}
Notice how the html tags are inside of the quote marks. If they are outside of the quote marks, javascript will register an error and the text will not appear on screen. Alternatively, you can leave out the html tags and simply leave the text, with quotation marks on the side, which will work, but then none of your html or css code will be applied to it. When inside an If/Else statement if the condition (the code inside the parentheses) needs an = sign, use ===, not =. That's just the way it works inside the condition, even though outside of the condition, = is correct.
All conditional statements, no matter how complex, end up with a yes or no answer, or, as computers like to call it, true or false. Here's a list of comparison operators in Javascript:
> Greater than
>= Greater than or equal
< Less than
<= Less than or Equal to
== Equal to
=== Strict Equal to
!= Not Equal to
!== Strict Not Equal to
Here's a few examples and clarifications:
(100 >= 100) is true, because 100 is equal to 100, and >= means greater than OR equal to.
("apple" < "bear") is true, because the first letter of the word apple, a, comes before the first letter of the word bear, b. So, letters that come after other letters are greater than the letters before them. Note that letter strings will always be greater than numbers, so if you compare a number and a letter, the letter is greater than the number.
("3" == 3) is actually true, because the == converts the "3" string to a number, making the two values equal.
("3" === 3) is false, because === will not convert the "3" to a number.
("" == 0) is true, that is, an empty string is == to 0.
("" === 0) is not true, that is, an empty string is not === to 0.
(165 === 165.9) is false, because the two numbers are not strictly equal.
("javascript" === "JavaScript") is false, because JavaScript is case sensitive.
!=, the ! point is called a not operator. It is better to use the !==, which is the strict not equal operator.
I wrote this guessing game for class today:
var userNumber = prompt("Let's see how good of a guesser you are! Guess a whole number from 1 to 6!");
var computerNumber = Math.floor(Math.random() * 6) + 1;
userNumber = parseInt(userNumber);
console.log(computerNumber);
if (userNumber === computerNumber) {
document.write("<p>You got it, " + userNumber + " is correct!</p>");
}
else {
document.write("<p>Sorry, the number was " + computerNumber + "!</p>");
}
It's neat, the user selects an integer from 1 to 6, the computer does the same, and then we find out if the user's selection and the computer's selection matched. Again, conditions evaluate to either true or false. We went over several values in Javascript, which were strings, numbers, and Booleans (true and false). It's self-explanatory, but true evaluates to true, and false evaluates to false.
I started reading a book titled "Eloquent Javascript," by Marijn Haverbeke, it's available for free online, which is amazing. The author says that:
"The art of programming is the skill of controlling complexity. The great program is subdued -- made simple in its complexity."
I liked that quote. I learned that ECMAScript and JavaScript can be used interchangeably, as ECMAScript is a standard document written to describe the way the JavaScript language should work to make sure everyone using JavaScript is on the same page. This standardization is referred to as the ECMAScript standard. I learned that in addition to web browsers, some databases, such as MongoDB and CouchDB, use JavaScript as their scripting and query language. Several platforms for desktop and server programming, such as the Node.js project are providing an environment for programming JavaScript outside of the browser. The book covers Node.js on chapter 20, and I'm really looking forward to that. I like the way the author writes.
I learned about bits, which are composed of 0's and 1's. Here's the number 13 in binary:
00001101
Neat, huh? This is what it actually means:
0 0 0 0 1 1 0 1
128 64 32 16 8 4 2 1
So that means we need one 1, one 4, and one 8, to get 1 + 4 + 8 = 13. Sweet! In order to organize bits in a Javascript environment, we use values. All values are made up of bits, but they play different roles. There are six basic types of values in JavaScript: numbers, strings, Booleans, objects, functions, and undefined values. The book went over math and numbers in Javascript.
2.998e8 is the same as 2.998 x 10(to the 8th power) = 299,800,000.
When creating an if statement, you must begin with if () {}, and there does not need to be an else statement, but if there is, you must end with it, and any else if () {} statements must be after the if () {}statement and before the else {} statement, if any. As soon as one condition is true, any other else {} or else if () {} statements do not run.
I went to another blog and reviewed the order of operations (PE MD AS), and the blog noted that multiplication and division and division are treated as "from left to right," and so are addition and subtraction, in other words:
4 - 2 + 1 = 3
The above does not equal 1, as you would think if you did the addition before subtraction and started on the left instead of the right. Another way to look at it is to separate the numbers into their individual values and then combine them:
(+4),(-2),(+1) = (3)
Either way, just remember it's left to right, not right to left, and PE MD AS is a useful tool.
SUMMARY OF CODING SKILLS
Total Treehouse Points: 4,867
Treehouse Points by Subject Matter (Miscellaneous not included):
HTML: 663
CSS: 1,599
Design: 1,193
Development Tools: 747
Javascript: 632
Treehouse Ranking (%): "You have more total points than 90% of all students."
Treehouse Badge(s) Earned Today:
Treehouse Courses Completed:
How to Make a Website
HTML
CSS Foundations
CSS Layout Techniques
Aesthetic Foundations
Design Foundations
Adobe Photoshop Foundations
Adobe Illustrator Foundations (66% complete, switched focus from web design to web dev)
Git Basics
Introduction to Programming
Codecademy (& other) Courses Completed:
HTML and CSS (Codecademy)
Design Foundations
Adobe Photoshop Foundations
Adobe Illustrator Foundations (66% complete, switched focus from web design to web dev)
Git Basics
Introduction to Programming
Codecademy (& other) Courses Completed:
HTML and CSS (Codecademy)
Books Read or in Progress:
Completed: "Head First HTML and CSS," by E. Robson & E. Freeman (37 pg preface and 710 pgs of actual content (as in, I'm not including the book's index)).
In Progress: "Eloquent JavaScript," by Marijn Haverbeke.
My Progress on The Odin Project:
1. Introduction to Web Development 100% Complete
2. Web Development 101 29% Complete
3. Ruby Programming 0% Complete
4. Ruby on Rails 0% Complete
5. HTML5 and CSS3 0% Complete
6. Javascript and JQuery 0% Complete
7. Getting Hired as a Web Developer 0% Complete
Hours Spent Coding Today: 4.5
Total Hours Coding: 347.5
No comments:
Post a Comment