By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,955 Members | 1,239 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,955 IT Pros & Developers. It's quick & easy.

New Perspectives on Javascript: Tutorial 2, Case 2

P: 4
I am very new to Javascript and would like some advise for the following...

a. Declare a variable named rNumber = to the value returned from the randInt() function using 5 as the parameter value.

b. Declare a variable named rAd = to the text string returned from the adDesrciption() function using rNumber as the parameter value.

c. Declare a variable named rLink = to the URL returned from the adLink() function using rNumber as the parameter value.
Nov 8 '08 #1
Share this Question
Share on Google+
9 Replies


gits
Expert Mod 5K+
P: 5,387
what have you done so far? how does the mentioned functions look like?

kind regards
Nov 8 '08 #2

P: 4
I am working in <div id="ads"> section.

[HTML]<html>
<head>
<!--
New Perspectives on JavaScript
Tutorial 1
Case Problem 2

The Ridgewood Herald Tribune
Author: Michael Shepard
Date: 11/06/2008

Filename: front.htm
Supporting files: ads1.jpg - ads5.jpg, ads.js, fp.jpg, front.htm, logo.jpg, random.js,
styles.css
-->
<title>The Ridgewood Herald Tribune</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="random.js"></script>
<script type="text/javascript" src="ads.js"></script>
<script type="text/javascript">
</script>

</head>

<body>

<div id="links">
<h1>Contents</h1>
<p class="section">Main</p>
<p class="linksub">
<a href="#">Home</a><br />
<a href="#">Subscriptions</a><br />
<a href="#">Contact Us</a><br />
<a href="#">News Sources</a><br /><br />
</p>
<p class="section">News</p>
<p class="linksub">
<a href="#">Local</a><br />
<a href="#">National</a><br />
<a href="#">International</a><br />
</p>

<p class="section">Sports</p>
<p class="linksub">
<a href="#">Baseball</a><br />
<a href="#">Basketball</a><br />
<a href="#">Football</a><br />
<a href="#">Golf</a><br />
<a href="#">Hockey</a><br />
<a href="#">Miscellaneous</a><br />
</p>

<p class="section">Opinion</p>
<p class="linksub">
<a href="#">Editorials</a><br />
<a href="#">Columnists</a><br />
<a href="#">Letters</a><br />
</p>

<p class="section">Classifieds</p>
<p class="linksub">
<a href="#">Employment</a><br />
<a href="#">For Sale</a><br />
<a href="#">Personals</a><br />
<a href="#">Real Estate</a><br />
<a href="#">Wanted</a><br />
</p>

<p class="section">Other</p>
<p class="linksub">
<a href="#">Business</a><br />
<a href="#">Weather</a><br />
<a href="#">Entertainment</a>
</p>
</div>
<div id="main">
<div id="ads">
<script type="text/javascript">
var rNumber = randInt();
var rAd = adDescription();
var rLink = adLink();
document.write(<a href="URL">);
document.write('<img src="adn.jpg" alt="description">');
document.write</a>
</script>
</div>

<div id="request"><a href="#">Contact us today to place your ad</a></div>

<p id="logo">
<img src="logo.jpg" alt="Ridgewood Herald Tribune" />
</p>

<p id="fp">
<img src="fp.jpg" alt="" />
</p>

<h2>Park Opens</h2>
<p id="intro">The <i>Adventure Island</i> theme park opened its doors on
Monday near Ridgewood. The park, one of the biggest in the Midwest, drew
large crowds, but the long lines didn't deter anyone. "I've been watching
them put up the rides over the last year, it's really exciting to finally
get inside the gates!" said Ridgewood resident, Denise Brooks.</p>

<p class="cont"><a href="#">story continues on page 2...</a></p>

<address id="footer">
<b>Ridgewood Herald Tribune</b> &nbsp;&nbsp; 10010 Atwood Ave.
&nbsp;&nbsp; Ridgewood, Iowa &nbsp; &nbsp; 80135<br />
Phone: (606)555-1101 &nbsp;&nbsp; Fax: (606)555-1102
</address>
</div>

</body>
</html>
[/HTML]
THis is an external js.

New Perspectives on JavaScript
Tutorial 3
Case Problem 2

Function List:
randInt
Used to return a random integer from 1 to 'n'

*/


