2

I have a known value to compare

var sT = 100;

with an array of random values and an unknown length

var someArray = [12, 21, 3, 54, 5, ... ] 

I then want to add each number to all previous values

var one = someArray[0];
var two = someArray[0]+someArray[1];
var three = someArray[0]+someArray[1]+someArray[2];
// ...

or possibly I could do something like this?

var otherArray = [];

someArray.forEach(function(i){
    otherArray.push(i+=i);
    somefunction();
});

function somefunction() {
    otherArray.forEach(function(i){
        // ?
    })
}

then I need to check the value of the array starting at 'one' like so...

var int;

if ( sT <= one ){
    int = 1;
}
else if ( sT >= one && sT <= two ) {
    int = 2;
}
else if ( sT >= two && sT <= three ) {
    int = 3;
}

else if ... 

// until the last

else if ( sT >= twenty*0.8 ) {
    int = 20;
}

Is there a way to do this much more programmatically?

5
  • yep, already using it... just happens to not be in this function Commented Feb 17, 2016 at 0:44
  • Why are you creating individual variables for the summed values, rather than putting them in a new array? Then you could do the sums in a loop, and do the comparisons in a loop (possibly all in the same loop) instead of a chain of if/else if. Commented Feb 17, 2016 at 0:46
  • @nnnnnn edited my question to include your idea of not creating variables. Commented Feb 17, 2016 at 0:50
  • @Ageonix yep, sure can Commented Feb 17, 2016 at 0:56
  • @ShawnAppleheadNaquin It looks like you not only want to create an array with the sums but also find the index of the range that your input value falls in (your var int you are setting). Is this correct? Commented Feb 17, 2016 at 1:35

3 Answers 3

1

You can remap the array using .map() to a series of the sums. Then use .findIndex() to find the index of what range the input is in. To just get the array with sums just don't do the .findIndex() part of this

var sT = 56;
var arr = [1, 54, 34, 2, 64, 75, 87]

function sumSeries(r, n) {
  return r+n;
}

function findRange(arr,sT) {
  return arr.map(function(v,i,a) {
    return a.slice(0,i+1).reduce(sumSeries);  // Set this array value to sum of it and all previous values
  }).findIndex(function(v,i,a) {  // When function returns true findIndex will return current index
    if(i == 0) {
      return sT <= v;
    } else if(i == a.length-1) {
      return sT >= v*0.8;
    } 
    return a[i-1] <= sT && sT <= v
  }) + 1;
}

document.write(findRange(arr,sT))

Edit: Added +1 to returned index value as apparently desired.

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

Comments

1

Here is not JavaScript code and without test, should illustrate it。

var sum = someArray[0]
var intValue
var total = someArray.count //TotalNumber
for int index = 0; index < total; index ++ {
  if (index + 1) >= total {
    return
   }
  if sT < sum {
      intValue = 1
    }else if sT >= total*0.8 {
      intValue = total
    } else if(sT >= sum && sT <= sum + someArray[index+1]) {
       intValue = index+2
    }
}

Comments

0

I used a slightly modified version from @Goblinlord

Also, added +1 to the return to account for the zero-base.

var sT = 56;

var sT = 56;
var arr = [1, 54, 34, 2, 64, 75, 87];

function sumSeries(r, n) {
  return r+n;
}

function findRange(arr,sT) {
  return arr.map(function(v,i,a) {
    return a.slice(0,i+1).reduce(sumSeries);
  }).findIndex(function(v,i,a) {
    if(i === 0) {
      if(sT <= v)
        return true;
      return false;
    }
    if(i == a.length-1) {
      if(sT >= v*0.8)
        return true;
      return false;
    }
    if(a[i-1] <= sT && sT <= v)
      return true;
    return false;
  });
}

console.log( findRange(arr,sT)+1 );

1 Comment

You could have added the +1 directly to the return value inside the function. Either way, I added that to my code in my answer as well as having simplified some of it significantly.

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.