473,799 Members | 3,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'var' question

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

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

var mydate=new Date()
var day=mydate.getD ay()
var month=mydate.ge tMonth()
var daym=mydate.get Date()
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.jp g",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jp g",
"ads/images/apronandcase.jp g")

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 1463
> 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.jp g",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jp g",
"ads/images/apronandcase.jp g"];

document.write( "<p align='right'>< img src = '" +
adspicarray[Math.floor(7*Ma th.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**********@gm ail.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.jp g",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jp g",
"ads/images/apronandcase.jp g"];

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


adspicarray[Math.floor(adsp icarray.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.javas cript 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**********@gm ail.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.lengt h property and
then all it takes to add images is another entry into the array.

--
Randy
comp.lang.javas cript 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_*****@hotmai l.com> wrote in message
news:qdRdg.1057 $kK6.1032@edtnp s89...
I am using the following for a different picture to show up every day....

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

var mydate=new Date()
var day=mydate.getD ay()
var month=mydate.ge tMonth()
var daym=mydate.get Date()
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.jp g",
"ads/images/kilt.jpg",
"ads/images/apronandcase.jp g",
"ads/images/apronandcase.jp g")

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.c om> 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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
May 28 '06 #8
JimK <1a****@gmail.c om> 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/rasterTriangleD OM.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

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

Similar topics

4
1799
by: raver2046 | last post by:
hello, How to have a global var in python ? thank you. raver2046 http://raver2046.ath.cx
2
1408
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
4164
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 creating a temporary file in /tmp and then copying it back to /var/adm. I rewrote the code to load the contents of the wtmp file into memory and then write the filtered records in memory directly back to /var/adm/wtmp, eliminating the need for a...
35
3628
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 to management theses :-((( I must to declare and use all variable with this scheme :
3
1329
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(); document.getElementById("selDay1").innerHTML = makeRequest('test.php?d=1'); -----------------------------------------------
6
2455
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 within the "for" loop body, and put these statement in the "for" loop body like this: printf("j var is: %d\n", ++j); printf("j var is: %d\n", j++); In the first statement, "j" increments first, and then prints "j"
13
1431
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 because all 3 ways of naming look bad and/or hard to type. From what I understand, scheme can have variables like var-name. I'm curious about reasons that python chose to disallow this. Another question I have is what other languages allow this...
13
1198
by: DL | last post by:
For instance, function addNew(i) { var 'element'&i ... } If possible, how to? Thanks.
0
10475
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10250
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10026
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.