Expand|Select|Wrap|Line Numbers
  1. function randInt(n) {
  2.    randNum = Math.ceil(Math.random()*n);
  3.    return randNum;
  4. }
  5.  
THis is the other js.

Expand|Select|Wrap|Line Numbers
  1. function adDescription(n) {
  2.    var descrip = new Array();
  3.    descrip[1]="[AD] Diamond Health Club - For all your Health Club Needs";
  4.    descrip[2]="[AD] Pixal - Quality Digital Equipment and Accessories";
  5.    descrip[3]="[AD] dHome - Quality Geodesic Domes and Homes";
  6.    descrip[4]="[AD] Dunston Retreat Center - get away";
  7.    descrip[5]="[AD] LanGear - Quality Network Solutions for all your Business Needs";
  8.  
  9.    return descrip[n];
  10. }
  11.  
  12. function adLink(n) {
  13.    var link = new Array();
  14.    link[1]="http://www.diamondhealth.com";
  15.    link[2]="http://www.pixalproducts.com";
  16.    link[3]="http://www.dhome.com";
  17.    link[4]="http://www.dunstonretreats.com";
  18.    link[5]="http://wwww.langearproducts.com";
  19.  
  20.    return link[n];
  21. }
  22.  
Nov 9 '08 #3

gits
Expert Mod 5K+
P: 5,387
basicly your calls there should look like the following:

Expand|Select|Wrap|Line Numbers
  1. var rNumber = randInt(5);
  2. var rAd = adDescription(rNumber);
  3. var rLink = adLink(rNumber);
  4.  
  5. alert('<a href="' + rLink + '">' + rAd + '</a>');
  6.  
before using the functions you should have a close look at it and see what parameters you need to pass and what the functions return.

kind regards
Nov 9 '08 #4

