Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-27-2013, 06:27 PM   PM User | #1
iLuvNerds
New to the CF scene

 
Join Date: Feb 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
iLuvNerds is an unknown quantity at this point
JS for loop Helppppp

I am trying to get my for loop to work to print the results and I have no clue on how...if anyone can help or at least give me a hint I would appreciate it!

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Midterm Sum of Integers</title>
<script type="text/javascript">
//Midterm


function calcSum() {
var box1 = parseInt(document.calculator.txtBox1.value);
var box2 = parseInt(document.calculator.txtBox2.value);
var box3 = parseInt(document.calculator.txtBox3.value);
var box4 = parseInt(document.calculator.txtBox4.value);
var box5 = parseInt(document.calculator.txtBox5.value);
var sum = (box1 + box2 + box3 + box4 + box5);

var calcResult = sum;
document.calculator.Result.value = calcResult;


for(var count = 1; count < calcSum.length; ++count){document.write(count+"<br/>");}

}





</script>
</head>

<body>
<h1>PLEASE ENTER NUMBERS AND PRESS THE TAB KEY</h1>
<form name="calculator">
<input type="text" name="txtBox1" value="0" onchange="calcSum()" /><br>
<input type="text" name="txtBox2" value="0" onchange="calcSum()" /><br>
<input type="text" name="txtBox3" value="0" onchange="calcSum()" /><br>
<input type="text" name="txtBox4" value="0" onchange="calcSum()" /><br>
<input type="text" name="txtBox5" value="0" onchange="calcSum()" /><br><br>
<h1>SUM:</h1><input type="text" name="Result"/>
</form>
</body>
</html>
iLuvNerds is offline   Reply With Quote
Old 02-27-2013, 06:31 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 941
Thanks: 7
Thanked 95 Times in 95 Posts
WolfShade is an unknown quantity at this point
calcSum is a function and doesn't have a length.

document.write is long deprecated. Create a div, give it an ID, and set the innerHTML to the value.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 02-27-2013, 06:32 PM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
the problem is the document.write statement which overwrites the entire page the first time through. Replace the long dead document.write with more modern JavaScript and your code will then work.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 02-27-2013, 07:00 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,032
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Prefer to use Number() rather than parseInt() - parseInt() is intended for conversion of numbers from one base to another. If you do use parseInt() you ought to specify the radix (10).

You ask the user to enter numbers, but then change them to integers. Why can the user not enter 2.5, 3.7 and so on? And what if the user enters "XYZ" or something? You can trap NaN entries with

Code:
var box1 = Number(document.calculator.txtBox1.value) || 0;   // assign 0 if the value entered is not a number.
box1 = parseInt(box1,10);  // if you really do want integers.  10 is the radix (number base - i.e. decimal)
document.calculator.txtBox1.value = box1;  // write it back to the field
Not sure what your document.write() is intended to output, but simply scrap it.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 02-27-2013 at 07:04 PM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 11:12 AM.


Advertisement
Log in to turn off these ads.