How do I return an image name based on an integer? I have an idea but it uses loads of if and else cases.
So here's the deal, I need a function which would return the name of an image based on an integer between 1 and 500. If the integer is between 1 and 30, I display image1 and from this point on I return a different image at 50, 75, 100, 125, 150, 175, 200, ...etc.
The first idea that comes to mind is using IFs and Elses like that:
if (number >=1 && number <= 29) {
Return image1
} else if (number >=30 && number <=49) {
Return image2
} else if... etc
But this function will become huge since I'll have around 20 If Else in order to cover all possible outcomes until number 500.
The other option is to create a JSON object with key: value pairs and get the image using the integer key like this:
returnImage(int) {
let images = {
30: 'image1',
50: 'image2',
75: 'image3'
}
return images[int]
}
Sadly I'm not sure how to make this work if the integer is 37 for example. I could obviously list all integers as keys but then the function would be even bigger than the if else function + there's gonna be a lot of repetition so that doesn't seem to work.
Any recommendations?