P: 4
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <!-- 
  4.    New Perspectives on JavaScript
  5.    Tutorial 4
  6.    Case Problem 2
  7.  
  8.    The Home Center
  9.    Author: Michael     
  10.    Date:   11/10/2008
  11.  
  12.    Filename:         home.htm
  13.    Supporting files: logo.jpg, random.js, styles.css, tips.js, work.jpg
  14.  
  15. IN THIS FILE SCROLL DOWN TO THE <div> ELEMENT WITH AN ID VALUE OF "tip"
  16. REPLACE THE CONTENTS OF THIS ELEMENT WITH AN EMBEDDED SCRIPT ELEMENT.
  17. WITHIN THE SCRIPT ELEMENT DO THE FOLLOWING: A). DECLARE A VARIABLE NAMED tipNum = TO A RANDOM INTEGER 
  18. BETWEEN 1 AND 10 RETURNED BY THE randInt() FUNCTION CREATED IN THE random.js file.
  19. ALSO, USE A SERIES OF document.write() methods TO WRITE THE FOLLOWING html CODE TO THE WEB PAGE:
  20. <h1>Random Tip<br> />title</h1>
  21. <p>tip</p>
  22. Where title is the title of the random title as generated by the tipTitle() function and
  23. tip is the text of the random tip as generated by the tipText() function.   
  24.  
  25. -->
  26. <title>The Home Center</title>
  27. <link href="styles.css" rel="stylesheet" type="text/css" />
  28. <script type="text/javascript" src="tips.js"></script>
  29. <script type="text/javascript" src="random.js"></script>
  30. </head>
  31.  
  32. <body>
  33.  
  34. <div id="searchbox">
  35.    Search <input size="20" />
  36. </div>
  37.  
  38. <div id="links">
  39.   <img src="logo.jpg" alt="the Home Center" />
  40.   <ul>
  41.      <li><a href="#">Home</a></li>
  42.      <li class="new"><a href="#">What's New</a></li>
  43.      <li><a href="#">Home Projects</a></li>
  44.      <li><a href="#">E-Newsletter</a></li>
  45.      <li><a href="#">FAQ</a></li>
  46.      <li class="new"><a href="#">Online Store</a></li>
  47.      <li><a href="#">Product Search</a></li>
  48.      <li class="new"><a href="#">Forums</a></li>
  49.      <li><a href="#">Our Partners</a></li>
  50.      <li><a href="#">Contact Us!</a></li>
  51.      <li><a href="#">About the Home Center</a></li>
  52.    </ul>
  53. </div>
  54.  
  55. <div id="main">
  56.    <div id="tip">
  57.  
  58.       <h1>Random Tip<br />
  59.       Tip Title
  60.       </h1>
  61.       Place random tip text here in this box.
  62.    </div>
  63.  
  64.    <p id="firstp"><b>The Home Center</b> is your number one source for do it yourself home 
  65.    improvement help with tutorials and tips on all your home repairs, remodeling, 
  66.    and redecorating. Our goal is to make your projects easy, inexpensive, and 
  67.    rewarding. Nothing matches the satisfaction of a job well done.</p>
  68.    <p>You will find step-by-step instruction and money-saving tips on all your 
  69.    do-it-yourself projects including house painting, wallpaper, carpentry, home 
  70.    insulation, woodworking, electrical, plumbing, air conditioning and heating, 
  71.    flooring, masonry, concrete, wood decks, interior decorating, gardening, 
  72.    how to save energy, reduce your utility bills, and more.</p>
  73.    <p>Be sure to visit our online store for great prices on the tools and 
  74.    materials you need for your home repair projects!</p>
  75.  
  76. </div>
  77.  
  78. </body>
  79. </html>
  80.  
  81. /*
  82.    New Perspectives on JavaScript
  83.    Tutorial 2
  84.    Case Problem 2
  85.  
  86.    Author: Michael 
  87.    Date:   11/10/2008
  88.  
  89.  
  90.  
  91. */
  92. //create a function named randInt(). The purpose of this function is to generate a random integer within a given range. 
  93. //THe function has two parameters: lower and upper, where lower is the lowest integer in the range
  94. //and upper is the highest in the range.
  95. //Add the following commands to the function
  96. //a.  Declare a variable named size = to the number of integers in the given range.
  97. //(hint: the size of the range is one more that the differnce between the highest and lowest integer.
  98. //b. Use the Math.floor and Math.random methods as well as the lower parameter and size variable to generate a random integer.
  99.  
  100.  
  101. function randInt()
  102.  
  103. var size
  104.  
  105. /*
  106.    New Perspectives on JavaScript
  107.    Tutorial 2
  108.    Case Problem 2
  109.  
  110.    Function List:
  111.    tipTitle(n)
  112.       Used to return the title for tip "n"
  113.  
  114.    tipText(n)
  115.       Used to return the text of tip "n"
  116. */
  117.  
  118.  
  119. function tipTitle(n) {
  120.    var title = new Array();
  121.    title[1] = "Damaged Tiles";   
  122.    title[2] = "Sanding Wood"; 
  123.    title[3] = "Dealing with Ice";
  124.    title[4] = "Weed Killers";
  125.    title[5] = "Effective Mowing";
  126.    title[6] = "Lighting the Outdoors";
  127.    title[7] = "Using a Sump Pump";
  128.    title[8] = "Sagging Gutters";
  129.    title[9] = "Long-lasting Brushes";
  130.    title[10] = "Using Downspouts";
  131.    return title[n];
  132. }
  133.  
  134. function tipText(n) {
  135.    var text = new Array();
  136.    text[1] = "To replace a tile, blast a hair dryer at full force onto the ragged " +
  137.              "edge of the tile that's chipped. Once it heats up, press a large wood " +
  138.              "chisel into that edge to lift away the tile, then use the chisel or a " +
  139.              "putty knife to remove any remaining adhesive.";
  140.    text[2] = "Before you stain wood, first sand it down to a silky smoothness, using " +
  141.              "successively finer grades of sandpaper. Once it's smoooth, soak a rag " +
  142.              "in turpentine and rub it into the wood. As the turpentine dries, the wood's " +
  143.              "grain will rise. Sand this down with super-fine grit, making sure never to " +
  144.              "sand against the grain. You're now ready to stain.";
  145.    text[3] = "Not sure how to take care of an icy driveway? Rock salt clears the ice, " +
  146.              "but it can kill nearby vegetation and damage the concrete below. Luckily, " +
  147.              "there's now a whole handful of environmentally-friendly products that melt " +
  148.              "the ice without ruining your lawn or sidewalk. You can also go the " +
  149.              "old-fashioned route and spread ash, sand or gravel on the ice to provide " +
  150.              "better traction.";
  151.    text[4] = "To control your weeds you may need " +
  152.              "to use chemical weed killers. Don't spread a weed killer on a newly " +
  153.              "seeded lawn. That may do more harm than good. If you spray weed killer with " +
  154.              "a garden-hose attachment, wait for a calm day with little or no wind to prevent " +
  155.              "it from spreading to adjoining flower patches and harming them. Finally, spray " +
  156.              "between mowings so that the chemical can sink into the weeds.";
  157.    text[5] = "Don't mow your grass when it's wet; that damages the blades and your lawn alike. "+
  158.              "Don't buzz cut an overgrown lawn to get back on schedule. Instead, cut the grass " +
  159.              "back in increments, never by more than a third of its current length. However, do cut " +
  160.              "your lawn as high as about three inches. That length helps limit weed growth, reduces " +
  161.              "the need for watering and promotes a strong root structures. Don't cut your lawn " +
  162.              "obsessively.";
  163.    text[6] = "Use outdoor lights with a photocell unit or a timer so they will turn off during the day. " +
  164.              "Turn off decorative outdoor gas lamps; eight gas lamps burning year round use as much " +
  165.              "natural gas as it takes to heat an average-size home during an entire winter. Finally, " +
  166.              "if you live in a cold climate, be sure to buy a lamp with a cold-weather ballast.";
  167.    text[7] = "Dealing with basement flooding? Get a sump pump if you don't already have one. " +
  168.              "If you do have one, consider a second one for emergency situations. If your sump pump " +
  169.              "is electric, get a battery-operated version as a backup since power outages often " +
  170.              "accompany heavy storms. Be sure to check your pump for problems. First, disconnect " +
  171.              "the pump and clean out any debris; then reconnect it and pour in enough water for a " +
  172.              "test.";
  173.    text[8] = "If your gutters aren't draining quickly and drip long after the rain, water " +
  174.              "may be collecting in a low spot caused by sagging. If that's the case, grab a friend " +
  175.              "and snap a chalk line along the gutter using both ends of its top edge as your guiding " +
  176.              "points. (You should have a slope of 1 inch for every 16 feet of gutter). Look for spots " +
  177.              "where the gutter's edge falls below the chalk line, then refasten the spikes, straps " +
  178.              "or brackets that hold it in place.";
  179.    text[9] = "Excessive soaking is the number one menace to the lifespan of a paintbrush. Instead of " +
  180.              "soaking, clean your brush immediately according to the directions. When using oil base " +
  181.              "paints, clean thoroughly in solvent, kerosene or thinner, then comb and place back in " +
  182.              "the keeper.";
  183.    text[10]= "To effectively get the water away from your house you can let your downspout " +
  184.              "empty onto a splash block or send the runoff to " +
  185.              "an extension gutter. Or you can get a retractable downspout extender which " +
  186.              "unfurls as it fills with water, then contract as the water stops flowing, " +
  187.              "tucking neatly under the downspout and out of the way of lawn mowers.";
  188.    return text[n];
  189. }
