/    Sign up×
Community /Pin to ProfileBookmark

Completely new to javascript, making random quotes

Hi there, new member, and completely new to javascript. I’m trying to finish this assignment for a class where I need to place random quotes into a webpage using JS and things are just completely wrong and I don’t know how to get it working.

The assignment comes from a book which has me do things in a specific way, so changing the whole script to work differently is out of the question. Can’t change the names of the variables/etc either.

There’s two files for the assignment – random.js , and twain.htm .

random.js

[code]
function randInt (size) {
var rNum = Math.ceil(Math.random()*5);
return rNum;
}
[/code]

I’m sure that the file is just the mechanism for making the quotes random. I put the number 5 there, because there’s supposed to be 5 quotes in total. Probably wrong to do that.

twain.htm

[code]
<script src=”random.js” type=”text/javascript”></script>
<script type=”text/javascript”>
function getQuote (qNum){
var mtQuotes = new Array ();
mtQuotes[0] = “”;
mtQuotes[1] = “a”;
mtQuotes[2] = “b”;
mtQuotes[3] = “c”;
mtQuotes[4] = “d”;
mtQuotes[5] = “e”;
return mtQuotes;
}
</script>
[/code]

The part of twain.htm where it should display the random quote:

[code]
<div id=”quotes”>
<script type=”text/javascript”>
var randValue = randInt()+5;
var quoteText = getQuote();
document.write(“[quoteText]”);
</script>
</div>
[/code]

Well…all I get is [quoteText] showing up in the text box where the quotes should be. From what I’ve seen in the book, the [] should be displaying the variable differently.

So, any way I could get this to work? Any help is greatly appreciated.

Again, I’m completely new to javascript, I’ve only read what I have in my book and that’s all I know about it.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@astupidnameNov 20.2008 — var rNum = Math.ceil(Math.random()*5); [B]change that to:[/B] var rNum = Math.ceil(Math.random()*size);

var randValue = randInt()+5; [B]change that to:[/B] var randValue = randInt(6);

document.write(""); [B]change that to:[/B] document.write(quoteText[randValue-1]);
Copy linkTweet thisAlerts:
@grandcrossauthorNov 20.2008 — That worked great, thank you!
×

Success!

Help @grandcross spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...