By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

Modulus

P: n/a
Hi there,

I'm working on this piece of code, but as i'm new to javascript i don't
understand this and what it means especially the question mark. count
% 2 ==1?
<HTML>
<HEAD><TITLE>EXAM QUESTION</TITLE>
<SCRIPT LANGUAGE="JAVASCRIPT">

var count = 1;

while (count <=8) {
document.writeln(count % 2 ==1? "XXXXX<br />" : "$$$$<br />");
++count;
}

</script>
</head>
<body>
</body>
</html>

Nov 30 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
shannon wrote:
I'm working on this piece of code, but as i'm new to javascript i don't
understand this and what it means especially the question mark. count
% 2 ==1?
It is a trinary operator, details here (they talk about C, but the
principles are the same):
http://www.scit.wlv.ac.uk/cbook/chap5.trinary.html

There are a few issues with your markup though.

Missing Doctype.
<HTML>
<HEAD><TITLE>EXAM QUESTION</TITLE>
<SCRIPT LANGUAGE="JAVASCRIPT">
Missing type attribute. The language attribute is deprecated.
var count = 1;

while (count <=8) {
This would probably be better off written as a for loop.

for (var count = 1 ; count <= 8 ; count++) IIRC.
document.writeln(count % 2 ==1? "XXXXX<br />" : "$$$$<br />");
<br /> in an HTML document means "a line break followed by a visible '>'"
character (although most browsers get this wrong). Things are different in
XHTML, but you aren't using that (and document.write doesn't play nice with
XHTML in current browsers).
</head>
In HTML the end tag for the head, and the start tag for the body are
optional, so you can put content directly after the title - but you are
document.writing content and then trying to close the head element. That
script element should probably be moved to the body.
<body>
</body>
</html>


--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Nov 30 '05 #2

P: n/a
David Dorward wrote:
shannon wrote:
[...]
var count = 1;

while (count <=8) {

This would probably be better off written as a for loop.

for (var count = 1 ; count <= 8 ; count++) IIRC.

or:

var count = 0;
while (count++ < 8) {

[...]
--
Rob
Nov 30 '05 #3

P: n/a
David Dorward wrote:
In HTML the end tag for the head, and the start tag for the body are
optional, so you can put content directly after the title - [...]


You are right, but I would not count on browsers to get it right that
do not get SHORTTAG right. Which are indeed many, but not all.
Regards,
PointedEars
Dec 1 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.