Randomized Image Generator Array with Descriptive Captions

I am currently designing a unique image generator with additional fields or captions that will be displayed on the page. To achieve this, I believe the best approach is to create an array of objects. However, my knowledge of objects and classes is a bit outdated, so I am unsure if I am structuring it correctly. Below is the HTML code for reference:

<div class="col-md-6 col-lg-4 col-sm-12">
    <img id="winnerImage" src="http://placehold.it/1500x1200" alt="Winners at Rhythm City Casino Resort&reg;" width="1500" height="1200" class="img-thumbnail img-fluid">
</div>
<div class="col-md-6 col-lg-8 col-sm-12">
    <h2 class="display-3" id="winnerHeader">Another Big Winner at Rhythm City!</h2>
    <h3 id="winnerCaption">Debbie R. &bull; $5,000&nbsp;Winner.</h3>
    <p class="text-center"><a href="winners.html" class="btn btn-primary">View Winners Gallery</a></p>
</div>

Here is the code from my script file:

window.onload = choosePic;

function BigWinner(source, name, amount) {
    this.source = source;
    this.name = name;
    this.amount = amount;
}

var winnersArray = new Array (
    BigWinner("JohnD_1280.40_DeWittIA_September2017.JPG", "John D.", "$1,280"),
    BigWinner("KaronB_13004.81_DavenportIA_September2017.JPG", "Karon B.", "$13,004"),
    BigWinner("KyleG_1742_NewBostonIL_September2017.JPG", "Kyle G.", "$1,742"),
    BigWinner("MarciaP_2000.40_LeClaireIA_November2017.JPG", "Marcia P.", "$2,000"),
    BigWinner("ShaneE_4164,76_CamancheIA_September2017.JPG", "Shane E.", "$4,164")
);

function choosePic() {
    randomNum = Math.floor((Math.random() * winnersArray.length));
    document.getElementById("winnerImage").src = "img/Casino/Winners/" + winnersArray[randomNum].source;
    document.getElementById("winnerCaption").innerHTML = winnersArray[randomNum].name + " &bull; " + winnersArray[randomNum].amount + " Winner";
}

Answer №1

There are a couple of improvements that can be made to your code. Firstly, your array syntax can be simplified. Additionally, remember to use the new keyword when creating new instances of BigWinners.

var winnersArray = [
    new BigWinner("JohnD_1280.40_DeWittIA_September2017.JPG", "John D.", "$1,280"),
    new BigWinner("KaronB_13004.81_DavenportIA_September2017.JPG", "Karon B.", "$13,004"),
    new BigWinner("KyleG_1742_NewBostonIL_September2017.JPG", "Kyle G.", "$1,742"),
    new BigWinner("MarciaP_2000.40_LeClaireIA_November2017.JPG", "Marcia P.", "$2,000"),
    new BigWinner("ShaneE_4164,76_CamancheIA_September2017.JPG", "Shane E.", "$4,164")
];

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for including assisting text in the date field with Material UI

