2012 m. vasario 6 d., pirmadienis

Intro to JavaScript, Task 1.

I have joined the Intro to JavaScript group on P2PU. The first task is to answer these questions:
  1. When parsing a String into a number using the parseInt() method, we are advised to always provide the radix. Why is this?
  2. Why do we lose precision when performing operations with decimal numbers in Javascript? Can you think of a few implications of why this would be a problem?
  3. Do you understand why the next operation produces the given result 115 * 4 - 4 + 88 / 2 = 500?
  4. What is a type, and why do you think types are useful in writing programs?
  5. What does typeof(4.5); do, and why does typeof(typeof(4.5)); return "string" ?
  6. "+" in Javascript is overloaded, what does this mean?
It took me about an hour to research everything. I hope the answers are correct :)
  1. First of all, since JavaScript is loosely typed it does not know what kind of number your string is holding. The parseInt() function will try to guess what kind of number you are giving it, however if your string contains a number like "09" or "0x99" bad things will happen. As explained in W3C Schools Javascript will interpret the leading "0" as a number written in the Octal notation, and the leading "0x" as Hexadecimal. For example trying to use parseInt("0x99") will give you 153. Therefore, if you know that you are going to be parsing only decimal numbers adding a radix of 10 is advised. var decimalNumber = parseInt("099", 10); will give you the correct 99. Tested this myself :)
  2. Currently I am unable to answer this question properly. I can only tell that internally all numbers in JavaScript are represented as floating-point values. Calculations that come close to 1 give odd results e.g.:  0.333 + 0.333 + 0.333 = 0.9990000000000001 . The main problem I can think of is calculations involving money. Money is always a sensitive topic, thus inaccurate calculations would a very undesirable thing. However, I think that this could be alleviated doing the calculations in cents. This would remove the floating points. As I said, currently I do not fully comprehend why JavaScript makes such mistakes with decimal calculations, but I will certainly be aware of it.
  3. We get 500 because of the mathematical order of operations. The first operation to be executed is the multiplication, then division, then addition and the last subtraction. The calculation is executed in the following order: 115*4  then 88/2 then -4+44 then 460 + 40. This standard is used in science, technology, mathematics and computer sciences. Wiki - Order_of_operations
  4. I do not fully understand this question... Is this about data types? If yes then - data type is a limitation/description of the possible contents of a variable. For example in JavaScript when declaring a variable one does not have to explicitly say what type of data can be held in that variable. e.g. var myVariable = 5; This is called loose typing. I could easily later say myVarialbe = "It is raining."; Now myVariable contains a string "It is raining.". Other languages like C# and Java are strongly typed. Meaning that when you declare a variable you have to specify what data type will it hold. This data type can not be changed later. There is a great debate what is better strongly typed or loosely typed. At the moment I do not have my own opinion.
  5. This question is directly linked to previous question about data types. Since in JavaScript variables can change their data types it is often necessary to check what data type is currently held in a particular variable. Function typeof() simply returns the data type of a variable. Since typeof() returns the textual representation of a data type contained inside a variable, performing the double typeof(typeof()) will always return a string.
  6. The + operator in JavaScript has two meanings depending on the context it is being used in. Therefore, it is called an overloaded operator. For example, in arithmetic operations + is used for addition. 2 + 2 = 4. However, for strings the same operator is used for concatenation "2" + 2 = 22. Note that  2 - 2 = 0 and "2" - 2 = 0, since operator - is not an overloaded operator (it can have only one meaning).
Aweome questions, I hope the answers are OK as well... Had a blast researching all this stuff :)

Text Editors

Choosing the best text editor was not an easy task. Since I am running Windows, there are plenty of different text editors. I have actually tried using many editors and IDE's e.g. Eclipse PDT, Front Page, Dreamweaver, but I constantly find myself returning to Notepad++. I really don't know why, but this editor just feels so fast and responsive, it just feels right. Also it has one feature that I really like - Replace In All Open Documents!
Notepad++ FTW!

Hello World!

I just couldn't start a programming blog without "Hello World!". As you may, or may not know I have just registered to P2PU and accepted the Introduction challenge!
To be honest, I have always wanted to start a blog, but never got past 3 posts and thus never published anything. This time everything is slightly different, I hope.  As I have only 30min to complete the introduction task, my first post won't be very long.
So for the introduction... I am a junior web developer, with almost a year of PHP, HTML/CSS experience. PHP is my primary language, for the last several months I have been using it on a daily basis. I am joining P2PU in order to become better at web development, and hopefully learn a handful of new things. I am very curious about mobile development with HTML5 and PhoneGap. If you know anything about that I would be more than happy to chat with you... :)

First post achievement - Unlocked!