1

I need to find specific array item from amount according **"1000,2000,3000|100,200,300"** eg..,(range|amount) string by input amount

like..,

--Range-- = --Output-- 0 to 1000 = 0<br /> 1001 to 2000 = 100<br /> 2001 to 3000 = 200<br /> 3000 onwards = 300

I did for this

    var input = 3001;       // (test inputs 1000,1500,2200)
    var output=0;
    var str = "1000,2000,3000|100,200,300";
    var range = str.split('|')[0].split(',');
    var amount = str.split('|')[1].split(',');
    for(var i=0;i<range.length;i++){
        var checkRange= parseInt(range[i]);
        if(input<=checkRange){
            output=i!=0?(amount[i-1]):0;
            break;
        }
    }
    console.log(output)

this code is working fine only if input amount in between 0 to last array item(range 0-3000, input is >=3000 ), but if input amount(<3000) is greater than last array item(range) then returning 0

Please help to solve this stuff!

2
  • I don't think this is very clear... What should the for loop return? The index of a given item or the amount? Commented May 13, 2017 at 6:11
  • var input should be in between range 0 to 1000 = 0 1001 to 2000 = 100 2001 to 3000 = 200 3000 onwards = 300 (returns 0 or 100 or 200 or 300 according to range) Commented May 13, 2017 at 6:14

2 Answers 2

1

Try this .

  1. Your if condition was wrong .Change with if(checkRange <=input)
  2. Because the checkrange always lesser the input.That only its always zero.
  3. Then remove the break statement.and declare the output as array.
  4. Then push each result value to array .Finally get the the last one value using pop()

var input = 3001;       // (test inputs 1000,1500,2200)
    var output=[0];
    var str = "1000,2000,3000|100,200,300";
    var range = str.split('|')[0].split(',');
    var amount = str.split('|')[1].split(',');
    for(var i=0;i<range.length;i++){
        var checkRange= parseInt(range[i]);
        if(checkRange <=input){
            output.push(amount[i])
           }
    }
    console.log(output.pop())

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

2 Comments

It is working fine... thanks a lot for correcting my codes and helping
@Rakesh ...you are always Welcome
1

You could use an iterative approach while checking if the value is smaller then the right range value.

function getAmount(ranges, value) {
    var data = string.split('|').map(function (a) { return a.split(',').map(Number); }),
        index = 0;

    data[0].every(function (a) { return a < value && ++index; });
    return data[1][index - 1] || 0;
}

var    string = "1000,2000,3000|100,200,300";

console.log(getAmount(string, 1));    //   0
console.log(getAmount(string, 999));  //   0
console.log(getAmount(string, 1000)); //   0
console.log(getAmount(string, 1001)); // 100
console.log(getAmount(string, 2000)); // 100
console.log(getAmount(string, 2001)); // 200
console.log(getAmount(string, 3000)); // 200
console.log(getAmount(string, 3001)); // 300
.as-console-wrapper { max-height: 100% !important; top: 0; }

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.