1

Hi I am trying to find a index for a number in percentage and integer array. Say arraynum = ['10%','250','20%','500'] and user sends a value 15%,in which range does this number resides? I could find index for a integer number using this code

 function test(number, ranges) { for (var i = 0; i < ranges.length; ++i) { if (number < ranges[i]) { return i; } } } var ranges = [2000, 4000, 6000, 999999]; console.log(test(1710, ranges)); 

Now I have mixture of integer and percentage value inside a array and number that a user pass to this function can be a integer,decimal or percentage How to find in which index does the given number resides? Should I convert all value in the mixture array to some format? How to do this? Can someone please help me with this? Thanks in advance.

9
  • Why are you doing this number < ranges[i]? I thought you want to compare if it is that number that the user inputted. Commented Oct 9, 2016 at 5:08
  • I need to do two steps like compare a number with given array if not find in which range the number is in and get it index. @RaxWeber Commented Oct 9, 2016 at 5:27
  • any boundaries for ranges?(upperLimit,lowerLimit) Commented Oct 9, 2016 at 5:37
  • yes,999999 @Aravind Commented Oct 9, 2016 at 5:48
  • my question is that only is there any fixed range Don't tell (0,99999) but some meaningful range based on your functionality? Commented Oct 9, 2016 at 5:50

3 Answers 3

1

Use parseFloat() to get the floating-point value of a string. You can use the fact that it only parses up until the first non-numeric character in the string in order to ignore the %.

Here is an implementation using Array#findIndex from ES6.

function test(number, ranges) { var num = parseFloat(number); return ranges.findIndex(function(element) { return num === parseFloat(element); }); } 
Sign up to request clarification or add additional context in comments.

7 Comments

I would be careful with findIndex if you aren't using a polyfill as it isn't support very well in browsers yet. Perhaps it might be better to use the [odash version: lodash.com/docs/4.16.4#findIndex
@thesublimeobject Right, the bulk of my answer is just parseFloat. They can do whatever they want for finding the index. Their loop would be fine even.
oh for sure. No issue with the answer whatsoever. Just wanted to point that out incase they didn't realize.
Thank you.I tried with this and getting -1 as index here is a fiddle jsfiddle.net/anusibi/Lvsvw6mn/5 @4castle
@Anu Is the data sorted? It's unclear to me what your input and output is meant to represent.
|
1

You might restructure your code like so

arraynum = ['10%','250','20%','500']; function test(value, arr) { return arr.indexOf(value.toString()); } test("10%", arraynum); // 0 test("500", arraynum); // 3 

9 Comments

Does this gives me index for decimal,integer and percentage values? @Damilola
Yes, I believe it should give you the first matched index for either integer or percentage values. It should be agnostic of the original value type since the value has been converted to string.
I tried with this and I am getting -1. Can you please see my edits in my question. @Damilola
See my edited answer. You should get correct index for existing element and -1 if element is not found
Thats working. But @Damilola user can give any input like test('25%', arraynum) or test('200', arraynum) for this I am getting index -1. Am I not clear?
|
0

Checking condition for mixture of values with one single input is little tricky and you need to go for JSON array. Please run

var array_of_elements = [{ "Price": 250, "Percentage": "10" }, { "Price": 500, "Percentage": "25" }, { "Price": 750, "Percentage": "50" }, { "Price": 1000, "Percentage": "75" }]; function test(number_or_percent, array) { for (var i = 0; i < array.length; ++i) { if (number_or_percent.charAt(number_or_percent.length - 1) == "%") { if (parseInt(array[i].Percentage) > parseInt(number_or_percent.slice(0, ((number_or_percent.length) - 1)))) { return i; } } else { if (array[i].Price > parseInt(number_or_percent)) { return i; } } } } console.log(test('100', array_of_elements)); console.log(test('30%', array_of_elements));

Is this what you want?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.