Test Your Javascript Knolwedge.

A Member? Please Login  
type your username and password to login
Date: October 13, 2008, 06:03 AM
249192 members and 147767 Topics
Latest Member: murphyscop
Nairaland [Nigerian Forum] Home Help Search Who is currently online? Login Register
Nairaland Forum  |  Technology  |  Webmasters (Moderators: SAM MILLA, uspry1)  |  Test Your Javascript Knolwedge.
Pages: (1) Go Down Send this topic Notify of replies
Author Topic: Test Your Javascript Knolwedge.  (Read 140 views)
my2cents (m)
Test Your Javascript Knolwedge.
« on: March 26, 2008, 02:20 PM »

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

Quote
<!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
timmy (m)
Re: Test Your Javascript Knolwedge.
« #1 on: March 26, 2008, 02:43 PM »

3  (didn't even need a paper for this  Tongue )
@tomX (m)
Re: Test Your Javascript Knolwedge.
« #2 on: March 26, 2008, 03:06 PM »

Definitely 3

Nice teaser by the way.
mambenanje (m)
Re: Test Your Javascript Knolwedge.
« #3 on: March 26, 2008, 04:38 PM »

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 (" ") and false is true
so the answer is 3
kazey (m)
Re: Test Your Javascript Knolwedge.
« #4 on: March 26, 2008, 05:17 PM »

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?
my2cents (m)
Re: Test Your Javascript Knolwedge.
« #5 on: March 26, 2008, 05:22 PM »

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
prodgalson
Re: Test Your Javascript Knolwedge.
« #6 on: March 26, 2008, 05:40 PM »

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
kazey (m)
Re: Test Your Javascript Knolwedge.
« #7 on: March 26, 2008, 05:51 PM »

the question is "if what is true?" is it if a variable contains a value? what exactly are we testing for?  Smiley
my2cents (m)
Re: Test Your Javascript Knolwedge.
« #8 on: March 26, 2008, 06:47 PM »

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? because 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.
 Interview With Simon Gooffin Ceo Esyndicat  Does It Worth It?  Cool Porno Paris Hilton!  Page 2
Pages: (1) Go Up Send Topic to Friend by E-mail Reply 
Google
 
Web www.nairaland.com
Sections: TV/Movies (2) Music/Radio (2) Celebrities Job Talk Jobs/Vacancies (2) Career Talk Romance Books Politics Sports Fashion Travel
Health Schooling Religion General(2) Business Webmaster Programming Computers Phones Cars & Trucks

Links: Page1 Page2 Page3 Page4 Page5 Page6 Page7 Page8 Page9 Page10

Nairaland is owned by Oluwaseun Osewa
Nairaland Forum | Powered by SMF 1.0.12.
© 2001-2005, Lewis Media. All Rights Reserved.