473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

'var' question

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
May 27 '06 #1
15 1437
> 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

May 27 '06 #2
JimK wrote:

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

May 27 '06 #3
JimK wrote:
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

May 27 '06 #4
pe**********@gmail.com 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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 27 '06 #5
JimK said the following on 5/27/2006 2:44 AM:
On 26 May 2006 23:06:59 -0700, pe**********@gmail.com wrote:
JimK wrote:
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


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.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 27 '06 #6
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

"Prophet" <M_*****@hotmail.com> wrote in message
news:qdRdg.1057$kK6.1032@edtnps89...
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

May 27 '06 #7
JRS: In article <h9********************************@4ax.com>, dated
Sat, 27 May 2006 05:59:06 remote, seen in news:comp.lang.javascript,
JimK <1a****@gmail.com> posted :


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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
May 28 '06 #8
JimK <1a****@gmail.com> writes:
?? 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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
May 29 '06 #9
VK

Lasse Reichstein Nielsen wrote:
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

May 29 '06 #10
JimK wrote on 29 mei 2006 in comp.lang.javascript:
John Stockton:
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


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>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 29 '06 #11
"VK" <sc**********@yahoo.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
May 29 '06 #12
"Evertjan." <ex**************@interxnl.net> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
May 29 '06 #13
JRS: In article <Xn********************@194.109.133.242>, dated Mon, 29
May 2006 09:02:50 remote, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.net> posted :
JimK wrote on 29 mei 2006 in comp.lang.javascript:
John Stockton:
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


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 | .

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
May 29 '06 #14
Dr John Stockton 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.


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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 30 '06 #15
JRS: In article <Xn********************@194.109.133.242>, dated Tue, 30
May 2006 08:00:21 remote, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.net> posted :
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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
May 30 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: raver2046 | last post by:
hello, How to have a global var in python ? thank you. raver2046 http://raver2046.ath.cx
2
by: rlo | last post by:
use the next script: var answers0 = new Array( (answer11), (answer12), (answer13), (answer14) ); question= new geefVraag( 0, 'question1', oke1-1, answers0); feedback= new Array(
6
by: Maja | last post by:
Greetings, /var/adm/wtmp files were getting out of hand and I wanted to filter the file rather than zeroing it out. I found some code that would only keep the last x days but it required...
35
by: Thierry Loiseau | last post by:
Hello all, and Happy end year 2005 ! Well, I would like to obtain a list of all JavaScript var statement, With "for...in" perharps ? That is bellow my recent test here, but the problem is...
3
by: overdalsveien | last post by:
hi This is probably elementary, but since my 'Dear Watson' is not present I hope you folks can help me:: --------------------------- var dag = date1.getDate();...
6
by: vlsidesign | last post by:
If I have a "for" loop like this: for (i = 0; i < 10; i++) or like this for (i = 0; i < 10; ++i) both of the "for" loops will function the same. I also noticed if I use a variable called j...
13
by: Rainy | last post by:
I have a stylistic question. In most languages words in var. name are separated by underscores or cap letters, resulting in var names like var_name, VarName and varName. I don't like that very much...
13
by: DL | last post by:
For instance, function addNew(i) { var 'element'&i ... } If possible, how to? Thanks.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.