Nov 10 '08 #5

P: 4
I need to generate a new ad everytime the window is reloaded. What am I suppose to do to the section <div id="ads"> to make this work correctly. Please look at what I have there now. Thanks



I am working in <div id="ads"> section.
[HTML]<html>
<head>
<!--
New Perspectives on JavaScript
Tutorial 1
Case Problem 2

The Ridgewood Herald Tribune
Author: Michael
Date: 11/06/2008

Filename: front.htm
Supporting files: ads1.jpg - ads5.jpg, ads.js, fp.jpg, front.htm, logo.jpg, random.js,
styles.css
-->
<title>The Ridgewood Herald Tribune</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="random.js"></script>
<script type="text/javascript" src="ads.js"></script>
<script type="text/javascript">
</script>

</head>

<body>

<div id="links">
<h1>Contents</h1>
<p class="section">Main</p>
<p class="linksub">
<a href="#">Home</a><br />
<a href="#">Subscriptions</a><br />
<a href="#">Contact Us</a><br />
<a href="#">News Sources</a><br /><br />
</p>
<p class="section">News</p>
<p class="linksub">
<a href="#">Local</a><br />
<a href="#">National</a><br />
<a href="#">International</a><br />
</p>

<p class="section">Sports</p>
<p class="linksub">
<a href="#">Baseball</a><br />
<a href="#">Basketball</a><br />
<a href="#">Football</a><br />
<a href="#">Golf</a><br />
<a href="#">Hockey</a><br />
<a href="#">Miscellaneous</a><br />
</p>

