Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7826189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:17:56+00:00 2026-06-02T09:17:56+00:00

I have an array of images var prizeImage; //this is a dynamic amount var

  • 0

I have an array of images

var prizeImage;

//this is a dynamic amount
var result = 55; 

//the length of this array will change. Images will be added/removed
var prizeImageArray = ['image1.png','image2.png','image3.png','image4.png'] 


//I want to avoid having to type each of these if statements. 
if(result < 25){
    prizeImage = prizeImageArray[0];
}
if(result > 26 && result < 50){
    prizeImage = prizeImageArray[1];
}
if (result...)//and so on

The above is terrible code.

I want a method that will allow for the prize image array to change size and to avoid typing each of the if statements.

I hope this makes sense, I have a feeling it doesn’t.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:17:57+00:00Added an answer on June 2, 2026 at 9:17 am

    If I understand what you’re asking, you want to have a percent (from 0-100) and find out which image in your array is closest to that percent location and get that image URL. If so, you can do that by dividing the percent by 100 to turn it into a decimal percentage, multiplying by the length of the array and then rounding that to an integer. That gets you the array index that is closest to your percentage. You can then index from the array to fetch the desired value.

    You can do it like this:

    function findPercentLocation(array, percent) {
        return(array[Math.round((percent / 100) * (array.length - 1))]);
    }
    
    var prizeImageArray = ['image1.png','image2.png','image3.png','image4.png'] 
    var imageSrc = findPercentLocation(priceImageArray, 27);
    

    If you want to make it more robust, you could protect against percent values outside the 0-100 range like this which would avoid extending beyond the bounds of your array:

    function findPercentLocation(array, percent) {
        if (percent < 0) percent = 0;
        if (percent > 100) percent = 100;
        return(array[Math.round((percent / 100) * (array.length - 1))]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an observable array with var items= [ {image: /images/image1.jpg }, {image: /images/image2.jpg
I have an array like this: var gridArray = [ 1,2,0,1,2,3,2,1,2,3,4,4,3,2,2,2,0 ] And I
I have the next array: var Images = $('.cs_slider').children(); Checked the array and it
I have an array of image IDs. var images = ['238239', '389943', '989238', ...
I have an array of images var photos = [images/bg01.jpg, images/bg02.jpg, images/bg03.jpg]; I am
I have an array (below) var img_name = new Array(images/test.jpg, images/test.jpg); var imgTotal =
I have an images array in which there are number of images. Not fixed,
I have image Array with two images out of that first image its showing
i have two int array of images , i have implemented it in grid
I am using PHP and I have an array of user images that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.