/    Sign up×
Community /Pin to ProfileBookmark

newbie question => calculation

hi there ,

if i wanted to make a script that when i input 45 on a textfield and then clickt ok , then would calculate 45*1500*3-250 and post it in a new text field

how would such script look like

sorry to bother

just trying to learn

thanks in advance

to post a comment
JavaScript

18 Comments(s)

Copy linkTweet thisAlerts:
@pyroMar 16.2004 — Try something like this:

<script type="text/javascript">
/*<![CDATA[*/
function cal(frm) {
input = frm.ammount.value;
val = input*1500*3-250;
frm.total.value = val;
}
/*]]>*/
</script>
</head>
<body>
<form action="" onsubmit="cal(this); return false;">
<p>Input: <input type="text" name="ammount" /><br />
Total: <input type="text" name="total" /><br />
<input type="submit" name="calculate" /></p>
</form>
Copy linkTweet thisAlerts:
@keuneleuteauthorMar 16.2004 — hey , thanks for the quick response

i'am sorry but i'am mistaken the prog should do this

i'am getting the answer and when fill in the answer it should calculate like this :


The calculation is: answer = (number * 3 + 2) - 250

Example with number 1500:

4252 = (1500 * 3 + 2) - 250


sorry for all the trouble i tried adjusting it but it didn't work

thanks in advance agian
Copy linkTweet thisAlerts:
@pyroMar 16.2004 — Post your updated code, so we can help you learn, by showing you what you did wrong. ?
Copy linkTweet thisAlerts:
@keuneleuteauthorMar 16.2004 — <script type="text/javascript">

/*<![CDATA[*/

function cal(frm) {

input = frm.ammount.value;

val = input+250-2/3;

frm.total.value = val;

}

/*]]>*/

</script>

the problem i'am getting is when i enter 4252 for example

it gets me 4252249.333333333

like when i change the script to +250

it shows me 4525250

and i have no clue why , if i use - then it works , neither works with /
Copy linkTweet thisAlerts:
@steelersfan88Mar 16.2004 — you are receiving a string, not a number. 2 ways to solve. Either subtract -250, or (recommended) use parseFloat(frm...) to receive val.

or define the var as a new integer or something oike that. Plus - 2/3 is just subtracting the decimal .666666... not sure if thats what you want.
Copy linkTweet thisAlerts:
@keuneleuteauthorMar 16.2004 — i'am understood what you are trying to say

but this is my first javascript

and if i cant use -2/3 how do i solve this problem then ?.

also how do i make it calculate -250 and then the -2/3 ?

i mean i kno you posted it but cant execute it becouse i dont have the knowledge todo so ....
Copy linkTweet thisAlerts:
@fredmvMar 16.2004 — [i]Originally posted by steelersfan88 [/i]

[B]you are receiving a string[/B][/QUOTE]
Completely incorrect. When JavaScript encounters a variable in which is a string then manipulated somehow via any math operator other than addition (due to it also serving as a string concatenation operator as well), the variable gets autocasted to a number data type.

Anyway, here's the solution:&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt;
&lt;head&gt;
&lt;title&gt;untitled&lt;/title&gt;
&lt;meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" /&gt;
&lt;script type="text/javascript"&gt;
//&lt;![CDATA{
function calcValue()
{
var f = document.forms[0];
f[2].value = f[0].value*1500*3-250;
return false;
}
//]]&gt;
&lt;/script&gt;
&lt;style type="text/css"&gt;
/*&lt;![CDATA[*/
input {
padding: 5px;
}
/*]]&gt;*/
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action="#"&gt;
&lt;div&gt;
&lt;input type="text" /&gt;
&lt;input type="button" value="-&amp;gt;" onclick="calcValue();" /&gt;
&lt;input type="text" /&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@keuneleuteauthorMar 16.2004 — thanks for the reply aswell

but i'am sorry that aint the solution neither ,

it is suppose to calculate the given number in a way like this

+250-2/3

so plus 250

minus 2

devide by 3

you made 1500*3-250 which was wrong i posted that one wrong

pfff this seems to be more complicated then i tought :p

anyone ? lol
Copy linkTweet thisAlerts:
@fredmvMar 16.2004 — &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt;
&lt;head&gt;
&lt;title&gt;untitled&lt;/title&gt;
&lt;meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" /&gt;
&lt;script type="text/javascript"&gt;
//&lt;![CDATA{
function calcValue()
{
var f = document.forms[0];
f[2].value = parseInt(f[0].value+250-2/3, 10);
return false;
}
//]]&gt;
&lt;/script&gt;
&lt;style type="text/css"&gt;
/*&lt;![CDATA[*/
input {
padding: 5px;
}
/*]]&gt;*/
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action="#"&gt;
&lt;div&gt;
&lt;input type="text" /&gt;
&lt;input type="button" value="-&amp;gt;" onclick="calcValue();" /&gt;
&lt;input type="text" /&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@keuneleuteauthorMar 16.2004 — *sigh* blows brain out , why cry's hard