<p class="section">Opinion</p>
<p class="linksub">
<a href="#">Editorials</a><br />
<a href="#">Columnists</a><br />
<a href="#">Letters</a><br />
</p>

<p class="section">Classifieds</p>
<p class="linksub">
<a href="#">Employment</a><br />
<a href="#">For Sale</a><br />
<a href="#">Personals</a><br />
<a href="#">Real Estate</a><br />
<a href="#">Wanted</a><br />
</p>

<p class="section">Other</p>
<p class="linksub">
<a href="#">Business</a><br />
<a href="#">Weather</a><br />
<a href="#">Entertainment</a>
</p>
</div>
<div id="main">
<div id="ads">
var rNumber = randInt(5); //gernerate a random integer from 1 to 5
var rAd = adDescription(rNumber); //description of the random ad
var rLink = adLink(rNumber); //url of the random ad
document.write('<a href="+rLink+" "+rAd+">');
document.write(rAd,rLink);
document.write("</a>") </div>

<div id="request"><a href="#">Contact us today to place your ad</a></div>

<p id="logo">
<img src="logo.jpg" alt="Ridgewood Herald Tribune" />
</p>

<p id="fp">
<img src="fp.jpg" alt="" />
</p>

<h2>Park Opens</h2>
<p id="intro">The <i>Adventure Island</i> theme park opened its doors on
Monday near Ridgewood. The park, one of the biggest in the Midwest, drew
large crowds, but the long lines didn't deter anyone. "I've been watching
them put up the rides over the last year, it's really exciting to finally
get inside the gates!" said Ridgewood resident, Denise Brooks.</p>

<p class="cont"><a href="#">story continues on page 2...</a></p>

<address id="footer">
<b>Ridgewood Herald Tribune</b> &nbsp;&nbsp; 10010 Atwood Ave.
&nbsp;&nbsp; Ridgewood, Iowa &nbsp; &nbsp; 80135<br />
Phone: (606)555-1101 &nbsp;&nbsp; Fax: (606)555-1102
</address>
</div>

</body>
</html>
[/HTML]
THis is an external js.

New Perspectives on JavaScript
Tutorial 3
Case Problem 2

Function List:
randInt
Used to return a random integer from 1 to 'n'

*/


Expand|Select|Wrap|Line Numbers
  1. function randInt(n) {
  2.    randNum = Math.ceil(Math.random()*n);
  3.    return randNum;
  4. }
  5.  
THis is the other js.

Expand|Select|Wrap|Line Numbers
  1. function adDescription(n) {
  2.    var descrip = new Array();
  3.    descrip[1]="[AD] Diamond Health Club - For all your Health Club Needs";
  4.    descrip[2]="[AD] Pixal - Quality Digital Equipment and Accessories";
  5.    descrip[3]="[AD] dHome - Quality Geodesic Domes and Homes";
  6.    descrip[4]="[AD] Dunston Retreat Center - get away";
  7.    descrip[5]="[AD] LanGear - Quality Network Solutions for all your Business Needs";
  8.  
  9.    return descrip[n];
  10. }
  11.  
  12. function adLink(n) {
  13.    var link = new Array();
  14.    link[1]="http://www.diamondhealth.com";
  15.    link[2]="http://www.pixalproducts.com";
  16.    link[3]="http://www.dhome.com";
  17.    link[4]="http://www.dunstonretreats.com";
  18.    link[5]="http://wwww.langearproducts.com";
  19.  
  20.    return link[n];
  21. }
  22.  
Nov 10 '08 #6

gits
Expert Mod 5K+
P: 5,387
lines 81 and 82 seems to be strange ... are the links supposed to work that way? :)
Nov 10 '08 #7

Dormilich
Expert Mod 5K+
P: 8,639
the quotation marks mess up line 81.

personally, I'd use DOM for that.
Nov 11 '08 #8

acoder
Expert Mod 15k+
P: 16,027
Never mind that (well, OK, it is important, but not as important as this), what's JavaScript doing in the HTML without being in script tags?!
Nov 11 '08 #9

acoder
Expert Mod 15k+
P: 16,027
mshepard, please do not double post your questions. I've merged the threads. That second thread had one post with code only and no explanation of what the problem was. It doesn't work like that. You need to show what you've done and the problem you're having getting it to work. Also remember to use code tags when posting code. Thanks.

Moderator.
Nov 11 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.