0

My values array is returning correctly in the form of [100,200,300,500] I just wish to sum up these values, what am I doing wrong?

Jquery

$('select').focus(function () {
    previous = parseInt($(this).val());
}).change(function() {

    var item_cost = parseInt($(this).attr('cost'));
    
    values = $.map($('select[name="cookies"]'), function (e) {
            
        return $(e).val()* item_cost;
                     
        for (var i = 0; i < values.length; i++) {
            total += parseInt(values[i]);
            console.log(total);                              
        }
    });

    alert(values);
4
  • Dunno, what are you seeing in the console? Please fix your code, it's hard to read as-is. Commented Nov 21, 2012 at 17:41
  • nothing, it seems the for loop is never being executed. Commented Nov 21, 2012 at 17:42
  • 1
    You have a return statement in the first line of your map callback function, before the for loop Commented Nov 21, 2012 at 17:43
  • return statement must be in the end of function if you ofcourse don`t want to exit function Commented Nov 21, 2012 at 17:46

1 Answer 1

2

Your code needs formatting. You missed a closing }); You need to declare var total = 0; as well. I'm not sure if that is elsewhere in your code.

$('select').focus(function() {
    previous = parseInt($(this).val());
}).change(function() {
    var item_cost = parseInt($(this).attr('cost'));

    values = $.map($('select[name="cookies"]'), function(e) {
        return $(e).val() * item_cost;
    }); // <-- RIGHT THERE

    for (var i = 0; i < values.length; i++) {
        total += parseInt(values[i]);
        console.log(total);
    }
});
alert(values);​
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.