'var' question

P

Prophet

I am using the following for a different picture to show up every day....

<script language="JavaScript">
<!--
function adspic() {

var mydate=new Date()
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var adspicarray=new Array("ads/images/kilt.jpg",
"ads/images/kilt.jpg",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jpg",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jpg",
"ads/images/apronandcase.jpg")

document.write("<p align='right'><img src = '" + adspicarray[day] +
"'/>")}
// -->
</script>


Question (1)
How do I do it so that it is random??

Question (2)
How do i make it so that [day] can be used in other functions (currently i
use the same 'var' sequence in each function
 
P

petermichaux

How do I do it so that it is random??

It will be something like the following untested code.


<script type='text/javascript'>

function adspic() {
var adspicarray=["ads/images/kilt.jpg",
"ads/images/kilt.jpg",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jpg",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jpg",
"ads/images/apronandcase.jpg"];

document.write("<p align='right'><img src = '" +
adspicarray[Math.floor(7*Math.random())] +"'/>");
}

</script>

Don't bother with the language attribute it is deprecated. Use they
type attribute instead. Also the <!----> is not necessary anymore
apparently. Also don't forget semi-colons at the end of your
statements. Also you can use array literal notation instead of new
Array(). Try putting your orignial code in http://www.jslint.com for
suggestions on improving syntax.

Peter
 
J

JimK

I am using the following for a different picture to show up every day....

<script language="JavaScript">
<!--
function adspic() {

var mydate=new Date()
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var adspicarray=new Array("ads/images/kilt.jpg",
"ads/images/kilt.jpg",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jpg",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jpg",
"ads/images/apronandcase.jpg")

document.write("<p align='right'><img src = '" + adspicarray[day] +
"'/>")}
// -->
</script>


Question (1)
How do I do it so that it is random??

Question (2)
How do i make it so that [day] can be used in other functions (currently i
use the same 'var' sequence in each function


var day = Math.round(Math.random() * 5)


day is variable of the day of the week (0 = Sunday - 6 = Saturday).
 
J

JimK

I am using the following for a different picture to show up every day....

<script language="JavaScript">
<!--
function adspic() {

var mydate=new Date()
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var adspicarray=new Array("ads/images/kilt.jpg",
"ads/images/kilt.jpg",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jpg",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jpg",
"ads/images/apronandcase.jpg")

document.write("<p align='right'><img src = '" + adspicarray[day] +
"'/>")}
// -->
</script>


Question (1)
How do I do it so that it is random??

Question (2)
How do i make it so that [day] can be used in other functions (currently i
use the same 'var' sequence in each function


var day = Math.round(Math.random() * 6) // 6 images


day is variable of the day of the week (0 = Sunday - 6 = Saturday).
 
P

petermichaux

JimK said:
var day = Math.round(Math.random() * 5)


day is variable of the day of the week (0 = Sunday - 6 = Saturday).

With this random number generation you will never generate a 6. Also 0
and 5 will occur with half the frequency of 1,2,3,4.

Peter
 
P

petermichaux

JimK said:
var day = Math.round(Math.random() * 6) // 6 images


day is variable of the day of the week (0 = Sunday - 6 = Saturday).

Now pictures 0 and 6 will appear with half the frequency of days
1,2,3,4,5

(0-0.5) --> 0
[0.5-1.5) --> 1
[1.5-2.5) --> 2
[2.5-3.5) --> 3
[3.5-4.5) --> 4
[4.5-5.5) --> 5
[5.5-6) --> 6
 
J

JimK

With this random number generation you will never generate a 6. Also 0
and 5 will occur with half the frequency of 1,2,3,4.

Peter

Your right, took me a minute to figure out why. I was thinking about
using Math.floor. I mis-counted the images in the array which I
corrected
 
R

Randy Webb

(e-mail address removed) said the following on 5/27/2006 1:52 AM:
How do I do it so that it is random??

It will be something like the following untested code.


<script type='text/javascript'>

function adspic() {
var adspicarray=["ads/images/kilt.jpg",
"ads/images/kilt.jpg",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jpg",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jpg",
"ads/images/apronandcase.jpg"];

document.write("<p align='right'><img src = '" +
adspicarray[Math.floor(7*Math.random())] +"'/>");
adspicarray[Math.floor(adspicarray.length*Math.random())]

Also don't forget semi-colons at the end of your statements.

Unless you know where they make a difference, you are generally better
off without trailing semicolons than with them. They are optional.
 
R

Randy Webb

JimK said the following on 5/27/2006 2:44 AM:
Your right, took me a minute to figure out why. I was thinking about
using Math.floor. I mis-counted the images in the array which I
corrected

Instead of hard coding the images, use the arrayName.length property and
then all it takes to add images is another entry into the array.
 
P

Prophet

So now how do I build the Var in so that it can be used in multiple
functions as I currently type the same code into each individual function
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Sat, 27 May 2006 05:59:06 remote, seen in
JimK said:
var day = Math.round(Math.random() * 5)


Reading the newsgroup FAQ *before* giving "advice" may help you to give
an illusion of competence.

R = Math.random()*5|0

<FAQENTRY>
The FAQ has AFAICS no example of any of the single-character logical
operators; one could be placed in 4.22.
 
J

JimK

JRS: In article <[email protected]>, dated
Sat, 27 May 2006 05:59:06 remote, seen in


Reading the newsgroup FAQ *before* giving "advice" may help you to give
an illusion of competence.

R = Math.random()*5|0


I like it, would have never thought of that method. Randy has already
pointed out the problem using Math.round(Math.random() * 6


?? for you. Far as I know JS has no way to work with binary numbers
like 00000000b directly, is this correct?
 
L

Lasse Reichstein Nielsen

JimK said:
?? for you. Far as I know JS has no way to work with binary numbers
like 00000000b directly, is this correct?

There is no way to specify that a number literal should be read as
an integer, and not a floating point number.
However, some operations implicitly convert their arguments to
a 32-bit integer. These are mainly the bit-operations: bitwise and,
bitwise or, bitwise xor, and the shifts.

/L
 
V

VK

Lasse said:
There is no way to specify that a number literal should be read as
an integer, and not a floating point number.
However, some operations implicitly convert their arguments to
a 32-bit integer. These are mainly the bit-operations: bitwise and,
bitwise or, bitwise xor, and the shifts.

Would it lead to implications if used in the reverse order?
0|510

In such case it could be used as a pseudo-allocator to use together
with say 0x1FE and 0776
 
E

Evertjan.

JimK wrote on 29 mei 2006 in comp.lang.javascript:
John Stockton:

I like it, would have never thought of that method. Randy has already
pointed out the problem using Math.round(Math.random() * 6

I like it too, but it is EATING my <br>s:

try:

<script type='text/javascript'>
document.write( Math.random()*5|0 + '<br>' );
document.write( Math.random()*5|0 + '<br>' );
document.write( Math.random()*5|0 + '<br>' );
document.write( Math.random()*5|0 + '<br>' );
document.write( Math.random()*5|0 + '<br>' );
document.write( Math.random()*5|0 + '<br>' );
document.write( Math.random()*5|0 + '<br>' );
</script>
 
L

Lasse Reichstein Nielsen

VK said:
Would it lead to implications if used in the reverse order?
0|510

Bitwise or is symmetric. It converts both operands to 32-bit integers
before operating on them. Then the result is converted back to a
64-bit floating point number (although an optimizing implementation
might choose to keep it as an integer until "floatness" is needed)
In such case it could be used as a pseudo-allocator to use together
with say 0x1FE and 0776

Not understood. Both of these number literals represent values of the
number type, i.e., floating point numbers.

/L
 
L

Lasse Reichstein Nielsen

Evertjan. said:
I like it too, but it is EATING my <br>s:

try:

<script type='text/javascript'>
document.write( Math.random()*5|0 + '<br>' );

The + operator has higher precedence than the bitwise or operator,
so this expression is equivalent to:
(Math.random() * 5) | (0 + '<br>')
The second operand of "|" is the string "0<br>", which converts
to the number NaN, which converts to the 32-bit integer 0.

So, that's what's eating your "<br>".
/L
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Mon, 29
May 2006 09:02:50 remote, seen in Evertjan.
JimK wrote on 29 mei 2006 in comp.lang.javascript:


I like it too, but it is EATING my <br>s:

try:

<script type='text/javascript'>
document.write( Math.random()*5|0 + '<br>' );
document.write( Math.random()*5|0 + '<br>' );
document.write( Math.random()*5|0 + '<br>' );
document.write( Math.random()*5|0 + '<br>' );
document.write( Math.random()*5|0 + '<br>' );
document.write( Math.random()*5|0 + '<br>' );
document.write( Math.random()*5|0 + '<br>' );
</script>

Whenever an expression (such as the RHS of my "R =") is placed within
another expression, it should be put in parentheses unless those
parentheses are known not to be needed. Example :-

R = 1 + 2
S = "3 = " + R // OK

S = "3 = " + 1 + 2 // not OK
S = "3 = " + (1 + 2) // OK


Bitwise OR is of low precedence, so that most other operators are
executed before it, and therefore B-OR prefers to eat well-chewed
operands.

R = 0|Math.random()*5

is probably better, since 5|0 looks like 510. Fortunately, there seems
to be no binary ! operator nor unary | .
 
E

Evertjan.

Dr John Stockton wrote on 29 mei 2006 in comp.lang.javascript:
Whenever an expression (such as the RHS of my "R =") is placed within
another expression, it should be put in parentheses unless those
parentheses are known not to be needed.

I love those quircks in a computer language,
like this "eating of the <br>s".

It makse them more alive and more like a real language.

However, the real culprit here is having "+" as a string concatenation
operator.

Having the "+" as "add" having precedence over "|" stands to logic,
but "+" as "concatenate" should not have that precedence, me seems.

Since that cannot be, let us at least enjoy it.

==========

When God said to the beasts "go and multiply",
one pair came up to him and complained:
"We cannot multiply, we are only adders"

And then they said:
"And we want unparenthesed precedence over those slimy concatenators too"

In the end the lonely unary "+" was given top precedence to compensate
for it's total lack of both multiplication and adding power.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Tue, 30
May 2006 08:00:21 remote, seen in Evertjan.
I love those quircks in a computer language,
like this "eating of the <br>s".

It makse them more alive and more like a real language.

However, the real culprit here is having "+" as a string concatenation
operator.

Nothing wrong with that; the culprit is having "+" accept non-matching
operands.

Binary + should accept only two strings or two numbers.
Unary + should accept anything, and convert it to Number.
Unary - should give the negative of unary +.
There should be a new unary operator (for brevity), to accept anything
and convert to String.
Except, of course, that it is too late now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top