söndag 30 januari 2011

document.write() alternatives?

In the book they use "print()" and "show()", but they don't work in the browser. So my questions are:

1. Why do they use examples that do not work? they should teach stuff that we can use.

2. The only way I know to output content in JS is "document.write()", is there a better, or at least another way? it's quite long to type document.write compared to PHP where "echo()" or "print()" does the same thing.


glenn.hellquist@gmail.com
webbkraft.com

/*#p2pu-Jan2011-javascript101*/

Week 1 Homework


Homework
Ex 2.1 - 2.6 + guard & default operator

Ex.2.1

(4 >= 6 || "grass" != "green") && !(12 * 2 == 144 && true)

||=If any of the values is true, it is true.
&&=Is true if, in this case, both the first AND the second value is true.
!=is NOT

4 >= 6
4 is not greater than or equal to 6 – false

"grass" != "green"
The words grass and green is not same – true

Because of the ||-operator the first parenthesis returns true because one of the values is true.

12 * 2 == 144 is false

True is true

Because of the &&-operator both values needed to be true, therefore this parenthesis returns false.

But with the “!” before the parenthesis we get !false which is true

We now have true from the first and !false(true) from the second.
With the remaining &&-operator it looks like this: true && !false which is true.

The answer: true.





Ex.2.2

Will give the result 1024 because *2 is calculated until the “times”-variable reach 8. We needed *2 10 times. When “time” reach 8 it has made 9 loops because 0 also is one time, and I started with the first “2” set in the “number”-variable.

          <script type="text/javascript">
          var number = 2;
          var times = 0;
          while (times < 9) {
          number = number * 2;
          times = times + 1;
          }
          document.write(number);
          </script>






Ex.2.3

Not possible :S
I have tried everything, even copied their solution and just replaced print with document.write.

        <script type="text/javascript">
        var line = "";
        var counter = 0;
        while (counter < 10) {
        line = line + "#";
        document.write(line);
        counter = counter + 1;
        }
         
        </script>

Anyone know what’s wrong? Please comment.





Ex2.4

Returns 1024.

          <script type="text/javascript">
          var number = 2;
          for (var times = 0; times <9; times = times + 1)
          number = number *2;
          document.write(number);
          </script>

I have not tried the triangle, because I did not get the first one to work.





Ex.2.5





Ex.2.6





The “default”-operator:




The “guard”-operator:





glenn.hellquist@gmail.com
webbkraft.com

torsdag 27 januari 2011

Week 1

Track 1


  1. The alliance of Netscape and Sun Microsystems did not use Java in the browser, because it was not suitable for that purpose. Why do you think Java was not suitable to be embedded in a browser?
Java was too heavy and clumsy.
  1. When parsing a String into a number using the parseInt() method, we are advised to always provide the radix. Why is it so strongly recommended?
The radix specifies what numerical system is used. If not specified, it will be decided by JavaScript itself, and that can cause unexpected results.
  1. What is a type, and why do you think types are useful in writing programs?
Really hard to put words on this one. The type can be for example: string, numeric or Boolean. JavaScript will handle the information differently depending on the type.
It is useful because a wide range of options gets available. Some events may only be executed if another event returns true or false, or if the result is equal, less than or greater than a value.
Control is the word.

Comment from Parag:
Think of type as something which offers certain buttons and actions when you press those buttons. So a type could be a DvdPlayer, in which case you will have buttons to start, stop, pause, forward, rewind, etc. If the type is a car then you will have start, stop, change_gear, etc.

Basically a type defines a universe within which certain things are possible by pressing buttons. The type also defines those buttons.

Coming back to programming, String is a type which has certain functions. These functions are the buttons, and what they do is the effect.
  1. 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?
Document.write(0.1+0.2) gave the result: 0.30000000000000004
While
              a=0.1;
              b=0.2;
              c=a*100;
              d=b*100;
              e=c+d;
              f=e/100;
                          
              document.write(f)
gave the result: 0.3
I didn’t manage to get 0.3 as the result without multiplying a and b with 100 separately. (a+b)*100 did not give the expected result.
  1. Do you understand why the following operation produces the given result 115 * 4 - 4 + 88 / 2 = 500
115*4=460
460-4=456
88/2=44
456+44=500
* and / have higher precedence and is therefore calculated before. This is why the result is 500 and not 272. (115+4)-4+(88/2) = 500

  1. What does typeof 4.5 do, and why does typeof (typeof 4.5) return "string" ?
typeof 4.5 returns: “number” just as typeof (4.5) would do.
But why
typeof (typeof 4.5) return "string", that I can’t find the answer for. Could it be that the result of typeof 4.5 is returned as a string?





glenn.hellquist@gmail.com
webbkraft.com

söndag 16 januari 2011

JavaScript 101

This is the blog where my assignments will be posted.

This first post is just to make sure it works as my first assignment was sent in via the "Apply form".
However, task#2 from the first assignment can be found here: http://jsfiddle.net/Ekul/4RAWL/


glenn.hellquist@gmail.com
webbkraft.com