rewrite each function

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Lphp
Forum Commoner
Posts: 74
Joined: Sun Jun 26, 2011 9:56 pm

rewrite each function

Post by Lphp »

I have this function
var sum = 0;

$(".totalprice").each(function() {
var val = $.trim( $(this).text() );

if ( val ) {
val = parseFloat( val.replace( /^\$/, "" ) );

sum += !isNaN( val ) ? val : 0;
}
});
can I rewite to something like , because I have a few total
var sum = 0;

$(".totalprice").each(function() {
var val = $.trim( $(this).text() );


total(val )

});
function total(val){
val = parseFloat( val.replace( /^\$/, "" ) );

sum += !isNaN( val ) ? val : 0;
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: rewrite each function

Post by requinix »

Yeah, but it's not clear to me why you would want to do that when the first version is much simpler...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: rewrite each function

Post by Christopher »

Yes you can. Did you try it?
(#10850)
Lphp
Forum Commoner
Posts: 74
Joined: Sun Jun 26, 2011 9:56 pm

Re: rewrite each function

Post by Lphp »

Christopher wrote:Yes you can. Did you try it?
I get it , I need to put outside the
function sumTotal(the_value, curr_vale) {

.......
return sum;
}
$(function() {

sum = sumTotal(".pptxt", $(this).children().val());
});
Post Reply