måndag 7 februari 2011

Week 2



As I did not have much time I´m not very satisfied with my answers, but I tried. Not all questions is answered, I just did not have enough knowledge for those.
  1. Why do languages provide the switch statement, when we can achieve the same thing with multiple if... elseif statements? Show one example of how you might use the switch statement.
Douglas Crockford mentions that it is a “multiway branch”, and according to Wikipedia that is:
“A multiway branch is a computer science term used to describe the change to a programs control flow based upon a value matching a selected criteria.”

Also “the switch value does not need to be a value. It can be a string”

Also “the case values can be expressions”
  1. What is encapsulation, and what do functions encapsulate?
“encapsulation” is never mentioned, but I think you mean that curly braces is needed around the function.
  1. What is a pure function? Is the function show() provided in Eloquent Javascript a pure function?
A function that does not need to be in a specific place to work, and it is a function that will always reurn the same value when given the same arguments.
Show() is not pure because it depends on the presence of a special place on the screen.
  1. What do we mean when we say a variable in a function is shadowing a top level variable?
If the variable exist in two places the one in the function has a higher precedence when the function is called. Like this:
This program will output:
10
4
          <script type="text/javascript">
                                  var a=5;
                                  var b=5;
                                  var c=a+b;
                                 
                                  function shadow(a,b) {
                                  a=2;
                                  b=2;
                                  c=a+b;
                                  return c;
                                  }
                                  document.write(c+"<br />");
                                  document.write(shadow())
          </script>
  1. A recursive function, must have some sort of an end condition. Why would we get a "out of stack space" error message if a recursive function does not have an end condition?
Maybe because otherwise it would repeat itself to many times. Data about the position in the context is stored in the computer’s memory for as long as the function is running. 
  1. Reflect about the difference between object inheritance and class inheritance
An object can inherit from an older object
  1. What is object augmentation, and how do we do it?
“Add stuff to it” Give new members and new methods by just assigning it.
  1. There is a way to add a method to String, such as any new String we create will have that augmented method (this is a bit different from object augmentation). How would you do this?

  1. What is garbage collection?
“JavaScript uses garbage collection to reclaim the memory occupied by strings, objects, arrays, and functions that are no longer in use.”-Copied from JavaScript: the Definite Guide fourth edition.
  1. What is the difference between an array and an object?
Arrays indexes are converted to strings and used as names for retrieving values.


Exercise 1
Ex.3.1

Will return the number 75. If the given number is negative the calculation will look like this: -(-75) -and- will end up +. Not very logic, but that is one of those thing I just need to accept. Because I can’t say that it feels correct that 10-(-75) =85 but it is.

          <script type="text/javascript">
          function absolute(number) {
                                  if (number < 0)
                                  return -number;
                                  else
                                  return number;
                                  }
                                  document.write(absolute(-75));
          </script>

Exercise 2
Ex.3.2

Will return true because 11 is greater than 10.

          <script type="text/javascript">
                                  function greaterThan (number1) {
                                  return function (number2) {
                                  if (number2 > number1)
                                  return true;
                                  else return false;
                                  };
                                  }
                                  var number = greaterThan(10);
                                  document.write(number(11))
          </script>

Exercise 3

I had to surrender to this one, it was too hard for me with the knowledge I have.




glenn.hellquist@gmail.com
webbkraft.com

/*#p2pu-Jan2011-javascript101*/

2 kommentarer:

  1. 1. What you say is right about the switch statement, but it doesn't actually answer why it exists over the equivalent if/else if/else ... construct. Just because it allows you to switch on strings doesn't mean you can't do that with the longer if / else if chain. Why do you think we'd use a switch as opposed to the if/else if construct?

    2. I would do some research on encapsulation, as it is a key aspect of programming. There's more to it than curly braces.

    Looking forward to future posts
    Nick

    SvaraRadera
  2. Thanx for the comment, I will look into encapsulation as you recommended.

    I will also look into switch statement. But I did not really se the point as Eloquent JavaScript were quite negative to the switch statement.

    SvaraRadera