Behold, my component definition: <DateField name={formElement.name} type="date" label={formElement.name} onChange={(date) => formik.setFieldValue(formElement.name, date)} value={formik.values[formElement.name] ...

The error message "Each item within a list must be assigned a unique 'key' prop" is being displayed

At the moment, I'm immersed in a project that utilizes React, Next.js, and Ant-Design. However, during the development process, I encountered an error due to the absence of a unique key like so: Here's the detailed log of the error: Warning: Ea ...

width of the cells within the grid table

What is the best way to ensure the width alignment of cells in both the header and main section? I have highlighted the correct option in the image with green checkmarks. Check out the image here. Here is my example and solution: View the code on CodePen. ...

What is the best way to display the value of a PHP variable in a JavaScript pop-up window?

Here are the scripts I have. A user will input a numerical value like 123 as a parameter in the URL, and the application will retrieve that value from MySQL and display it in the textarea. For example, if you enter "example.com/index.php?id=123" in the UR ...

What is the process for obtaining intersection set data from an array?

I'm trying to find the intersection set within an array only containing type 1 and 2. Can you help me with that? var arr = [ { id: 1, auths: [ { authId: 1, type: 1, value: 'Test1' }, { authId: 2, type: 1, ...

When a ng-model is added, the input value disappears

i am currently working on a form that contains angular values. <tr ng-repeat="alldata in c.da"> <td>{{alldata.id}}</td> <td><input type="text" class="form-control" value="{{alldata.name}}" ...

Here's a guide on how to display texts underneath icons in Buttons using Material UI

Currently, this is the Button I have displayed https://i.sstatic.net/YkHp0.png I am trying to figure out how to position the Dummy Button text beneath the icon. Can someone assist me with this? Below is my code snippet: <Button className={classes.d ...

Create a compile-time array that is the result of adding two other compile-time arrays together

Imagine having two constexpr arrays (type[N] or std::array<type, N>) constexpr int X[5] { 9, 8, 7, 6, 5 }; constexpr int Y[5] { 1, 2, 3, 4, 5 }; Would you be able to create a new constexpr array by applying an element-wise operation (or constexpr f ...

store multiple names in an array of strings

A challenge has been presented: Write a class named Seyyed that includes a method named seyyed. The task involves saving the names of various people in a String array within the main method and determining how many names begin with "Seyyed". An attempt has ...

Integrating Braintree with Angular for accepting Android Pay transactions

Currently facing a challenge with Braintree that I need help resolving. I have successfully set up Braintree to generate my client_token using my API, and created the drop-in feature as a test. Here is how I implemented it: (function () { 'use st ...

Mastering the Art of Writing an Ajax Post Request

I am attempting to transmit an image URL to a node.js server from some JavaScript using the Ajax POST method. My expectation is for the server to respond with some text, but I'm encountering issues and unsure where the problem lies. Below is the relev ...

Executing a JS function within a table cell

I've been attempting to invoke a function within a table cell to dynamically create some data, but I keep encountering this error below. I'm uncertain whether I'm correctly calling defined functions and JavaScript functions inside the table ...

Ways to verify if the user has inputted a typeahed value

My code snippet looks like this: var students = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('fullName'), queryTokenizer: Bloodhound.tokenizers.whitespace, remote: { ...

Interrogating a multi-dimensional array using the MongoDB C# driver

Here is how my document is structured: "ID" : "fruit1", "Keys" : [ ["apple", "carrot"] ["banana"] ] I am trying to query for Keys = "carrot" using MongoDB C# driver. Can anyone help with this? When I tried in shell, this ...

Populate a JSON table in React with checkboxes and automatically mark them based on the JSON data

I'm currently working on creating a React table using JSON data like this: [ { "Id_side": 123, "Name_side": "R4", "Name_cycle": "C1" }, { "Id_side": 345, "Name_side": "M1", "Name_cycle": "C2" ...

Exploring the power of NodeJS modules through exports referencing

Encountering difficulties referencing dynamic variables related to mongoose models based on user session location value. Two scripts are involved in this process. The first script is called location.js & reporting.js In the location.js script: module.ex ...

What is the best way to pass data from a child component to its parent in React?

I have a dynamic table where users can select items from a dropdown menu and add them to the list. Currently, I store the item list in the component's state to render the table dynamically. I want users to be able to click on an item in the table and ...

Utilizing SequelizeJS to Pass an Array of Values

I am currently exploring the possibility of passing an array as the value for the property of an instance. In my model, I have set the dataType to STRING and I am inserting values from jQuery fields into an array which I then parse from the body and assign ...

What are some ways to conceal the API connection within a button?

I have a code that allows me to perform a certain API call with a link. Here is an example: <a class="btn btn-default" href="https://testapi.internet.bs/Domain/Transfer/Initiate?ApiKey='.$user.'&Password='.$pass.'&Domain=&ap ...

Engage with Vue.js and traditional JavaScript (plain) in your projects

Exploring the integration of Vue with regular JavaScript has presented a challenge. While some solutions online propose moving all functions to a Vue component, this approach proves impractical for complex or large functions. The task now is finding a way ...