...

View Full Version : how to parseInt and get total from if/else

07-20-2012, 11:39 PM
I seem to be befuddled on how to take a simple equation and get a total printed onto the alert
how do I get the total for this? do I need a return? a formula another function? Please help TY :D
Sample below:

function playnice(form) {

var cat =

if(cat<=25+1.50)

else(cat>25*10/100

<input type="text" name="kitty" parseFloat=" ????" value="">

Old Pedant
07-21-2012, 12:19 AM
You never define what total is.

And your else is utterly illegal.

And parseFloat as an <input> attribute doesn't make any sense at all.

Try showing *REAL* code. That code isn't doing anything at all.

07-21-2012, 07:10 PM
I need to get it to add and show it on an alert. I am new to javascript so please be patient. Thank you :)

<HTML>
<TITLE>Test Input</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function testResults (form) {

var amt = (myform.inputbox.value);
var total= (amt) += 1.50;
var s = (amt) *= 10 / 100;
if(amt<=25 + total) {
}
else if(amt>25+s){
}
return;
}
</SCRIPT>
<BODY>
<FORM NAME="myform" ACTION="" METHOD="GET">Enter the purchase price: <BR>
<INPUT TYPE="text" NAME="inputbox" parseInt="" VALUE=""><P>
<INPUT TYPE="button" NAME="button" Value="Click" onClick="testResults(this.form)">
</FORM>
</BODY>
</HTML>

Philip M
07-21-2012, 08:13 PM
I am sorry to say that there are many errors in your code. So many in fact that you ought to revise your understanding of this subject - as you say, you seem befuddled. I am not certain even that I have worked out what your calculations are supposed to do.

Here you are - please study it and learn. It is not really in your best interests that others do your all or most homework for you. Your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need. Also, if you hand in other people's work which you do not completely understand, then you will start to fall behind and your difficulties will increase. And naturally there is a limit to the number of times you can take your pitcher to this particular well.

<html>
<title>Test Input</title>

<script type = "text/javascript">

function testResults (form) {
var amt = form.inputbox.value;
var total = Number(amt) + 1.50; // Must convert input string to a number - add a flat 1.50
var s = amt * 10 / 100; // 10% of input amount
if (total <= 25) {
}
else { // if total is over 25
total += s; // add 10% of input amount
}
}
</script>
<body>
<form action="" >Enter the purchase price: <br>
<input type ="text" name="inputbox" value =""><br>
<input type ="button" name="button" value="Click" onClick="testResults(this.form)">
</form>
</body>
</html>

Note these days HTML tags should be in lower case as future web technologies require this. And the name attribute of <form> is deprecated.

There is of course no validation of the user input. In real life that would not be acceptable.

<script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9).

I don't see anything about parseInt() as mentioned in your thread title. But as it says on the tin, parseInt() returns an integer. You want Number().

“I don't pretend we have all the answers. But the questions are certainly worth thinking about..” - Arthur C. Clarke quotes (English Writer of science fiction, b.1917

07-22-2012, 07:19 AM
First I would like to start out saying that I do appreciate your help, but I also know that having someone else do my homework is not in my best interest. I find that, according to what you are saying about so much being depreciated, I am being taught wrong. This is very disturbing to me. I will study what you have sent but I will not submit it as my homework, as I have to look at my self in the mirror each day and give account of my doings . I just wanted to know how to put things together.

Thank you again. Have a nice day.

Philip M
07-22-2012, 03:45 PM
First I would like to start out saying that I do appreciate your help, but I also know that having someone else do my homework is not in my best interest. I find that, according to what you are saying about so much being depreciated, I am being taught wrong. This is very disturbing to me. I will study what you have sent but I will not submit it as my homework, as I have to look at my self in the mirror each day and give account of my doings . I just wanted to know how to put things together.

Thank you again. Have a nice day.

You are not the only person who is being taught obsolete Javascript!

And the word is deprecated - not depreciated. There is a big difference! Like sagacious and salacious. :D

07-22-2012, 06:14 PM
You are not the only person who is being taught obsolete Javascript!

And the word is deprecated - not depreciated. There is a big difference! Like sagacious and salacious. :D

Thank you for your correction :)

Since I am being taught obsolete Javascript, do you have any suggestions where to learn the proper way? Maybe through the "World Wide Web Consortium" (W3C)? Or should I find a mentor with vast experience and a ton of patience?
I also want you to know that learning this is not on a whim or just a school thing. I want to have my own business in Web Design, but at the same time I would like to be able to know ALL of the programming languages. It is a passion not a hobby.
Thanks again. :)

Old Pedant
07-22-2012, 09:46 PM
I would like to be able to know ALL of the programming languages.

Ummm... I have been programming for 43 years. I have used the following programming languages (and probably a few more that I have omitted) [in rough order that I learned them]:
Fortran II
SBC BASIC
COBOL (fundamentals, only)
Fortran IV
IBM 360 Assembly Language (AL from here on)
Interdata 80 AL
PPS4 AL
Data General AL
EDS BASIC
Algol (fundamentals)
HP-MX AL
Various programmable calculator ALs
Eclipse AL
Eclipse BASIC
6800 AL
6502 AL
Forth
NS COPS AL
Z-80 AL
Z-BASIC (well, to be fair, I helped create the language)
Apple II Integer BASIC
C (by writing a compiler for it)
Atari BASIC (also helped create it)
Action!
BASIC XL
BASIC XE
Tiny C
dBase II
Pascal
Prolog
C++
dBase IV
OOSQL
SQL (4 different varieties thereof)
Java
HTML
VBScript
JavaScript
VB.NET
C#

And that means I have probably learned maybe 10% of the computer languages that I could have. Maybe. Probably less than 5%, actually.

I suppose that if all I had ever done was learn computer languages and never actually USED them to create products then I could have learned maybe 50% of the languages. Maybe.

Then again, maybe not.

I believe you need to rethink that goal of "ALL languages." Assuming you want to actually earn a living.

Old Pedant
07-22-2012, 09:49 PM
If you meant the w3cschools site, then no. Most of the JavaScript they teach there is older. Oh, the language, per se, hasn't changed much, but the USAGE of it has.

07-22-2012, 10:59 PM
I suppose you are right about learning all of it, should have started this right out of College, but life got in the way. I am impressed with how much you know and I don't have 43 years to learn some of what you have learned, but I will keep plugging along.

It has been a pleasure chatting with you, and thank you for the heads up on learning computer coding. Take care :)

Richter
07-22-2012, 11:42 PM
@Old Pedant
You forgot Ruby and Python :eek:
ps. It seem like I forgot some language ... oh F# lol

Old Pedant
07-23-2012, 03:51 AM
@Old Pedant
You forgot Ruby and Python

Nope. I just haven't learned them yet. Started on Python.

I did forget a few languages in that list. But so obscure nobody would care.

The most obscure one was assembly language for a custom Atari game microprocessor. I actually had to *first* create the assembly language before I could write the game! It was a weird one. 10 bit ROM (instruction set) and 5 bit RAM (actually, just game registers...no RAM in the conventional sense). I think it only had maybe 16 or 20 different machine instructions. Maybe a few more.