randInt(1, 10) {
function getrandomInt (1, 10) {
return Math.floor(Math.random() * (10 - 1 + 1)) + 1;
help with random number generation
Page 1 of 110 Replies - 535 Views - Last Post: 07 February 2012 - 10:28 AM
Topic Sponsor:
#1
help with random number generation
Posted 01 February 2012 - 07:32 PM
cant seem to get random number generation function working properly. Any help is greatly appreciated!
Replies To: help with random number generation
#2
Re: help with random number generation
Posted 01 February 2012 - 07:43 PM
Since it's your first post you should know that just saying you can't get it to work properly isn't enough. I'm trying to figure out whether you're trying to create a function or create a function within an object.
You should learn the basics of a function first. A parameter isn't passed by hardcoding the value directly. Defeats the purpose of a function and I don't think it's valid Javascript. Here's an example of a function
Start there and revise your code to see where you went wrong.
You should learn the basics of a function first. A parameter isn't passed by hardcoding the value directly. Defeats the purpose of a function and I don't think it's valid Javascript. Here's an example of a function
function example(x, y) {
return (x * y);
}
alert(example(3, 4)); //example of a function call
Start there and revise your code to see where you went wrong.
This post has been edited by codeprada: 01 February 2012 - 07:44 PM
#3
Re: help with random number generation
Posted 01 February 2012 - 07:50 PM
the given code is somewhat incomplete. if you have it as such, it should quit with a syntax error.
despite that the logic is correct.
despite that the logic is correct.
#4
Re: help with random number generation
Posted 01 February 2012 - 07:57 PM
I'm a beginner completely to javascript and this is just an ex exercise for a chapter in my text.
javascript
html
What I have to do is create a randomInt() function that generates a number 1-10 for a tip and I use that function to display said tip. I'm not asking you to do this for me, just for help. I'm not sure if i have the syntax correct or not. My professor's lectures are pretty poor and so is the textbook.
javascript
/*
New Perspectives on Javascript, 2nd Edition
Tutorial 2
Case Problem 2
Author:
Date:
Function List:
randInt(lower, upper)
Used to generate a random integer in the range (lower, upper)
*/
var size = 10;
randInt(1, 10) {
var size = 10;
var randInteger = randInt(1, 10);
var randomnumber=Math.floor(Math.random()*11);
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
html
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!--
New Perspectives on Javascript, 2nd Edition
Tutorial 2
Case Problem 2
The Home Center
Author: Will Griffin
Date: 2/1/12
Filename: home.htm
Supporting files: logo.jpg, random.js, styles.css, tips.js, work.jpg
-->
<title>The Home Center</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="tips.js"></script>
<script type="text/javascript" src="randtxt.js"></script>
</head>
<body>
<div id="searchbox">
Search <input size="20" />
</div>
<div id="links">
<img src="logo.jpg" alt="the Home Center" />
<ul>
<li><a href="#">Home</a></li>
<li class="new"><a href="#">What's New</a></li>
<li><a href="#">Home Projects</a></li>
<li><a href="#">E-Newsletter</a></li>
<li><a href="#">FAQ</a></li>
<li class="new"><a href="#">Online Store</a></li>
<li><a href="#">Product Search</a></li>
<li class="new"><a href="#">Forums</a></li>
<li><a href="#">Our Partners</a></li>
<li><a href="#">Contact Us!</a></li>
<li><a href="#">About the Home Center</a></li>
</ul>
</div>
<div id="main">
<div id="tip">
<script type=" text/javascript">
var tipNum = randInt</script> </div>
<p id="firstp"><b>The Home Center</b> is your number one source for
do-it-yourself home improvement help with tutorials and tips on all your home
repairs, remodeling, and redecorating. Our goal is to make your projects easy,
inexpensive, and rewarding. Nothing matches the satisfaction of a job well done.</p>
<p>You will find step-by-step instructions and money-saving tips on all your
do-it-yourself projects, including house painting, wallpaper, carpentry, home
insulation, woodworking, electrical, plumbing, air conditioning and heating,
flooring, masonry, concrete, wood decks, interior decorating, gardening,
and the installation of energy-efficient fixtures and appliances</p>
<p>Be sure to visit our online store for great prices on the tools and
materials you need for your home repair projects!</p>
</div>
</body>
</html>
What I have to do is create a randomInt() function that generates a number 1-10 for a tip and I use that function to display said tip. I'm not asking you to do this for me, just for help. I'm not sure if i have the syntax correct or not. My professor's lectures are pretty poor and so is the textbook.
#5
Re: help with random number generation
Posted 01 February 2012 - 08:05 PM
Since randomInt is suppose to be a function you must declare it as one. getRandomInt was done correctly. randomInt is missing the keyword function as well as a closing brace. I'm not understanding why getRandomInt was created when it isn't called anywhere. You also have the variable size just there unused.
#6
Re: help with random number generation
Posted 01 February 2012 - 08:35 PM
This is what i gathered from what you suggested.
And these are the errors i keep getting from the error console when i try and run the webpage.
Error: missing formal parameter
Source File: file:///F:/Data%20Files%20for%20Text/Data%20Files%20for%20Text/Tutorial.02/case2/randtxt.js
Line: 17, Column: 17
Source Code:
function randInt(1, 10); {
Error: randInt is not defined
Source File: file:///F:/Data%20Files%20for%20Text/Data%20Files%20for%20Text/Tutorial.02/case2/hometxt.htm
Line: 54
I know i am at a great disadvantage working with this code. I greatly appreciate your help and your patience
function randInt(1, 10); {
var randInteger = randInt(1, 10);
var randomnumber=Math.floor(Math.random()*11); }
And these are the errors i keep getting from the error console when i try and run the webpage.
Error: missing formal parameter
Source File: file:///F:/Data%20Files%20for%20Text/Data%20Files%20for%20Text/Tutorial.02/case2/randtxt.js
Line: 17, Column: 17
Source Code:
function randInt(1, 10); {
Error: randInt is not defined
Source File: file:///F:/Data%20Files%20for%20Text/Data%20Files%20for%20Text/Tutorial.02/case2/hometxt.htm
Line: 54
I know i am at a great disadvantage working with this code. I greatly appreciate your help and your patience
#7
Re: help with random number generation
Posted 01 February 2012 - 08:58 PM
as codeprada already said, a function definition expects parameter names to be given, not numbers.
besides that, the task is already giving you the starting point (line #10).
besides that, the task is already giving you the starting point (line #10).
#8
Re: help with random number generation
Posted 01 February 2012 - 08:59 PM
- Your parameters should be a variable and not a value.
- Remove the semi-colon before your opening brace. A semi-colon terminates a line of code.
Another thing to note is unless a function is recursive you shouldn't call it within itself. I'm assuming you were suppose to call getRandomInt instead of randomInt here
var randInteger = randInt(1, 10);
#9
Re: help with random number generation
Posted 01 February 2012 - 09:14 PM
getRandomInt() is neither required by the task, nor do I see a necessity for it.
#10
Re: help with random number generation
Posted 05 February 2012 - 08:34 PM
To be a more general purpose function, you should consider this change...
<html>
<head>
<title> Untitled </title>
<script type="text/javascript">
// if trying to get a random number from a range
// you should change to this for a more general solution
function getrandomInt (s,e) { // generate # between s and e
return Math.floor(Math.random() * (e-s+1)) + s;
}
alert(getrandomInt(1,10));
alert(getrandomInt(100,200));
alert(getrandomInt(1000,1200));
</script>
<style type="text/css">
</style>
</head>
<body>
</body>
</html>
#11
Re: help with random number generation
Posted 07 February 2012 - 10:28 AM
Sorry i haven't been on in the past few days, but i just wanted to thank you guys for all of your help.From what i have looked up about this book i am using most people seem to agree that the code is outdated and trash, but its homework so i just have to do it lol. Thanks again!
Page 1 of 1
|
|

New Topic/Question
Reply


MultiQuote




|