Page 1 of 1

rewrite each function

Posted: Thu Aug 25, 2016 9:16 am
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;
}

Re: rewrite each function

Posted: Thu Aug 25, 2016 10:38 am
by requinix
Yeah, but it's not clear to me why you would want to do that when the first version is much simpler...

Re: rewrite each function

Posted: Thu Aug 25, 2016 10:43 am
by Christopher
Yes you can. Did you try it?

Re: rewrite each function

Posted: Mon Sep 05, 2016 9:36 am
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());
});