Click to See Complete Forum and Search --> : Math???
huskrfreak88
02-28-2004, 01:06 AM
Ok what i want to do is have 7 Prompt boxes come up and have the user specified data. then it needs to apply a formula to the inputed numbers. then just have an alert pop up with the results of the formula... hard part is i have a bi-conditional statement within! Please help with all of it... just a basic form i can fill in the formula! Thanks a TON!
The start is like this... i just need math help haven't learned js math!
<SCRIPT LANGUAGE="JavaScript">
{
{
var attack = prompt("Attack?", "");
var defense = prompt("Defense?", "");
var strength = prompt("Strength?", "");
var hits = prompt("Hits?", "");
var ranged = prompt("Ranged?", "");
var prayer = prompt("Prayer?", "");
var magic = prompt("Magic?", "");
return (((attack+defense+strength+hits)/4))+((prayer+magic)/8))
}
}
</script>
hmm why doesn't it work???
Pittimann
02-28-2004, 01:34 AM
Hi!
Do you really think it is a good idea to have 7 prompts pop up?? I would definately run away from that site, after realizing, that the first prompt is not the only one! Why not do that with text inputs in a form?
As far as maths is concerned: what do you need - cosinus of value 2 * pi / value 1 + value 3 blah blah... Please specify, what you want. It seems, you not only want to sum up all these values.
Cheers - Pit
PeOfEo
02-28-2004, 01:35 AM
return (((attack+defense+strength+hits)/4)+((prayer+magic)/8));
looks like you had a bracket issue too.
Pittimann
02-28-2004, 01:38 AM
Hi!
Just saw your edit and PeOfEo's reply.
The stuff entered into a prompt is a string and not a number. Using attack+defense will concatenate the values instead of adding them.
You should use something like Number(attack)+...
Cheers - Pit
fredmv
02-28-2004, 07:33 AM
Moreover, note that having a return statement doesn't make any sense (and is technically invalid) unless inside of a function.
steelersfan88
02-28-2004, 09:29 AM
instead of using return, place it into a form objext or write it into the document
or, give the user another thing to worry about and use an alert box (if you want to send your visitors away, i recommend this one, 8 alerts/prompts!)
huskrfreak88
02-28-2004, 10:13 AM
alright i see what you mean! umm in that case i am TOTALLY helpless!!! anyone willing to post a script that would work??? here is what i want... (where A-G is inputed info)
IF (E*1.5>A+B) THEN (((E*1.5)+B+D)/4)+((F+G)/8)
Else((A+B+C+D)/4)+((F+G)/8)
THANKS IF THAT MAKES SENSE PLEASE HELP!! I'M JS ILLITERATE!
steelersfan88
02-28-2004, 10:18 AM
start by changing:
var attack = prompt("Attack?", "");
var defense = prompt("Defense?", "");
var strength = prompt("Strength?", "");
var hits = prompt("Hits?", "");
var ranged = prompt("Ranged?", "");
var prayer = prompt("Prayer?", "");
var magic = prompt("Magic?", "");
to
var attack = parseFloat(prompt("Attack?", ""));
var defense = parseFloat(prompt("Defense?", ""));
var strength = parseFloat(prompt("Strength?", ""));
var hits = parseFloat(prompt("Hits?", ""));
var ranged = parseFloat(prompt("Ranged?", ""));
var prayer = parseFloat(prompt("Prayer?", ""));
var magic = parseFloat(prompt("Magic?", ""));
I would continue, but the style of code you displayed is very poor.
If .. Else statements should look more like:
if (condition) {
//lines of code
}
else {
//lines of code
}Your if then statement has no {}s and only changes line one time, it is very hard to see what the condition is that must be true for the code under the if block to run.
I think we need a sticky thread about programming style. It seems that people get more lazy about their programming that its starting to make us volunteers confused! (MODERATORS!)
Pittimann
02-28-2004, 11:04 AM
Hi!anyone willing to post a script that would work???Hope, your last post means, you could live without prompts :D. If so:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var result;
var checkArr=new Array();
var errorArr=new Array();
var count;
function calc(frm){
count=-1;
var1=parseFloat(frm.fieldA.value);
var2=parseFloat(frm.fieldB.value);
var3=parseFloat(frm.fieldC.value);
var4=parseFloat(frm.fieldD.value);
var5=parseFloat(frm.fieldE.value);
var6=parseFloat(frm.fieldF.value);
var7=parseFloat(frm.fieldG.value);
for (var i=0;i<7;i++){
eval('checkArr['+i+']=var'+(i+1)+';');
if (isNaN(checkArr[i])){
count++;
errorArr[count]=' '+(i+1);
}
}
if(count==-1){
if (var5*1.5>var1+var2) result=(var5*1.5+var2+var4)/4+(var6+var7)/8;
else result=(var1+var2+var3+var4)/4+(var6+var7)/8;
alert(result);
}
else{
alert('Wrong (non-numeric) input in field(s)'+errorArr+'!');
}
}
//-->
</script>
</head>
<body>
<form>
A: <input type="text" name="fieldA" value=3><br>
B: <input type="text" name="fieldB" value=5><br>
C: <input type="text" name="fieldC" value=3><br>
D: <input type="text" name="fieldD" value=4><br>
E: <input type="text" name="fieldE" value=5><br>
F: <input type="text" name="fieldF" value=6><br>
G: <input type="text" name="fieldG" value=7><br>
<input type="button" onClick="calc(this.form)" value="view"><br>
</body>
</html>
Cheers - Pit
steelersfan88
02-28-2004, 11:05 AM
and if not, just change the
frm.fieldA.value
back to your prompt dialogs, thats all!
huskrfreak88
02-29-2004, 02:07 AM
THANKS!!!!!
Pittimann
02-29-2004, 02:28 AM
Hi!
Just one more thing:
If you use such code with prompts (no - I will not try to prevent you from using them!!!:D), the user would have to go through all seven prompts again, if only 1 entry is invalid. Due to the fact, that this is not implemented in the code I posted, nothing would happen.
Using form fields enables the user to correct invalid entries...
My code used with prompts would need some while statement to show each of the prompts again whenever the entry is invalid.
Cheers - Pit
huskrfreak88
02-29-2004, 01:50 PM
how would you make something that when you enter a number such as 57 it searches a data base and returns a different number... like this below you enter the left number and it uses the right number in a calculation
Level Experience
2 83
3 174
4 276
5 388
6 512
7 650
8 801
9 969
10 1154
11 1358
12 1584
13 1833
14 2107
15 2411
16 2746
17 3115
18 3523
19 3973
20 4470
21 5018
22 5624
23 6291
24 7028
25 7842
26 8740
27 9730
28 10824
29 12031
30 13363
31 14833
32 16456
33 18247
34 20224
35 22406
36 24815
37 27473
38 30408
39 33648
40 37224
41 41171
42 45529
43 50339
44 55649
45 61512
46 67983
47 75127
48 83014
49 91721
50 101333
51 111945
52 123660
53 136594
54 150872
55 166636
56 184040
57 203254
58 224466
59 247886
60 273742
61 302288
62 333804
63 368599
64 407015
65 449428
66 496254
67 547953
68 605032
69 668051
70 737627
71 814445
72 899257
73 992895
74 1096278
75 1210421
76 1336443
77 1475581
78 1629200
79 1798808
80 1986068
81 2192818
82 2421087
83 2673114
84 2951373
85 3258594
86 3597792
87 3972294
88 4385776
89 4842295
90 5346332
91 5902831
92 6517253
93 7195629
94 7944614
95 8771558
96 9684577
97 10692629
98 11805606
99 13034431
Pittimann
02-29-2004, 01:57 PM
Hi!
Server side! Sorry: javascript is not the right language for what you want.
Cheers - Pit
steelersfan88
02-29-2004, 02:02 PM
Pittiman is absolutely right (sound familar, haha). If you want javascript, put them all in a huge array and maybe store it in an external js file:function checkVal(theNum) {
theNum = parseInt(theNum)
var success = 0
var vals = new Array(2, 83, 3, 174, ..)
for(var i = 0;i<vals.length;i+=2) {
if(theNum == vals[i]) {
success = 1
i += 1
return vals[i];
i -= 1
break;
}
}
if(success == 0) {
return 0; // no value has returned
}
}
var newNum = checkVal(2) // var newNum = 83
what? you don't need server side for that... i think husker was confused, since what he has there is an array, not a database.
PeOfEo
02-29-2004, 02:36 PM
Originally posted by samij586
what? you don't need server side for that... i think husker was confused, since what he has there is an array, not a database. No, java script might be able to do it but server side would be a better alternative here (talking about an array, obviously if you used a data base it would be server side). When you are using this much data it could lag up the site for the client if you do this with client side scripting. The server can execute this code much faster and much more efficiently then the client could.
steelersfan88
02-29-2004, 02:47 PM
it would helpful i think if we talked to him, but it seems that if he aint talkin ,we cant help :-(
huskrfreak88
02-29-2004, 02:56 PM
sorry for not talking! I wasn't home... Ok well basically what i want is like a calculator, where they enter the level they are at, the level they want to be at, and then it calculates the difference in experiance and tells them what they need to do to get the remaining experiance... SO! what i basically want to do is this: User types Current Level: 15 Desired Level: 20
Then the computer retrieves the experiance points for those levels, finds the difference and then divides it by 'X' (doesn't really matter what that is for explination purposes)
Possible?
PeOfEo
02-29-2004, 03:04 PM
Yes, it is possible. It can also be done with java script. I myself prefer to do all functional scripting server side, but whatever.
huskrfreak88
02-29-2004, 03:16 PM
i don't know what server side is.. sorry!
PeOfEo
02-29-2004, 03:27 PM
Originally posted by huskrfreak88
i don't know what server side is.. sorry! :p. Server side: the script is executed by the server and the server will output the html that the users browser reads. ASP ASP.NET PHP CGI JSP CF are all server side languages. .SHTML means the page is using a server side include (this is when the server parses the contents of one file into the file with the include on it).