4 Replies - 1731 Views - Last Post: 28 March 2007 - 07:30 PM

#1 scooby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 07

array to get quotes

Posted 28 March 2007 - 06:07 PM

<script src="random.js" type="text/javascript">
function getQuote(qNum)
{
 var mtQuotes = new Array(5);

 mtQuotes[0] = "";
 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>

<div id="quotes">
   <script type="text/javascript"> 
   var randValue = randInt(5);
   var quoteText = getQuote(qNum);  
   document.write(quoteText);
   </script>
   </div>


I need to know if my return statement will actually return the quote? I am not sure how to these arrays work. We had to create a function getQuote with qNum as the parameter. then we had to declare mtQuotes as a new array with the size of five and put in the quotes in the numbers 1-5. I am not sure it will return the quote
I try it and it does not when i call the getQuote(qNum); function. I have to use the getQuote function in the div id="quotes" but it tells me qNum is undefined. so they are not printing randomly

I really would appreciate some help
Scooby
function randInt(size)
{ 
 var rNum = Math.ceil(Math.random()*5);
 return rNum;
}

here is the random function called randInt

[mod edit] Use the code tags. Because Mr. T pity the foo who doesn't use code tags.

This post has been edited by hotsnoj: 28 March 2007 - 07:18 PM


Is This A Good Question/Topic? 0
  • +

Replies To: array to get quotes

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: array to get quotes

Posted 28 March 2007 - 06:39 PM

They are not printing randomly because you are not generating a random number, at least not anywhere shown. Can you show an example of where you call the function?
Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 250
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: array to get quotes

Posted 28 March 2007 - 06:43 PM

var quoteText = getQuote(qNum); 


should be
var quoteText = getQuote(randValue); 


You need to actually pass the random value you generated.
Was This Post Helpful? 0
  • +
  • -

#4 scooby  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 23-February 07

Re: array to get quotes

Posted 28 March 2007 - 06:46 PM

View PostAmadeus, on 28 Mar, 2007 - 06:43 PM, said:

var quoteText = getQuote(qNum); 


should be
var quoteText = getQuote(randValue); 


You need to actually pass the random value you generated.



It is telling me i need an object and giving me an error with that. This is what i thought but kept getting this error.
Was This Post Helpful? 0
  • +
  • -

#5 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 93
  • View blog
  • Posts: 3,583
  • Joined: 31-March 03

Re: array to get quotes

Posted 28 March 2007 - 07:30 PM

Do you know that randValue is a number?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1