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 11-08-2012, 03:27 AM   PM User | #1
nittwitt103
New to the CF scene

 
Join Date: Nov 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
nittwitt103 is an unknown quantity at this point
Writing a simple array or for loop

Hi all. Apologies in advance, as I'm a JS noob. I'm working in a print medium that uses JS for variables and my code works. However, it's messy so I've been trying to rewrite it more efficiently using an array and a loop and counter, but to no avail.

Basically, the user fills out certain fields (like a spreadsheet almost), then I change those values from strings to numbers and total them. It works, it's just I want to learn how to write something much cleaner and more efficient. Thanks in advance!

Here's the code thus far:

LineTotal1 = StringToNumber(Field("Linetotal1"));
LineTotal2 = StringToNumber(Field("Linetotal2"));
LineTotal3 = StringToNumber(Field("Linetotal3"));
LineTotal4 = StringToNumber(Field("Linetotal4"));
LineTotal5 = StringToNumber(Field("Linetotal5"));
LineTotal6 = StringToNumber(Field("Linetotal6"));
LineTotal7 = StringToNumber(Field("Linetotal7"));
LineTotal8 = StringToNumber(Field("Linetotal8"));
LineTotal9 = StringToNumber(Field("Linetotal9"));
LineTotal10 = StringToNumber(Field("Linetotal10"));
Discount = StringToNumber(Field("Discount"));
Tax = StringToNumber(Field("Tax"));


var Total = LineTotal1
+ LineTotal2
+ LineTotal3
+ LineTotal4
+ LineTotal5
+ LineTotal6
+ LineTotal7
+ LineTotal8
+ LineTotal9
+ LineTotal10
- Discount
+ Tax;

return Total;
nittwitt103 is offline   Reply With Quote
Old 11-08-2012, 04:09 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Why did you invent your own StringToNumber when it's built into JS in several different ways.

i.e.,
Code:
num = Number(str);
num = parseFloat(str);
num = 1 * str;
num = + str;  // not a typo...use a unary plus
num = parseInt(str); // if you want to be sure you get integer
There are subtle differences in those, but surely any of them are good enough for most work.

And why did you invent a Field( ) function when, again, that comes naturally?

Anyway:
Code:
var Total = 0;
for ( var n = 1; n <= 10; ++n )
{
    total += StringToNumber(Field("Linetotal" + n) );
}
Total -= StringToNumber(Field("Discount"));
Total += StringToNumber(Field("Tax"));
But *DO* think about using what is already builtin instead of reinventing wheels.

Example:
Code:
var form = document.forms[0];
var Total = 0;
for ( var n = 1; n <= 10; ++n )
{
    total += Number( form["Linetotal" + n].value );
}
Total -= Number(form.Discount.value);
Total += Number(form.Tax.value);
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
nittwitt103 (11-08-2012)
Old 11-08-2012, 04:12 AM   PM User | #3
nittwitt103
New to the CF scene

 
Join Date: Nov 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
nittwitt103 is an unknown quantity at this point
Hello! Yes, you are 100% correct. Those are already built in, but I'm using a form of VDP software that has it's own unique "building blocks". For instance I tried using "parseInt" to no avail in another area (though I may have been guilty of doing it wrong). It's doubly hard because I'm learning JS and having to learn this as well.

You help is much appreciated though!
nittwitt103 is offline   Reply With Quote
Old 11-08-2012, 05:31 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I assume you are, for some reason, required to use that VDP stuff. Too bad. It's kind of like being told to write an essay in Spanish while you are studying Italian. Yeah, there are cognates, but the aren't exactly the same.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 11-08-2012, 02:04 PM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by nittwitt103 View Post
Hello! Yes, you are 100% correct. Those are already built in, but I'm using a form of VDP software that has it's own unique "building blocks". For instance I tried using "parseInt" to no avail in another area (though I may have been guilty of doing it wrong). It's doubly hard because I'm learning JS and having to learn this as well.

You help is much appreciated though!
if javascript logic works correctly, there are low-level computer-science substitutions you can use.


if you don't need it to be an integer strictly speaking but just a number that will add instead of concat, you can use either

Code:
 val="123";
 (val *1) === Number( val ) // times one
or
Code:
 val="123";
 +val === Number( val ) // leading plus
those are harder to read than Number(), Math.floor(), parseFloat() and ParseInt(), but they add up.


for integers, you can do other ones to floor or round:

Code:
var num=Math.random();
parseInt(  num * 10  )  ===  (0| num * 10) ;
aka:
Code:
var num=Math.random();
Math.floor(  num * 10  )  ===  (0| num * 10) ;

as a callable function:
Code:
function int(number){ return 0|number;}
if your numbers are known to be positive and you want to round it instead of floor it,
Code:
val ="123.876";
+val+.5|0
if the numbers are negative, you have to flip the sign, round it and unflip...
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%

Last edited by rnd me; 11-08-2012 at 02:06 PM..
rnd me 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 09:54 AM.


Advertisement
Log in to turn off these ads.