Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,208,670 members, 8,003,323 topics. Date: Friday, 15 November 2024 at 11:25 AM

Test Your Javascript Knolwedge. - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Test Your Javascript Knolwedge. (1016 Views)

Test Your Web Programming Skills / Test Your Javascript Knowledge (pt 3) / Test Your Javascript Knowledge (pt 2) (2) (3) (4)

(1) (Reply) (Go Down)

Test Your Javascript Knolwedge. by my2cents(m): 2:20pm On Mar 26, 2008
This quiz is based on some piece of code I just stumbled upon. You are to do it based on the honor code system (in other words, first solve it on paper, then run it in your favorite browser):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script>
function testYourKnowledge() {
var i = 1;
if(1) {
i = 2;
if(1) {
i = 3;
alert(i);
}
alert(i);
}
alert(i);
}
</script>
</head>

<body onLoad="testYourKnowledge()"></body>
</html>

Tasks:
1) Run this on paper, and paste your result here
2) Run it in your favorite browser, see if the results you get equal what you posted here. KEEP THE ANSWER TO YOURSELF till the opportuned time.
3) In about 2 hrs, I will post the correct answer
4) Come back here, share your answer and explain why you think the answer you had equals (or doesn't equal) the correct answer.
5) This isn't limited to those in this forum but the "programmers" as well.

(HEY! COME ON! I SEE YOU RUNNING THE CODE ALREADY! BE HONEST - DO IT ON PAPER FIRST! cool)

So what are you waiting for? Enough with the "site reviews". We are developers. Let's put on our developer hats for a change and get our feet wet wink
Re: Test Your Javascript Knolwedge. by timmy(m): 2:43pm On Mar 26, 2008
3 (didn't even need a paper for this tongue )
Re: Test Your Javascript Knolwedge. by tomX1(m): 3:06pm On Mar 26, 2008
Definitely 3

Nice teaser by the way.
Re: Test Your Javascript Knolwedge. by mambenanje(m): 4:38pm On Mar 26, 2008
hey guys come on why 3
is it because 1 in javascript is true or what ??
because if 1 is not true in javascript like it some other languages then the answer is 1 and if its true the answer is 3.
anyway I think in javascript anything other than an empty string (" "wink and false is true
so the answer is 3
Re: Test Your Javascript Knolwedge. by kazey(m): 5:17pm On Mar 26, 2008
i don't know about javascript coding practices, but there is something wrong


var i = 1;
if(1) {
i = 2;
}

isn't it supposed to be
if (condition)

so the proper synthax would be if ( i == 1) and vice versa to arrive at the respective result?

Here there is no condition, so how did you guys arrive at a result? Please don't tell me because you saw the alert after running it on your browser?
Re: Test Your Javascript Knolwedge. by my2cents(m): 5:22pm On Mar 26, 2008
Pretty interesting guys. Keep it coming cool

I am actually busy working on something so lucky for you all, the time for posting an answer has been extended by at least an hour.

I will definitely keep you all posted. One thing is for sure though - we need to do more of these grin
Re: Test Your Javascript Knolwedge. by prodgalson: 5:40pm On Mar 26, 2008
hrm, i forget how js behaves with poorly written code tongue ,

BUT :

1) If javascript auto-casts '1' to a boolean, and equates it to true, and
2) Assuming alert casts untyped vars as a strings,

then the alert will show '3 each time


ps: Just kidding about poorly written code, JS itself is crappy because it allows for loose typing , and untyped vars, i has no type, and alert should be strict and take strings only, that being said, i miss javascript, i could be lazy smiley
Re: Test Your Javascript Knolwedge. by kazey(m): 5:51pm On Mar 26, 2008
the question is "if what is true?" is it if a variable contains a value? what exactly are we testing for? smiley
Re: Test Your Javascript Knolwedge. by my2cents(m): 6:47pm On Mar 26, 2008
Very philosophical Kazey cool

Okay, I thought I would get more answers but I guess not. That tells me, we are more of designers in here than developers but I could be wrong. That notwithstanding, I will provide the answer and the explanation as well.

The answer is "3" printed out 3 times (hint: count the number of calls to alert wink)

First and foremost, I noticed I got more YIMs than posts here. To collate all the questions/issues/concerns, I would like to say that the aim of this post wasn't to make anyone feel bad or to intimidate anyone or to say, "that answer sucks!". We are all here to learn. I have always strongly believed that. I was actually hoping for more of a discussion, which does exist but just not from as many people as I expected.

Even I was thrown for a loop here. Well, technically I was correct. I said "3", "2", "1". See, Javascript is very funny with variables and scope. In Javascript, every variable has global scope. Memory is allocated once to a variable and if a call to that variable is made again, it's original value gets overridden by the new value. Counter that with the way it is in other programming languages. This is where namespacing, use of anonymous functions and self-contained, self-executing scripts come in. Also, in javascript, a boolean by default is true. That is why "if(1)" always executes regardless.

The lesson: You have to be very careful with how and where you use your variables. Why? Cos that "i" var for instance that you call somewhere on your page might be interfering with another "i" variable declared in an imported js file. This might even be worse in cases where you are using libraries like scriptaculous, dojo, etc.

So to conclude: The answer is "3", "3" and "3". Thanks for participating in this. I will definitely come up with more of this. Hopefully, I have motivated enough people to come up with a similar exercise.

Thanks once again.

(1) (Reply)

Partner With Me / How To Choose A Good Domain Name For Your Website / Need To Register A Domain . . .any Suggestions?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 21
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.