2

So I wanted to choose an array index based off its percentage in the array.

The "percentage in the array" is just a function of the index, so like for an 11-element array the 50% index would be 5.

const numbers = [0 , 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; 

I imagine I'd want to first go ahead by grabbing the length of the array to have a solid number to work a percentage from.

numbers.length; // 14 

Though how would I then go about using a percentage to go into the array using the length to select an index that NEAREST matches the percentage? For example if I wanted to select the index that was nearest to being on 25% of the array?

6
  • You need to decide what "percentage" means to you. You need to go through the array and calculate this "percentage" for each value in the array. Then you need to decide what "nearest" means. Then you need to find the percentage that is "nearest" to the given one and get its index. Commented Jul 20, 2021 at 15:26
  • 1
    @HereticMonkey I could be wrong but I think the "percentage in the array" is just a function of the index, so like for an 11-element array the 50% index would be 5. Commented Jul 20, 2021 at 15:33
  • @Pointy That's certainly one interpretation of that phrase. I tend to make as few assumptions about other people's questions as possible. Commented Jul 20, 2021 at 15:36
  • Yeah that's exactly it @Pointy , you explained it a lot better thank you :D Commented Jul 20, 2021 at 15:36
  • @Harry You can and should edit your question to clarify it. Commented Jul 20, 2021 at 15:37

3 Answers 3

8

I think you are looking for something like this.

const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; const percentage = 50 let indexAtPercentage = Math.round((numbers.length - 1) * percentage / 100) console.log("index: " + indexAtPercentage + "\nnumber at index: " + numbers[indexAtPercentage])

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

1 Comment

It's worth noting that Math.round() will ceil numbers ending with .5.
2

You can calculate the index or value of a given percentage by subtracting one from the length of the array, multiplying it by the percentage, and finally flooring the result.

The percentage parameter should be a value between 0.00 (0%) and 1.00 (100%).

const indexOfPercent = (arr, percent) => Math.floor((arr.length - 1) * percent), valueOfPercent = (arr, percent) => arr[indexOfPercent(arr, percent)]; const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; for (let i = 0; i < numbers.length; i++) { const percent = i / (numbers.length - 1), index = indexOfPercent(numbers, percent), value = valueOfPercent(numbers, percent); console.log(`${i} ${percent.toFixed(2)} ${index} ${value}`); }
.as-console-wrapper { top: 0; max-height: 100% !important; }

Comments

2

I believe it is as clear as calculating the required index by getting the desired percentage mark from the length.

const numbers = [0 , 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]; let desiredPercentage = 25; if (desiredPercentage) { let indexAtPercent = Math.floor(numbers.length * desiredPercentage / 100); if (indexAtPercent) indexAtPercent--; console.log(numbers[indexAtPercent]); }

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.