Friday, January 7, 2011

Sign Up Task

1. What I learned from watching http://www.diycomputerscience.com/courses/course/JAVASCRIPT/competency/185.


I think the very first thing I learned is that all Javascript books are bad. I have taught myself html, css, php, and MySQL from books. However, I've really struggled with learning Javascript from books. Maybe it wasn't me all this time...


You must be careful with the "+" operator. It can be used for addition, concatenating strings, or converting a string to a number value. This is something to be well aware of because it could be confusing in a loosely typed language, like Javascript.


Something else I thought is interesting is that once a string is formed it is immutable. So if you want to change something about a string you have to make a new string in place of the original string with the value you want.


It is also handy to know that the falsey values are boolean false, null, undefined, "", number 0, and NaN. Everything else is truthey.


Something that I learned, but don't totally understand is that Javascript is a unification of objects and hash tables. New objects can be added to the hash table. I don't really see how this is an advantage or why it was chosen to be done. Hopefully we can cover some of this!


Javascript is a real language, and is part of the C family. That makes me relieved that it shouldn't be too hard to learn. While you don't build executables out of it, the full program is delivered as source code.


I learned it is better to use === and !== for doing comparisons. This is because it does an equals on value and datatype. == and != do a coercion which makes programs less reliable and run slower.


You also have to be careful with division operation because of the floating point math. This can lead to problems when doing calculations on, say, money. So you have to be careful and do things like multiply by 100 cents and divide by 100 cents to make sure the math works out.


2. Sum Alert

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
<!--
var sum = 2+2;
function mathPop() {
window.alert( "2+2="+sum )
}
//-->
</script>
</head>
<body>
<form>
<input type="button" onClick="mathPop()" value="What is 2+2?">
</form>
</body>
</html>