Click to See Complete Forum and Search --> : generating random numbers
scragar
06-17-2003, 06:21 AM
I wish to generate a random number between 1 and 20 then have javascript check if it is over ten then Should it be so to disply its interger(whole number not decimals) and if not come up with an alert saying it is not 10 or more and send me back to my last page (I can do the alert part myself I just need help in verifing the random number and generating it)
Thanks for any help!
The site should be good when it's finished.
P.S. Anyone help me with placing several confimation boxes where each one determines the next one's question?
:confused: :confused: :confused: :confused: :confused:
Gollum
06-17-2003, 06:25 AM
use Math.random() and Math.ceil()...
var nRandom = Math.ceil(Math.random() * 20);
will generate a whole number from 1 to 20
scragar
06-17-2003, 06:31 AM
That is very nice however It does not help me weight the numbers myself or have js identifie the number in the alert (that happens the be the main problem!?)
:confused: :confused: :confused: :confused: :confused:
SlankenOgen
06-17-2003, 06:33 AM
P.S. Anyone help me with placing several confimation boxes where each one determines the next one's question?
post exact details please.
~mgb
Gollum
06-17-2003, 06:40 AM
It does not help me weight the numbers myself
How do you want to weight them?
or have js identifie the number in the alert
just put the number into the alert...
var nRandom = Math.ceil(Math.random() * 20);
alert('The number is ' + nRandom);
scragar
06-17-2003, 07:35 AM
The site is supposed to semi converse I dentifieng different topics and responding with the apropriate responce such as:
What do you want to talk about?
the respond with football,
Js then generates its number, if it's 2 js responds with something like who is your faveourite player?
I want it like this (although I'm not sure its possible) to make each "conversation" unique!
SlankenOgen
06-17-2003, 07:43 AM
The javascript is easy, but if you want to generate a whole conversation it can get very unwieldly. If you make the questions arrays.
var Qs = new Array("what is your favourite book","who is your favourite player",...etc);
Now, if the answer is 1 window.alert(Qs[1]);
Get the idea?
~mgb
scragar
06-17-2003, 07:49 AM
Not realy any chance you could describe it in more detail?
I am not very skilled in javascript and untill the start of may I thought it was some foreign language!!!
Gollum
06-17-2003, 07:53 AM
wow, ambitious! let us know how you get on...
SlankenOgen
06-17-2003, 10:06 AM
If you are not very experienced you should put it on hold for a month or two and get to know javascript basics. It's an easy language to learn and you should be doing magic with it in a short while if you apply yoursef!!!
Best of luck
~mgb
scragar
06-18-2003, 04:26 AM
I know a fair bit about JS, however no-one offers any good tutorials!
Any Idea's for a good one?!
SlankenOgen
06-18-2003, 05:19 AM
Just do a search for Javascript tutorials.
There's thousands of them out there.
Best of luck.
~mgb
scragar
06-18-2003, 05:30 AM
OK so I will put that on hold, By the way anyone know how to get an "IF" function to work, whenever I try they allways appaer in text not as prompts or alerts?!
P.S. can JS be used to perfom simple sums? If so how/ what do you enter?
AdamGundry
06-18-2003, 05:38 AM
anyone know how to get an "IF" function to workDo you mean a normal if statement, like this?
if (condition) {
command; // executed if condition is true
command;
} else {
command; // executed if false
}
can JS be used to perfom simple sums?Yes, there are various things you can do. Most simply use the normal operators, such as addition:
a = 10;
b = 15;
c = a + b; // c is now 25
Check out the manual on operators (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/ops.html) and the Math object (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/math.html).
Adam
scragar
06-18-2003, 06:19 AM
Hey two finals Question (I hope!!)
1) Can Js search a variable fo a particular term?
I.e.
<script>{
phrase==Prompt ("enter a phrase","")
Then I wish to check the phrase to see if it contains a term such as cat, HOw do I get JS to search for it (I would prefere to have the variables easily defined in any responce)
2) how about the script for a function after a set time from loading?
Thanks for any help!!!
:confused: :confused: :confused: :confused: :confused: :confused:
AdamGundry
06-18-2003, 06:48 AM
1. There are a variety of methods you can use, but probably the easiest is to use String.search() (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/string.html#1194332). Here is an example:
phrase = prompt("Enter a phrase?");
word = "cat";
if (phrase.search(/ + word + /gi) != -1){
alert("The phrase you entered contained the word cat!");
}
2. You can use setTimeout() (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/window.html#1203758), like this:
<body onload="setTimeout('myfunction()', 1000)">
<script type="text/javascript">
function myfunction(){
alert("This message appears 1 second (1000ms) after the document loads."):
}
</script>
Adam
SlankenOgen
06-18-2003, 06:59 AM
var stringToSearch = "xxxHellozzzzzzzzzz";
function testForMatch()
{
stringToSearch = stringToSearch.toLowerCase();
if(stringToSearch.match("hello"))
{
window.alert("Found: hello");
}
}
}
This will do a case insensitive search.
~mgb
scragar
06-18-2003, 07:22 AM
I think this computer is stuffed, It refuses too display any alert boxes from any site but still displays comand prompts and confirm boxes?
Any way my site refuses to let my place if functions!!! Why? anyone help with the new problem (I think it's proberlem number 9 but I am not sure)
Thanks again for any help now or in the future (if I get proberlem number 10!!!!!!)
:confused: :confused: :confused: :confused: :confused: :confused:
AdamGundry
06-18-2003, 02:27 PM
Can you please post a link to the problem page, so we can see if your syntax is right.
BTW, IF is not a function it is a statement. See this manual page: http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/stmt.html#1004833
Adam
scragar
06-19-2003, 04:46 AM
It's OK I discopvered the problem, I asked JS to find: Comment on instead of comment, I forgot about capitals !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I apoligise profoundly for any problems I may have caused!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SlankenOgen
06-19-2003, 05:18 AM
Ah well...most errors are of a very simple nature like typing mistakes and simple syntax errors. You'd be surprised how much time I have used looking for complex logical mistakes which did not exist only to find that the error was very simple in the end.
My debugging goes something like-
1. Check for typing errors.
2. Check for typing errors.
3. Check for typing errors.!
4. Check for syntax errors.
5. Check for logical errors.
6. Check for compatibility problems.
7. Check for typing errors!!!
~mgb