thanks for the update when executing your script


4252 become's 4252249


i'am geting a number. (answer)

in this case 4252

The calculation is: answer = (number * 3 + 2) - 250

Example with number 1500:

4252 = (1500 * 3 + 2) - 250

In this example 4252 is the answer

what i want to achieve is , i want to make a script that doe's the reverse thing

so 4252 become's 4252+250-2/3 = 1500

its 1500 i was looking for in this case

i tried your updated method but as you can read a few post above that method aint working why not i dont kno

i'am just a beginner and this is my first script why , becouse i need this lol

i dont kno why it it is a problem to produce it but by the looks of it it is a great challenge for you ppl aswell :p

and yes i can use a calculater but i need it fast = under 3 seconds
Copy linkTweet thisAlerts:
@keuneleuteauthorMar 16.2004 — anyone that can help ?
Copy linkTweet thisAlerts:
@steelersfan88Mar 17.2004 — to fredmv, sorry to rain on your parade, but all programming languages do this. When retrieving data from any field, a string is retrieved. It kind of looks as if you are agreeing with me in that it is a string until it finds an operator other than addition ... i kind of pointed that out, saying you could subtract a negative number for the same affect of adding, rather than concatening, to a string containing a number.

I also said that you could use parseFloat, which would do its job as well, converting it into a floating point decimal.

Now which part of that is completely incorrect, besides you?

EDIT: I checked out my third option as well:[CODE]var aNumber = new Number
alert(aNumber + 22)[/CODE]
And that worked fine, rather than an alert of NaN, you get 22, and therefore it will retrieve a number.

I even built an example for you:[CODE]var b = "22"
var a = new Number(b)
alert(a + 22)[/CODE]
The string 22 of the var b is assigned as a number, then when adding 22, the result is 44. steelersfan88++; fredmv--
Copy linkTweet thisAlerts:
@fredmvMar 17.2004 — [i]Originally posted by steelersfan88[/i]

[B]to fredmv, sorry to rain on your parade, but all programming languages do this. When retrieving data from any field, a string is retrieved. It kind of looks as if you are agreeing with me in that it is a string until it finds an operator other than addition ... i kind of pointed that out, saying you could subtract a negative number for the same affect of adding, rather than concatening, to a string containing a number. [/B][/QUOTE]
Sorry for the confusion. What it comes down to is the fact that the data is autocasted to a number data type if and only if some kind of math operator (other than addition) is used. Alternatively, but not recommended, you could explictly cast the data to an alternate data type but this makes no sense when JavaScript can simply autocast it for you.[i]Originally posted by steelersfan88[/i]

[B]The string 22 of the var b is assigned as a number, then when adding 22, the result is 44.[/B][/QUOTE]
You are incredibly confused. Before you make potentially false accusations please know what you're talking about first. I was referring to having two [b]strings[/b], not a string and an explict number variable created via the [font=courier]Number[/font] constructor. For instance:"5" + "5";Would yeild the string "55" since they are concatenated as opposed to added. However:("5" - 0) + 5;Is ten because it gets autocasted to a number data type by subtracting zero from it, which has been my point all along until you twisted things around.
Copy linkTweet thisAlerts:
@keuneleuteauthorMar 17.2004 — so is it possible then

?
Copy linkTweet thisAlerts:
@steelersfan88Mar 17.2004 — but as in the example provided, javascript requires a user to autocast it manually, since javascript will look to concatenate the result to 2222, rather than 44, as the example wil result. And the problem is addition. Of course, other operations would not require anything of the sort, as it will do it for you.

But a note to all students, other languages won't! Few teachers will allow you to operate with strings as numbers, they MUST be converted. For example, Visual Basic will allow you to add strings since its concatenation character is the &, but no other language will ... you should convert manually, you must in most high level languages!!!
Copy linkTweet thisAlerts:
@fredmvMar 17.2004 — Even though it is rather cool that JavaScript does that for you, it could perhaps introduce some bad habits however, when using JavaScript primarily it's a good feature nonetheless and just shows you another reason why JavaScript is more in the league of a functional language like LISP or Scheme as opposed to a language like C++ or Java.
Copy linkTweet thisAlerts:
@keuneleuteauthorMar 17.2004 — okay thanks guy's the was a very interesting

thread i learnd from it

and thank you
Copy linkTweet thisAlerts:
@keuneleuteauthorMar 17.2004 — mama mia its solved ?)))))))))

this is the solution

<script type="text/javascript">

/*<![CDATA[*/

function cal(frm) {

input = frm.ammount.value;

val = (input*1+250-2)/3;

frm.total.value = val;

}

/*
]]>*/

</script>

</head>

<body>

<form action="" onsubmit="cal(this); return false;">

<p>Input: <input type="text" name="ammount" /><br />

Total: <input type="text" name="total" /><br />

<input type="submit" name="calculate" /></p>

</form>


easy and simpel ?)))))))
×

Success!

Help @keuneleute 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.19,
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,
)...