P2PU – JS Chapters 2-3

Feb 06

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));

Leave a Reply