söndag 30 januari 2011

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

Inga kommentarer:

Skicka en kommentar