Click to See Complete Forum and Search --> : need explainations on how a script works


Technodynamic
04-15-2006, 04:55 PM
I've used javascript for awhile, all self taught, but now I am learning it for real so I can get certified. I have a few script questions and for anyone with some experience, they should be fairly easy. I really appreciate the help.

Also, please provide how you got the answer so I can learn. :o :)

What is the value of "total" after execution of the below code?
j = 0;
total = 0;
do
{
j++;
total = total ^ j;
}
while (j < 2);



Referring to the below code, if checkbox "s" is checked, and parameter "fld" is equal to "q," what does function calc return?
function calc(form,fld) {
var d = 0;
if(fld == "r") {
if(form.s.checked) {
d=Math.sqrt(form.r.value); }
else { d=form.r.value/2; }
} else {
if (form.s.checked) {
d = form.e.value * form.e.value;
} else { d = form.e.value * 2; }
}
return d;
}



Referring to the below code, what is the value of "ch2" after execution?
var ch1 = 'b';
var ch2;
switch(ch1) {
case 'a':
ch2 = '1';
case 'b':
ch2 = '2';
case 'c':
ch2 = '3';
break;
default:
ch2 = '4';
}

A1ien51
04-15-2006, 10:49 PM
Copy and paste them and run them.

Look at this http://www.w3schools.com/js/default.asp there are explainations.

Eric

Kramy
04-15-2006, 11:45 PM
0 ^ 127 returns 127.
1 ^ 127 returns 126.
2 ^ 127 returns 125.

It's bit logic. Binary related. The answer is 3.


For your third one, the answer should be pretty obvious. (3)

Default: is only called when there are 0 matches. When a case matches, all actions afterwards will be executed, and all cases until the next break will be ignored. Think of it like a dish washer - once you hit the right part of the cycle('b'), all the other parts will be executed until it stops(break;)