0

On a click event, I need to take some numbers from several input fields, find the total of those numbers, do some math on those numbers individually (including diving each by the total), and output new numbers in order, individually, somewhere else.

Here's a screenshot, if it helps: calculator screenshot

So far, I've approached it by pushing the inputs into an array. But for whatever reason, I can't figure out how to perform math on the numbers in the array and then have them output elsewhere.

Any pseudo code or tips would be amazingly helpful.

I've tested and deleted other stuff a million times, but this is where I'm at.

HTML:

 <div class="input-grid">
            <div class="input-row">
                <div class="input-box"><input type="text"><span class="input-unit">px</span></div>
                <i class="fa fa-chevron-right"></i>
                <span class="input-response">1.541</span>
            </div>
            <div class="input-row">
                <div class="input-box"><input type="text"><span class="input-unit">px</span></div>
                <i class="fa fa-chevron-right"></i>
                <span class="input-response">1.542</span>
            </div>
            <div class="input-row">
                <div class="input-box"><input type="text"><span class="input-unit">px</span></div>
                <i class="fa fa-chevron-right"></i>
                <span class="input-response">1.543</span>
            </div>
            <div class="input-row">
                <div class="input-box"><input type="text"><span class="input-unit">px</span></div>
                <i class="fa fa-chevron-right"></i>
                <span class="input-response">1.544</span>
            </div>
        </div>
        <div class="btn-wrap">
        <span class="calc-btn">calculate</span>
        <span class="reset-btn">reset</span>
        </div> 

JS:

$('.calc-btn').click(function(){

    var inputs = [];


    function collectInputs(array){

        $('input').each(function(){

        array.push($(this).val());

        });

    }


collectInputs(inputs);


});
8
  • 1
    Where's your code so far? Commented Dec 16, 2013 at 22:16
  • Are calculations on all input fields same ? Commented Dec 16, 2013 at 22:19
  • maybe you forgot use parseInt the number before calculate Commented Dec 16, 2013 at 22:22
  • @DoorknobofSnow I added some of what I've done. Commented Dec 16, 2013 at 22:22
  • @Ani Yes, all the same. Basically, I'll divide each input by the total (of all inputs), multiply each by 10 and divide by the lowest input. Commented Dec 16, 2013 at 22:22

4 Answers 4

1

JS

$('.calc-btn').click(function(){

var inputs = [];
var total = 0;

    $('.input-box input').each(function(){

        inputs.push($(this).val());
        total = total + ($(this).val());

    });
    var minvalue = Math.min.apply(Math,inputs); // get minimum value in array
    var count_array = inputs.length; // number of elements in array

    //calculation part
    for (i = 0; i < inputs.length; ++i) {
           inputs[i] = ((inputs[i] / total) * 10); / minvalue;
           inputs[i] = inputs[i].toFixed(3);
    }


});

Fiddle: http://jsfiddle.net/KDqts/3/

Sign up to request clarification or add additional context in comments.

8 Comments

this solution seems the most clear to me, of the three here. so in order to set the values of each .input-response, would you just $('.input-response').each(i) {$(this).text(input[i])}; ?
yes, correct. your syntax is incorrect, but the logic is right.
I'm running it, and my console is telling me it has no method toFixed(); -- any idea why that would be?
oh...remove that toFixed. It's is a function on numeric values but not for string values.
k, so when I do that, I get these crazy values being returned. like "3.33000000000333e-11" for all of the input responses. Any ideas? Sorry to be bother. You've helped so much already.
|
0
var numbers = [];

//take some numbers from several input fields, 
$('.myFields').each(function() {
  numbers.push($(this).val());
});

//find the total of those numbers, 
var total = 0;
$.each(numbers, function() {
  total += this;
});

//do some math on those numbers individually 
var processed = [];
$.each(numbers, function(i) {
  //(including diving each by the total), 
  processed[i] = this / total;
});

//and output new numbers in order, 
processed.sort();

$.each(numbers, function() {
  //individually, somewhere else.
  $('#somewhere').val(this);
});

1 Comment

I was a little fuzzy on how to use $.each that way (as opposed to $('element').each(); but that really clarifies it for me. Thanks, I'll give this a try.
0
$('.calc-btn').click(function(){
     var minVal=Infinity;
    /* create array, get minVal, and sort*/
    var arr= $('input').map(function(){
        var val=1*$(this).val();
        minVal = val < minVal ? val : minVal;
        return val
    }).get().sort();
    /* do math on array*/
    $.each(arr, function(i, val){
        arr[i]= val *10 /minVal;
    });

    /* do something with `arr`*/  
});

DEMO

Comments

0

It took a while, but I got it figured out (with lots of help). Thanks Ani!

HTML:

<div class="input-grid">
            <div class="input-row">
                <div class="input-box"><input type="text"><span class="input-unit">px</span></div>
                <i class="fa fa-chevron-right"></i>
                <span class="input-response">1.541</span>
            </div>
            <div class="input-row">
                <div class="input-box"><input type="text"><span class="input-unit">px</span></div>
                <i class="fa fa-chevron-right"></i>
                <span class="input-response">1.542</span>
            </div>
            <div class="input-row">
                <div class="input-box"><input type="text"><span class="input-unit">px</span></div>
                <i class="fa fa-chevron-right"></i>
                <span class="input-response">1.543</span>
            </div>
            <div class="input-row">
                <div class="input-box"><input type="text"><span class="input-unit">px</span></div>
                <i class="fa fa-chevron-right"></i>
                <span class="input-response">1.544</span>
            </div>
        </div>
        <div class="btn-wrap">
        <span class="calc-btn">calculate</span>
        <span class="reset-btn">reset</span>
        </div>

JS:

$('.calc-btn').click(function(){

var inputs = [];
var total = 0;

    $('.input-box input').each(function(){
    var num = parseFloat($(this).val());
        inputs.push(num);
        total = total + (num);

    });
    var minValue = Math.min.apply(Math,inputs); // get minimum value in array
    var count_array = inputs.length; // number of elements in array

    //calculation part
    for (i = 0; i < inputs.length; i++) {
           inputs[i] = ((inputs[i] / total) * 1000)/ minValue;
           inputs[i] = inputs[i].toFixed(3);

    }

        $('span.input-response').each(function(i){
            $(this).text(inputs[i]);

         });

});

One major problem was that I needed use parseFloat to convert the values to integers before pushing them to an array. Another problem was figuring out how to round the final answers to a management number of decimals which I used toFixed(); for. Thanks to everyone for their help!

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.