I want to search a file with the value of a variable

Asked

Viewed 49 times

-1

I would like to find an image with the variable value valor, function-generated getRandomInt(), which generates a random number between the max and the min. My goal is that each time the user accesses the site, a new image appears next door, including, if someone knows another way to solve this problem, I would appreciate it. Finally, so far, I’ve created a switch so that, at each value generated, one looks for a certain image, but this form is half a step.

For the record, the function works perfectly, the problem is on the line of import.

function getRandomInt(min, max) {
    var byteArray = new Uint8Array(1);
    window.crypto.getRandomValues(byteArray);

    var range = max - min + 1;
    var max_range = 256;
    if (byteArray[0] >= Math.floor(max_range / range) * range)
        return getRandomInt(min, max);
    return min + (byteArray[0] % range);
}

const valor = getRandomInt(1,6);

import homeImage from `../../assets/home-images/${valor}.png`;
  • If the range is 1 to 1000 why use getRandomValues and other things? It’s not easier to create images with the name 1.png up to 1000.png?

  • I am in a similar situation to yours. I am also interested in answers on this topic, which also happened to me with the Topflix.film, but I managed to solve the problem. There I could see that these forums work and offer excellent alternatives.

1 answer

0


Hello,

You can use the following code:

const image = import(`../../assets/home-images/${valor}.png`);

and within a loop, pass it on the tag <img src={image} />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.