خرید بک لینک

Vote count: 0

I have to create a new input text every time the user enters an integer and those integers should be add it to an array which at the end the program must sum or subtract depending on the values.

For example, if the array is like this: [1, 5, 10] the final result is: 16 but if the array goes like this [2, 3, 7, -1, 5] the final result should be 7, why? it is because you sum up 2 + 3 + 7 and then subtract 5.

The main problem I have right now is that there is something wrong with my code which is not doing the final calculation.

A bit of code:

var arrayOfNumbers = [];
var column = 1;

function logingValues (){

    var values = $('input[name^=valuesToSum]').map(function(idx, elem) {
            newVal = $(elem).val();
            if (isNaN(newVal)) {
              newVal = 0;
            }
            retu newVal;
    }).get();

    console.log("Values aquired from inputs " + values);

    arrayOfNumbers = values;

    console.log("Values inserted on Array " + arrayOfNumbers);
}


function removeElement (id){
    $('#' + id ).remove();
    console.log("element #" + id + " was eliminated");
}

function iterateOverArray(array){

   var oldArray = array;
   var newArray = "";

   for (var i = 0; i < oldArray.length; i++) {
      newArray += " + " + oldArray[i];
   }

   retu newArray;
}

function addElement (whereToPrint){

    logingValues();

    var newArray = iterateOverArray(arrayOfNumbers);

    var printId = "print" + whereToPrint;

    console.log(printId);

    console.log("we can see the array like this " + arrayOfNumbers);

    $('#'+ printId).html(newArray);

    console.log('Element was created');


      column += 1;

      var newInput  =  // here goes a template

      $('.elemPlaceHold').append(newInput);

}

function loadingBar (){

    var summingBar = '<div class="rect1"></div><div class="rect2"></div><div class="rect3"></div><div class="rect4"></div><div class="rect5"></div>';

    $('.spier').append(summingBar);
}

function caculationEffect (countTotal){

    $('#result').html("");
    loadingBar();


    setTimeout(function(){

        $('.spier').html("");
        $('#result').html(countTotal);
    }, 2000);
}

function calculate (){

    console.log("is calculating....");

    logingValues();
    // Variable to hold your total 
    var countTotal = 0;  
    // Variable that states how the process start (sum/subtract)
    var conditional = true;
    var changeCondition;

    for(var i = 0; i < arrayOfNumbers.length; i++) {

        if(arrayOfNumbers[i] === -1){
            changeCondition = conditional ? conditional = false : conditional;
        }else{
            if(conditional === true){
               countTotal += arrayOfNumbers[i];
            }else{
               countTotal -= arrayOfNumbers[i];
            }
        }
      }
    console.log('countTotal', countTotal);
    caculationEffect('countTotal', countTotal);
}

Here is a JSBin

Any suggestions?

asked 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: پنجشنبه 17 تير 1395 ساعت: 9:24

صفحه بندی