Javascript: Does anyone have full understanding?

Jordie

Suspended / Banned
Messages
1,828
Name
Jordie
Edit My Images
Yes
Hi to all who know.

I am teaching myself JS from a book, which I must say is top notch.

The only down side with it, is it doesnt fully explain some of the expressions.

<html>
<head>
</head>
<body>
<script language="javascript">
daysOfMonth = 29;
Year = 2009;
if (daysOfMonth == 28 || daysOfMonth == 29)
month = "February";
if (daysOfMonth == 29 && (Year % 4) == 0)
LeapYear = true;
document.write("Days of Month: " + daysOfMonth + "<br>");
document.write("Month: " + month + "<br>");
document.write("Year: " + Year + "<br>");
document.write("Leap Year: " +LeapYear +"<br>");
</script>
</body>
</html>

Here is the code I am working on. I just dont understand why the bits in bold is the way it is.

And whats the deal with the "||" isnt it just the same as "&&"?

Can anyone explain this too me?

Thanks in advance.
 
And whats the deal with the "||" isnt it just the same as "&&"?

Can anyone explain this too me?

Thanks in advance.

I don't know JS but I'd guess that "||" means OR and "&&" means AND.

(Year % 4) == 0) is probably testing that the result of Year divided by four is an Integer .





* I'ts been 10 years since I played with Basic/Fortran/Pascal/Coral/Occam & a few assembly languages :lol:
 
I don't know JS but I'd guess that "||" means OR and "&&" means AND.

(Year % 4) == 0) is probably testing that the result of Year divided by four is an Integer .





* I'ts been 10 years since I played with Basic/Fortran/Pascal/Coral/Occam & a few assembly languages :lol:

Nice one, made it much clearer than the book :D

Its been 6 years since I started playing around with HTML (16 now) I'm a bit geeky :lol:
 
Nice one, made it much clearer than the book :D

Its been 6 years since I started playing around with HTML (16 now) I'm a bit geeky :lol:


I think I wrote my first programs in 1979 on a DEC system 10 :eek:
 
|| = OR
&& = AND
= (Single Equals) sets a value to another value, eg
x = 10 will set x to 10.
== (Double equals) is for comparison.

Be careful with equals and double equals as its easy to change values by accident if you use the wrong one.
 
The % is modulus (Division remainder)
So, what this is saying:
if (daysOfMonth == 29 && (Year % 4) == 0)
LeapYear = true;


Is
If daysofmonth is equal to 29 and year is divisable by 4 with no remainder then it is a leap year.
 
The % is modulus (Division remainder)
So, what this is saying:
if (daysOfMonth == 29 && (Year % 4) == 0)
LeapYear = true;


Is
If daysofmonth is equal to 29 and year is divisable by 4 with no remainder then it is a leap year.

Yep, that's right
 
Cowasaki must be the man for this, being as he wrote the exif border script in JS.
 
There seems to be a fair few of us software engineers on TP...
 
Software Engineer see, letters after my name an everything :D

I would have imagined that it would be clear by your nickname in an instance.
That said, I don't really use the byte order mark in my utf-8 files.
Although I guess that belonged to the "everything" part of your sentence. :D

I'm only a humble programmer :P
 
The % is modulus (Division remainder)
So, what this is saying:
if (daysOfMonth == 29 && (Year % 4) == 0)
LeapYear = true;


Is
If daysofmonth is equal to 29 and year is divisable by 4 with no remainder then it is a leap year.

Cheers BOM.

I had the modulus section a bit earlier in the book.

Didnt get it though, until a long lesson from the father who explained it all for me :lol:



My bad, didnt think about putting it in this section.

Ta for the mod/admin that moved it :thumbs:
 
I would have imagined that it would be clear by your nickname in an instance.
That said, I don't really use the byte order mark in my utf-8 files.
Although I guess that belonged to the "everything" part of your sentence. :D

I'm only a humble programmer :P

:lol:

BOM is actually short for BigOrangeMonkey :D
 
New question...

<script language="javascript">
var1 = 99;
(isNaN(var1))?document.write("The variable, " + var1 + " is not a
number.<br/>"):document.write("The variable, " + var1 + " is a number.<br/>");
</script>

Can you see anything wrong with this statement? because I cant :shrug:
 
For the ? : operator you usually have to assign it to another variable rather than use it to output text e.g.

f = (a==b) ? 1 : 0 ;

So f is set to 1 if a==b and 0 otherwise.

try

str = (isNaN(var1)) ? "The variable, " + var1 + " is not a
number.<br/>" : "The variable, " + var1 + " is a number.<br/>";

document.write(str);
 
For the ? : operator you usually have to assign it to another variable e.g.

f = (a==b) ? 1 : 0 ;

So f is set to 1 if a==b and 0 otherwise.

I see where your coming from, but the book lies then :bang:

I am doing a page on Identifying numbers.

basically all I am trying to do i using the isNaN expression to determine whether the variable is a string or a number.
 
I think I was editing my entry when you replied, try the snippet I posted above.
 
For sake of readability, don't use the ternary operator for longer statements, use an if else construct with proper indentation. You'll probably be thanking yourself later on when you get back to that code.

I may well be :lol:
 
It's scripting not programming ...

I spent years learming perl.. then PHP came out and......
 
Slightly on/off topic, the only javascript I know is when you type into the URL bar on an internet browser
javascript:alert("message")
it pops up a little warning box in the middle of the screen.
Awesome for going onto your mate's computer when he's getting stuff from the printer and telling him he's a gaybo. :p
 
Back
Top