lördag 12 mars 2011

JavaScript kursen avslutad

Jag nu är JavaScript kursen avslutad och jag vrider tillbaka till Svenska för att fortsätta bloggandet.

Vilken väg det bär åt nu vet jag inte, men att det kommer ha att göra med mitt nystartade företag Hellquist Webb och webbdesign är iaf säkert.


Hellquist Webb

glenn.hellquist@gmail.com
Webbkraft

söndag 20 februari 2011

Week 4 homework. Attempt 1

When inserted in the source code, this program counts the "#text" nodes and returns the result.
But I don't thing this is the solution to this weeks homework because my program seems to find only the whitespace(which is of type #text) and not the actual text. So I will continue working on this.

Anyway, the result  I get is 7  text nodes in the childnodes of <body>

<form style="text-align:center">
<input type="button" value="Click me!" onclick="countText()" />
</form>

<script>
function countText() {
var nr = 0;
var counter = 0;
    for (nr=0;nr<500;nr++) {
    var theHtmlNode = document.childNodes[1];
    var theBodyNode = theHtmlNode.childNodes[1];
    var theRest = theBodyNode.childNodes[nr];

    if (theRest.nodeName == "#text") {
        counter++;
    }
    else if (theRest.nodeName == "SCRIPT") {
        break;
    }
    //alert(theRest.nodeName);/*this can be used to manualy go through the nodes to see what it actually finds.*/
    }
alert(counter);
}

</script>



glenn.hellquist@gmail.com
webbkraft.com

/*#p2pu-Jan2011-javascript101*/

lördag 19 februari 2011

I don't understand why

1)
The following code does not give the expected result. I think it should alert "P", and not "H1" as it does. What do you think?

<html>
  <head>
    <title>This is a Document!</title>   
  </head>
  <body>
    <h1>This is a header!</h1>
    <p id="excitingText">
      This is a paragraph! <em>Excitement</em>!
    </p>
    <p>
      This is also a paragraph, but it's not nearly as exciting as the last one.
    </p>
   
<script>

var theHtmlNode = document.childNodes[0]; // the first child of document is HTML

var theBodyNode = theHtmlNode.childNodes[1]; // the second child of HTML is BODY

var theParagraphNode = theBodyNode.childNodes[1]; // the second child of BODY is P

alert( "theParagraphNode is a " + theParagraphNode.nodeName + " node!" ); // so how can this alert H1?

</script>
  </body>

</html>

2)
Also this is very confusing: 

<html>
  <head>
    <title>This is a Document!</title>  
  </head>
  <body>
    <h1>This is a header!</h1>
    <p id="excitingText">
      This is a paragraph! <em>Excitement</em>!
    </p>
    <p>
      This is also a paragraph, but it's not nearly as exciting as the last one.
    </p>
   
<script>
var theParagraphNode = document.getElementById('excitingText');
if ( document.firstChild.lastChild.childNodes[3] == theParagraphNode ) {
 alert( "theParagraphNode is exactly what we expect!" );
}
</script>
  </body>

</html>


Why is "P" now the fourth node?
Does it count like this:
1 HTML
2 BODY
3 H1
4 P

or how does it work? 


glenn.hellquist@gmail.com
webbkraft.com

/*#p2pu-Jan2011-javascript101*/

tisdag 15 februari 2011

Week 3


  1. In Javascript, functions are first class objects. What does it mean to be a first class object?
First class objects are special types of objects that can do the same things as objects.

  1. Functions and variables share the same namespace. What does this mean and what important implication does this have?
If I have a variable and a function with the same name the program will not work.
          <script type="text/javascript">
                          var test = 1;
                          function test(number) {
                          number = 2;
                          return number;
                          }
                          alert(test());
                         
          </script>
This will not give any output as long as both the function and the variable have the same name “test”.

  1. Douglas Crockford equates Javascript functions with Lambdas, and also mentions that they are a secure construct. Can you do some research and reflect on what he means by 'secure construct'?

  1. Can you explain the concept of a closure.
“The scope that an inner function enjoys continues even after the parent function have returned.”
I got the feeling that this meant that a function within a function does not reset after the “outer” function is done. The inner function goes on until it fulfills it owns conditions.
Looking forward to response on this one, also very curious what others have answered.

  1. What is the difference between a function and a method?
A method is a property that contains a function.

  1. In Javascript there is no implicit type checking of arguments passed to functions. This could lead to bugs if programmers are not careful about what they pass to functions. If you create a function, how would you protect it from well meaning but careless programmers?

    Don't know yet.

  1. Javascript functions have implicit access to something called this. this points to different things depending on how the function was created. Can you explain why we need this, and what it represents in different type of functions.

No, I can’t.


  1. Why doesn't Javascript have a cast operator?
Maybe because JavaScript is clever enough to understand what type the value is, and do not need to be told.

  1. What is reflection and why is it easy in Javascript (you may need to do research to answer this question)?
I don’t know.

Homework 1
Ex. 6.1
Seriously, these exercises from eloquent javascript is so useless that they are not worthy my time. I mean, not even the code in the “solution” give any output in their own console. How am I supposed to learn? And the exercise does not use stuff we learned from the text before the exercise. No, they present new stuff that is needed in the solution. We should not find new stuff in the solution, everything that is needed should be in the text just before the exercise.
I had enough of this now. I was hoping to learn something useful from this course, but with exercises that only work in their own custom made console I just don’t see the point with it.
I have learned a lot more from Google and the Douglas Crockford videos on my own.




glenn.hellquist@gmail.com
webbkraft.com

/*#p2pu-Jan2011-javascript101*/

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*/

tisdag 1 februari 2011

Browser solution for 2.3 and 2.4 triangels


The solution for 2.3 and 2.4 triangels – thanx to adaptives

2.3
The <br /> tag must be quoted to work.

        <script type="text/javascript">
        var line = "";
        var counter = 0;
        while (counter < 10) {
        line = line + "#";
        document.write(line + "<br />");
        counter = counter + 1;
        }        
        </script>
2.4
The curly braces must be added, or else it will output only once.

          <script type="text/javascript">
          var number = "";
          for (var times = 0; times <10; times = times + 1) {
          number = number + "#";
          document.write(number + "<br />");
          }
          </script>


glenn.hellquist@gmail.com
webbkraft.com

/*#p2pu-Jan2011-javascript101*/

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*/