PDA

View Full Version : A few javascript bugs that I am trying to find...



Petoux
28th November 2006, 05:30 PM
First...I will give the code, and then I will state the problem I am trying to fix and find.

In this website, I have two other files embedded in my sight.

This first one is a regular text file which is an external source of the javascript:




function randInt(size){
var rNum = Math.ceil(Math.random()*1);
return rNum;
}



This next one is a css file that is an external source of a few added things, not sure if it relates to javascript, but here is the code anyway:




body {background-image: url(paper.gif); color: black}
#leftcol {float: left; margin-right: 10px; width: 220px}
#main {float: left; width: 400px; padding: 5px}
#quotes {text-align: center; background-color: white; color: black;
border: 3px outset white; font-family: sans-serif; padding: 2px;
font-size: 10pt}
li {list-style-image: url(button.gif); padding-left: 5px}
li a {font-size: 8pt; font-family: sans-serif}



The final part is my website HTML code, which is the main one I have been having troubles with the javascript, and I think I have an idea how to fix it, but I'm not sure how to do it.

Here is the code...everything bolded is what I am having trouble with, and then they are referenced seperately below:




<title>The Mark Twain Quotes Page</title>
<link href="mtstyles.css" rel="stylesheet" type="text/css" />
<script src="random.js.js" type="text/javascript" />

<script type="text/javascript">

function getQuote(qNum){
var mtQuotes=new Array(5);
mtQuotes[1]="I smoke in moderation, only one cigar at a time.";
mtQuotes[2]="Be careful of reading health books, you might die of a misprint.";
mtQuotes[3]="Man is the only animal that blushes or needs to.";
mtQuotes[4]="Clothes make the man. Naked people have little or no influence on society.";
mtQuotes[5]="One of the most striking differences between a cat and a lie is that a cat has only nine lives.";
return mtQuotes[qNum];
}

</script>

</head>

<body>

<div id="leftcol">
<img src="twain.gif" alt="Mark Twain" />
<br />
<div id="quotes">

<script type="text/javascript">

function{
var randValue = Math.ceil(Math.random()*5);
var quoteText = getQuote(qNum);
return();
/* document.write("quoteText"); */
}

</script>

</div>

etc...



Refered singularly:




<script src="random.js.js" type="text/javascript" />



The above statement has a basic syntax of <script type="mime-type">script commands and comments</script>

But is it random.js or random.js.js?

Also, this is an external file and the syntax looks right to me.

...




<script type="text/javascript">

function getQuote(qNum){
var mtQuotes=new Array(5);
mtQuotes[1]="I smoke in moderation, only one cigar at a time.";
mtQuotes[2]="Be careful of reading health books, you might die of a misprint.";
mtQuotes[3]="Man is the only animal that blushes or needs to.";
mtQuotes[4]="Clothes make the man. Naked people have little or no influence on society.";
mtQuotes[5]="One of the most striking differences between a cat and a lie is that a cat has only nine lives.";
return mtQuotes[qNum];
}

</script>



This function is supposed to create an array named mtQuotes that contains five quotes each with an assigned index which I have just fine...it seems.

It is supposed to return the value of mtQuotes array for ghe qNum index.

The syntax looks correct, so I'm not sure what's wrong.

...




<script type="text/javascript">

function{
var randValue = Math.ceil(Math.random()*5);
var quoteText = getQuote(qNum);
return();
/* document.write("quoteText"); */
}

</script>



This script is also an embedded type, and it has the right <script></script> code.

The problem with this one is that the function doesn't have a name, and I'm not sure what to name it.

The function is supposed to declare a variable called "randValue" which is supposed to pick a random integer between 1 and 5 with rounding up the number. That is done, so I don't think anything is wrong with that one.

Yet here is the main problem I am trying to solve:

I want to decare a variable named "quoteText" {Which I have} which contains the Mark Twain quote whose array index value is equal to randValue.

I'm not sure how to do that, but my best quess is somehow use the getQuote() function I created to extract the correct quote from the array. Yet I'm not sure how to do it.

Can you help me? Is my syntax wrong? Is their sometimg I'm missing? Anything?

Thanks...

:D

Carnage
29th November 2006, 08:38 AM
<title>The Mark Twain Quotes Page</title>
<link href="mtstyles.css" rel="stylesheet" type="text/css" />
i removed the external js file as from your posted content you dont need it
<script type="text/javascript">

var mtQuotes=new Array(5);
mtQuotes[1]="I smoke in moderation, only one cigar at a time.";
mtQuotes[2]="Be careful of reading health books, you might die of a misprint.";
mtQuotes[3]="Man is the only animal that blushes or needs to.";
mtQuotes[4]="Clothes make the man. Naked people have little or no influence on society.";
mtQuotes[5]="One of the most striking differences between a cat and a lie is that a cat has only nine lives.";

i changed this into straight forward code; dosn't seem much point to me having it as a function.

</script>

</head>

<body>

<div id="leftcol">
<img src="twain.gif" alt="Mark Twain" />
<br />
<div id="quotes">

<script type="text/javascript">

var randValue = Math.ceil(Math.random()*5);
var quoteText = mtQuotes[randValue];
document.write(quoteText);
again i dont see why this needs to be a function; its only called once. You will notice i changed "quoteText" to quoteText, this is because you want the value of the variable quoteText not the litral value "quoteText"

secondally, i removed the need for the funtion get quote with a direct referance to the array containing the quotes.
</script>

</div>

etc...



some corrections above; not sure if it'll work but the code is much cleaner now.

Petoux
29th November 2006, 10:12 AM
It moved the text where I didn't want it to, but I forgot a </div> comment, so I added that in, and it moved it back.

The stuff you supplied me with fixed the problem, thanks Carnage!!

:D

Oh, why arn't those functions anyway? I'm not QUITE sure why it shouldn't be, could you specify a little bit more?

Thanks again.

^.^

Carnage
29th November 2006, 11:21 AM
functions are great when you want to use something over and over again (or even just twice) but in your case you are only using it once so using functions just makes the code more complex.