Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-27-2003, 09:42 AM   #1 (permalink)
UnderWing
Registered User
 
Join Date: Mar 2003
Posts: 2
UnderWing is on a distinguished road
Just a newbie asking about JS

I'm a complete newbie to Javascript, so here's my problem:

I'm making a calculator program (code following) that is simple and can only compute Addition, Subtraction, Multiplication, Division, and Powers. I use radio buttons to have the user input his/her type of equation, and two text fields to input the numbers involved. When they press a button, the page returns an alert window with the solution. I have gotten everything to work, except addition. It seems to want to treat the input as a string, instead of a variable. Here's the code:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bg="#ffffff" text="#000000">
<script language="JavaScript" type="text/javascript">
<!--
function solve()
{
var f = document.f;
var num1 = 0;
var num2 = 0;
var sign = 0;
var final = 0;
var i = 0;


/*check for num1.*/
num1 = f.num1.value;

/*check for sign.*/ /*signs: 1 = +, 2 = -, 3 = *, 4 = /, 5 = ^*/
for (i = 0; i < f.sign.length; i++) if (f.sign[i].checked) sign = i + 1;

/*check for num2.*/
num2 = f.num2.value;

if (sign == 1) final = (num1 + num2);
if (sign == 2) final = num1 - num2;
if (sign == 3) final = num1 * num2;
if (sign == 4) final = num1 / num2;
if (sign == 5) {
var power = num2;
final = num1;
while (power > 1) {
final = final * num1;
power--
}
}

alert(final);



}
</script>

<form name="f">
<input name="num1" value=0>
<br>
<input type="radio" name="sign" value="1"> +<br>
<input type="radio" name="sign" value="2"> -<br>
<input type="radio" name="sign" value="3"> *<br>
<input type="radio" name="sign" value="4"> /<br>
<input type="radio" name="sign" value="5"> ^<br>
<input name="num2" value=0>
<br>
<input type="button" value="Solve" onclick="solve(f);">
</form>
</body>
</html>
UnderWing is offline   Reply With Quote
Old 03-27-2003, 12:28 PM   #2 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
to convert a string to an int:

Code:
var a_string = "123";
var the_int = parseInt(a_string);
alert(the_int * 10);
// should get 1230
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 03-27-2003, 01:23 PM   #3 (permalink)
saline
I am red.
 
saline's Avatar
 
Join Date: Feb 2003
Location: Cleveland, OH
Posts: 139
saline is on a distinguished road
abc123 is right but I also just wanted to add one little thing. I copied your code into the program I use to edit javascript and guess what I found. final, the variable your using to hold the total, is technically a reserved word in javascript.

If you don't know what I mean by reserved word it's the keywords that the interpreter or compiler looks for like 'if' or 'for' or 'while'.

Strange thing is, final is reserved for future use, which means, at least right now, it doesn't do anything. So its ok to use but if someday someone uses your script on a browser where final is specified your script will crash and burn.

I would recommend using the variable name 'total' instead of final or maybe 'final_total' just to be super specific.

welcome to codenewbie!
saline is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Would you mind if there were small flash elements at code newbie? sde Lounge 10 05-29-2004 09:05 PM
Linux Newbie Online Ilya020 Lounge 30 01-16-2003 03:02 PM
Newbie Help (Japanese Language Support) DasXel Linux / BSD / OS X 11 01-14-2003 06:20 AM
Linux Newbie Online Ilya020 Linux / BSD / OS X 2 01-07-2003 09:49 AM
Newbie Site ? DesertWolf Java 2 12-05-2002 12:38 PM


All times are GMT -8. The time now is 08:20 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting