HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Displaying total cost of prices from a table

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
totaldisplayingpricescostfromtable

Problem

I have this JavaScript function which gets prices from a table, adds them up, and outputs the total cost. I want to refactor this to make it as small as possible.

function getTotalPrice(do_request)
{
console.log('Get total price');
var prices_array = new Array(); // Where our prices are held

// For each .total_item_price - a  within my table.
$(".total_item_price").each(function(){
    var text = $(this).text();                   // Get the value
    var prices = text.substring(1, text.length); // Format it into a string
    prices_array.push(prices);                   // Push it onto our array
});

var result = eval(0);
//  Add up our array
for(i = 0; i £" + result + "");        
}
else 
{
    // Otherwise we just want the total price and return it.
    return result;
}
}

Solution

First, I would switch from using eval to parseFloat, since it appears that eval provides no extra features and allows arbitrary code to be executed unless you just happen to have values other then numbers in total_item_price tds.

Second, since you are performing no extra processing with prices_array, it would be better to skip the array altogether and combine:

var prices_array = new Array(); // Where our prices are held
 // For each .total_item_price - a  within my table.
 $(".total_item_price").each(function(){
     var text = $(this).text();                   // Get the value
     var prices = text.substring(1, text.length); // Format it into a string
     prices_array.push(prices);                   // Push it onto our array
 });

 var result = eval(0);
 //  Add up our array
 for(i = 0; i < prices_array.length; i++)
 {
     temp = eval(prices_array[i]);
     result += temp;
 }


into (with result renamed as totalSum for readability):

var totalSum = 0;
 // For each .total_item_price - a  within my table.
 $(".total_item_price").each(function(){
     var text = $(this).text();                   // Get the value
     var price  = text.substring(1, text.length); // Format it into a string
     price = parseFloat(price);
     totalSum += price;
 });

Code Snippets

var prices_array = new Array(); // Where our prices are held
 // For each .total_item_price - a <td> within my table.
 $(".total_item_price").each(function(){
     var text = $(this).text();                   // Get the value
     var prices = text.substring(1, text.length); // Format it into a string
     prices_array.push(prices);                   // Push it onto our array
 });

 var result = eval(0);
 //  Add up our array
 for(i = 0; i < prices_array.length; i++)
 {
     temp = eval(prices_array[i]);
     result += temp;
 }
var totalSum = 0;
 // For each .total_item_price - a <td> within my table.
 $(".total_item_price").each(function(){
     var text = $(this).text();                   // Get the value
     var price  = text.substring(1, text.length); // Format it into a string
     price = parseFloat(price);
     totalSum += price;
 });

Context

StackExchange Code Review Q#4415, answer score: 6

Revisions (0)

No revisions yet.