Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,672 members, 7,801,937 topics. Date: Friday, 19 April 2024 at 06:24 AM

Test Your Javascript Knowledge (pt 2) - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Test Your Javascript Knowledge (pt 2) (1623 Views)

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

(1) (Reply) (Go Down)

Test Your Javascript Knowledge (pt 2) by prodgalson: 8:05pm On Mar 26, 2008
Kudos to 2¢ for the idea of quizzes, but I think these people need a challenge smiley
This one is simple as well, but requires a bit of understanding on javascript assignment operators.

Just by looking at the below, can you tell WHICH of the alerts will popup, and WHY.
If you can't figure out which, run the code. Then try to figure out WHY.

(As a BONUS, what does the 1st alert print out? and WHY)


<html>
<head>
<title>Untitled</title>
<script>
function testYourKnowledge() {


var i = 0 || 23;

/* 1st Alert */
alert(i);


if(i = true){
/* 2nd Alert */
// Yes in the line above it's a single equals sign
alert("Hello"wink;
}



if (i=false){
/* 3rd Alert */
alert("wetin"wink;

if(i){
/* 4th Alert */
alert("how far"wink;
}
}

/* 5th Alert */
alert(i);

}
</script>
</head>
<body onLoad="testYourKnowledge()"></body>
</html>
Re: Test Your Javascript Knowledge (pt 2) by my2cents(m): 8:46pm On Mar 26, 2008
Well prodgal,

Since it wasn't required to post our answers and I have already had private conversations with you, I will say this one na phD levels and not spoil the fun for others grin

I got the answer after 2 tries but must confess that I wasn't able to explain why - typical example of knowing how to code with knowing the "why" behind it.

Thanks for bringing this up man. I hope others follow suit and post similar quizzes as well.
Re: Test Your Javascript Knowledge (pt 2) by prodgalson: 9:03pm On Mar 26, 2008
See silence, where are the web developers?

Afam? Timmy? mambenanje? kazey? Where are you chaps?

Maybe it's because of time zone difference sha, we'll see who responds by morning.
Re: Test Your Javascript Knowledge (pt 2) by Afam(m): 10:20pm On Mar 26, 2008
@prodgalson,

It seems almost every post I make is being turned into a competition so I am staying away from this.

I am on 24hrs internet access and I am enjoying the quizzes thus far.

Enjoy!
Re: Test Your Javascript Knowledge (pt 2) by mambenanje(m): 3:13pm On Mar 27, 2008
hey nice stuff
since I don't have time, let me just work it out and I don't realy want to test it, from replies I will know if I was correct or not


ANSWER

there are three alerts:

the first alert will display 23 // because the or in javascript will get to the second part. let me expand the code so it will be clear if((i=0) || (i=23)) so after such a line of code i will be holding 23 which is the last

the second alert shows hello //because the if statement ends as if(true)

the third alert shows false // because if(i=false) will boil down to if(false) and nothing will proceed and the next if also test if(false) and doesnt procceed and finally the last alert is alert(false) which is actually false

I hope my brainstorming is right
kudos to u prodgal but I must confess that I used the reasoning from actionscript 1.0 and not javascript but they are similar
Re: Test Your Javascript Knowledge (pt 2) by kheme(m): 3:54pm On Mar 27, 2008
ok,  here i go,

the first alert pops 23, because the 1st express is 0, and so i was ignored, and then it simply means var i = 23. if the '0' was changed to something like 1, then var i would equal 1.

the second alert pops hello cos the declaration i = true (same as var i = true; true being a boolean value) returns true after declaration.

the third alert won't pop because the declaration i = false in "if (i=false){" returns false, even though the value of i has already been changed to false (i have no idea why though)

also, the fourth alert won't pop because the value of i is now false and the if condition just before the 4th alert is the same thing as writing,  if (false){ (because i is NOW false)

the fifth alert pops false cos like i just said, the new value of i is now false

so em, how much did i win!?!?? grin tongue
Re: Test Your Javascript Knowledge (pt 2) by my2cents(m): 4:10pm On Mar 27, 2008
Yaaay Kheme!

You (as well as other responders, as a matter of fact) should immediately proceed to the following location to claim your prizes:

https://www.nairaland.com/nigeria/topic-123031.0.html
Re: Test Your Javascript Knowledge (pt 2) by prodgalson: 9:50pm On Mar 27, 2008
Nice one guys,

As for WHY, it has to do with the assignment operator and how it behaves in EcmaScript (JS was the originating language of ECMAscript , which is rather rampant right now (mambenanje: Actionscript is ECMAScript as well, but AS1 was a poor, incomplete implementaion. Get on board with AS3 man! wink )

Operators can be thought of as functions , in a sense that they evaluate/output/return a value, as well as performing their primary function. '+' is an operator, so '5+3' , adds the two numbers, and 'evaluates to' 8.
similarly, i = 3 does the assignment, assigning the right operand to the left operand, but it also actually 'evaluates to' 3
In general, doing if( x = y) does the assignment, but is also similar to doing if (y )
its a handy trick in some situations, at least for reducing lines of code, eg: writing this:

if (x = y)
is similar to doing
x = y;
if(x),

But even more interesting is i = 0 || 23
(Kheme got this one)
This has to do with logical assignment, one of my favorite smiley
The logical OR operator also 'equates to' a value (just like the assignment operator), and in in JS v1.2 and later, it is not simply boolean true or false, but actually returns either the value of the first operand (if it does not equate to false (or 0, null, undefined, or the empty string) ), or the value of the second operand (regardless of what it is)
so the right operand (0 || 23) evaluates to just 23.

This is usefu for shorthanding your code:
lets say, you want to do this:
" if x is empty string, set y = 23, or else, set y = x "

You could write:
if (x == ""wink{
y = 23
} else{
y=x;
}
But the short hand way is to do
y = x || 23

But you can even condense it further to just
x ||=23
though i think that's only in JS2.0 and later (you can do it in actionscript though smiley )
Re: Test Your Javascript Knowledge (pt 2) by prodgalson: 9:49pm On Apr 09, 2008
any questions?

(1) (Reply)

Smf Or Phpbb? Which Is Better? / Criticize The Nairaland Website / Pls, Dear Nairaland Webmasters, Help Me Review My Website.

(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. 26
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.