...

View Full Version : Writing a simple array or for loop



nittwitt103
11-08-2012, 04:27 AM
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;

Old Pedant
11-08-2012, 05:09 AM
Why did you invent your own StringToNumber when it's built into JS in several different ways.

i.e.,


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:


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:


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);

nittwitt103
11-08-2012, 05:12 AM
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!

Old Pedant
11-08-2012, 06:31 AM
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.

rnd me
11-08-2012, 03:04 PM
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


val="123";
(val *1) === Number( val ) // times one
or

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:



var num=Math.random();
parseInt( num * 10 ) === (0| num * 10) ;

aka:


var num=Math.random();
Math.floor( num * 10 ) === (0| num * 10) ;



as a callable function:

function int(number){ return 0|number;}

if your numbers are known to be positive and you want to round it instead of floor it,

val ="123.876";
+val+.5|0

if the numbers are negative, you have to flip the sign, round it and unflip...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum