Webcraft

P2PU – JS Chapters 2-3

Posted by Alex Mrvaljevich on 6:18 pm in webcraft | 0 comments

Well, this is my second post regarding the JS 101 Class, and i must admit it took me quite a while to complete the reading, mostly because  of the unfamiliarity of much of the terms, I will try to break it down as simply as I can, partly because the assignment asks us to, but also because if I delve too deep I might get lost again.

The first bit is all about the history behind JS, and that is covered pretty well in the last post, but the second part goes a bit further down into the types and variables of JavaScript.

Most of this is pretty much the same as I was taught in Calculus: Multiply First, resolve everything above the division, then divide… its simple arithmetic and should be straightforward to anyone that paid attention in math (if you didn’t… you should have, it was fun).

But to tell the computer how you want the data processed, you need to define what are called Variables; think of these as the “names” you give to the stuff that gets thrown around in your program.

There are many kinds of variables in JS, but because it is a “loose typed” language, you can pretty much throw anything in the “var” bucked and JS will figure out what to do with it afterwards… actually JavaScript is pretty permissive with how you develop, even though I have no experience with it I guess that it can lead to some fast development, and also some sloppy code if you don’t mind your standards and conventions.

Some basic stuff:

  • Use && when you want to check if both conditions are true in an argument
  • Use || if you want to check if either of the conditions are true
  • Use == to check if two things are alike
  • Use != to check if two things are different

The rest of the chapter goes on to define some other basic functionalities of development, the famous IF statement that has been around for aaaaages and its variations (If else, for, while, etc.) where it really starts to get complicated in is functions (chapter 3).

To me the most difficult part to understand was the bit about recursion, I never seem to understand how the program knows when to stop, but I guess I will pick that up in further chapters, for now the basics are that you use functions as segments of code you can call up at any time to solve the different problems you come across during your program, They are there to help, but in a non-obtrusive way: variables defined within functions can use any name without interfering with other variables, they can be nested within each other to create complexity but still be unobtrusively tucked away in the “stack”.

Complex themes like recursion and “closure” took me a bit of a read to understand, but I hope as we delve deeper into the book the exercises help understand the rest. For now, here are my assignments:

Ex. 2.1:

((False) || (True)) &&

! ((False) && (True))

(True) && ! (False)

(True) && (True)

(True)

Ex. 2.2:

var base = 2;
var power = 2;

while (power <=10 ) {

power = power + 1;
base = base * 2;
}
print (base);

Ex.  2.3

var line = ”";
var count = 0;
while (count < 10) {
line = line + ”#”;
print(line);
count = count + 1;
}

Ex. 2.4

var base = 1;

for (var power = 0; power < 10; power = power + 1) {
base = base * 2;
}
print(base);
—–

var line = ”";

for (var count = 0; count < 10; count = count +1) {

line = line + ”#”;
print(line);

}

Ex. 2.5

var result = 4;
var answer = Number(prompt(“2 + 2 is…?”, ”you can do it”));

if (answer == 4)
alert (“whow!”);
else if (answer == 3)
alert(“Almost!”);
else if (answer == 5)
alert (“Almost!”);
else
alert (“Something Mean :-) ”);

Ex. 2.6
var answer;
while (true) {
answer = Number(prompt(“2 + 2 is…?”, ”you can do it”));
if (answer == 4) {
alert (“whow!”);
break;
}
else if (answer == 3 || answer == 5) {
alert(“Almost!”);
}
else {
alert (“Something Mean :-) ”);
}
}

Ex 3.1

function absolute(number) {
var result = ”";
if (number < 0)
result = number * -1;
else
result = number;
return result;
}
show (absolute(-3))

Ex 3.2

function greaterThan(number) {
return function Test(argument) {
return argument>number;
}
}

var input = greaterThan (90);
show (input(100));

Mozilla – P2PU School of webcraft

Posted by Alex Mrvaljevich on 12:12 pm in webcraft | 2 comments

Mozilla – P2PU School of webcraft

School of Webcraft For the following 10 weeks, i will digress from the original concept of this blog as i will (hopefully) be attending some classes at  Mozilla-P2PU School of Webcraft, this post in particular relates to a Javascript 101 class that i will be applying for.

The assignment was to watch a video (here), a brief introduction to JavaScript and blog about it.

First, i want to clear something up… i finally understood the joke “Java is to Javascript as Car y to Carpet”.

The main reason why i was never interested in JS was that i thought it was just an version of Java, which through the video y learned has nothing to do with each other, which is a very unfortunate mishap for the language as it gets a very bad rep.

It is important also to state that i am not a web developer, as such this is all a bit daunting for me, but also necessary. I did not know about “loose typing” Vs. “strong typing”, seeing as how Java ys a Loose Typing language (i.e. the “Types” of the variables don’t have to be set when you initialize them) i can understand both the benefit of added flexibility and coding speed, as well as the dangers of misstyping a variable and having the code blow up in your face.

The second assignment was a simple alert bot, (which you can find here) and the first thing that happened was that i concatenated using the + sign instead of adding, you have to be very careful of that as the sign has multiple uses and if you don’t add the proper syntax you can end up with cases like “2″ + “2″ getting 22 as a result.

All in all it was a fun morning, the video is very entertaining, and i hope i am accepted into the program.