0

I issue i have with the script below is that i get perfect values in the oCollectedValue array but for some reason the oCreatedOn only get 1 string pushed to it and its the last one on the CreatedOn array. for the life of me i cant see whats wrong with this code. Help please. Thanks!

Route = "";
    var UpperCoherenceLimit = 9.99;
    var LowerCoherenceLimit = 4.98;
    var CollectedValue = ["1.7865","3.7865","4.786532564000","5.7865","6.7865","7.7865"];
    var CreatedOn = ["7/1/2018 12:00:00 AM","7/2/2018 12:00:00 AM","7/3/2018 12:00:00 AM","7/4/2018 12:00:00 AM","7/5/2018 12:00:00 AM","7/7/2018 12:00:00 AM"];
    var SkippedValueList = [];
    var oCollectedValue = [];
    var oCreatedOn = [];
    var DisableCoherenceError = true;

    for (var i = 0; i < CollectedValue.length; i++)
    {
        if(CollectedValue[i] > UpperCoherenceLimit || CollectedValue[i] < LowerCoherenceLimit)
        {
            if(UpperCoherenceLimit != LowerCoherenceLimit)
            {
                if(DisableCoherenceError == false)
                {
                    SkippedValueList.push(CollectedValue[i])
                }
                else
                {
                    SkippedValueList.push(CollectedValue[i])
                }
            }
        }
        else
        {
            oCollectedValue.push(CollectedValue[i])
            oCreatedOn.push(CreatedOn[i])
        }
    }

    if (oCollectedValue.length == 0)
    {
        Route = 'SKIP';
    }
4
  • 1
    List items separated with comma ? Commented Jul 13, 2018 at 18:25
  • 1
    if(DisableCoherenceError == false) makes no difference in the pushing ... Commented Jul 13, 2018 at 18:27
  • @JonathanHamel the program i use passes on the variables and values without explicitly declaring them like i did. I guess i just created with values as a reference. so the formatting wouldnt affect what the code does. I can run the script with the values presented but the oCreatedOn array gets populated by the last string of the CreatedOn array. makes sense? Commented Jul 13, 2018 at 18:33
  • @NinaScholz correct, that is just in the script and shouldn't interfere with whats being pushed to the oCreatedOn array. Commented Jul 13, 2018 at 18:41

2 Answers 2

2

Just separating the arrays items correctly seems to resolve the issue ? Javascript will automatically do typecasting for you when comparing numbers with strings, but you should consider parsing your strings to floats as well.

    var UpperCoherenceLimit = 9.99;
    var LowerCoherenceLimit = 4.98;
    var CollectedValue = ["1.7865", "3.7865", "4.786532564000", "5.7865", "6.7865", "7.7865"];
    var CreatedOn = ["7/1/2018 12:00:00 AM", "7/2/2018 12:00:00 AM", "7/3/2018 12:00:00 AM", "7/4/2018 12:00:00 AM", "7/5/2018 12:00:00 AM", "7/7/2018 12:00:00 AM"];
    var SkippedValueList = [];
    var oCollectedValue = [];
    var oCreatedOn = [];
    var DisableCoherenceError = true;

    for (var i = 0; i < CollectedValue.length; i++)
    {
        if(CollectedValue[i] > UpperCoherenceLimit || CollectedValue[i] < LowerCoherenceLimit)
        {
            if(UpperCoherenceLimit != LowerCoherenceLimit)
            {
                SkippedValueList.push(CollectedValue[i])
            }
        }
        else
        {
            oCollectedValue.push(CollectedValue[i])
            oCreatedOn.push(CreatedOn[i])
        }
    }
    
    console.log(oCollectedValue);
    console.log(oCreatedOn);

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

1 Comment

The thing is, he said he gets perfect values in the oCollectedValue array. How is this possible if the comma separator in the array declaration is indeed the problem here?
0

The following test passes:

require('chai').should();

describe('Arrays', () => {
  it('needs commas', () => {
    Route = "";
    var UpperCoherenceLimit = 9.99;
    var LowerCoherenceLimit = 4.98;
    var CollectedValue = ["1.7865", "3.7865", "4.786532564000", "5.7865", "6.7865", "7.7865"];
    var CreatedOn = ["7/1/2018 12:00:00 AM", "7/2/2018 12:00:00 AM", "7/3/2018 12:00:00 AM", "7/4/2018 12:00:00 AM", "7/5/2018 12:00:00 AM", "7/7/2018 12:00:00 AM"];
    var SkippedValueList = [];
    var oCollectedValue = [];
    var oCreatedOn = [];
    var DisableCoherenceError = true;

    for (var i = 0; i < CollectedValue.length; i++) {
      if (CollectedValue[i] > UpperCoherenceLimit || CollectedValue[i] < LowerCoherenceLimit) {
        if (UpperCoherenceLimit != LowerCoherenceLimit) {
          if (DisableCoherenceError == false) {
            SkippedValueList.push(CollectedValue[i])
          }
          else {
            SkippedValueList.push(CollectedValue[i])
          }
        }
      }
      else {
        oCollectedValue.push(CollectedValue[i])
        oCreatedOn.push(CreatedOn[i])
      }
    }

    if (oCollectedValue.length == 0) {
      Route = 'SKIP';
    }
    oCreatedOn.length.should.equal(3);
  });
});

So the code you posted does not reflect the problem you are facing. Please give us a Minimal, Complete, and Verifiable Example

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.