Attempting to refresh the Document Object Model (DOM) by incorporating sorted numbers and representing them visually with height bars using

Currently, I am working on creating a visualization of height bars with 40 randomly generated values similar to a sorting visualizer. I have implemented mergesort for sorting the values. Initially, when updating the DOM for the first time, I generated random values one by one in a loop and then updated them in the DOM using CSS heights.

However, I already have 40 sorted numbers in a list. So, my question is how can I update the DOM for each index to display the values using CSS height? You can refer to the image of the randomly generated numbers provided below to understand what I am aiming to achieve. In the case of random number generation, I could create them one by one. But with 40 pre-sorted values, I am struggling to figure out how to implement this for each index.

When I click the mergeSort button, the DOM gets cleared using .empty but then it fails to display anything or only shows the last sorted value for all 40 bars (e.g. 190 190 190...). How can I make sure that each index is displayed correctly and stop after they are sorted once?

Answer №1

In the code provided, the variable i remains constant and does not increment, causing all bars to have the same height based on the last value in the Sorted array.

To ensure each bar has a different height, set i to 0 before the loop and increment it for each iteration.

Be sure that the number of elements with the class .frequencyItem matches the length of the Sorted array.


i = 0;
$('.frequencyItem').each(function()
{
   x = Sorted[i++];
   $(this).css("height", x);
});

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

Retrieve the outcome of an Ajax request

Hey there, I have a situation where I need to submit a form and make an ajax call. Here's what I'm doing: $('#pForm').on('submit', function(e) { e.preventDefault(); //Prevents default submit var form = $(this); var post ...

Adding the p5.js library to Stackblitz IDE: A step-by-step guide

Recently, I've been using Stackblitz as my IDE to improve my coding skills on my Chromebook. While it has been effective in many ways, I have encountered difficulties when trying to integrate the p5 library. As a beginner in programming, I only grasp ...

Setting values for a volatile array in C++

My dilemma involves managing a dynamic array of MyType elements stored in shared memory, which has been set up using CreateFileMapping and similar functions: volatile MyType *arr; The challenge I'm facing is how to successfully assign a value to an ...

Bootstrap 4 input fields extend past padding boundaries

I have created a basic form using the following code: <div class="container"> <div class="jumbotron" style="width:100%"> <h1> ConfigTool Web Interface </h1> <p> Edit the con ...

PHP: eliminate key from $_POST array

Similar Question: How to Remove a Specific Element from an Array in PHP I'm facing a problem with my $_POST array where I need help trimming or removing the key 'submit'. I find array manipulation confusing at the moment. Any assistance ...

Preventing the <img> element from being selectable and draggable simultaneously is effective in Firefox, but not in Chrome or Opera

I have a picture that I want to render both unselectable and undraggable. (This is not an attempt to prevent users from copying) To accomplish this, I can: Add a .noselect class to the <img> tag. As recommended here. Disable pointer-events. (I am ...

Looking for assistance with extracting only numerical values from a webpage using Selenium in Python

Website Elements <td style="font-size:20px;font-family:LucidaGrande,tahoma,verdana,arial,sans-serif;padding:10px;background-color:#f2f2f2;border-left:1px solid #ccc;border-right:1px solid #ccc;border-top:1px solid #ccc;border-bottom:1px solid #ccc; ...

Most effective approach to setting up a timer using Javascript and PHP

I'm currently working on a quiz application using PHP and Javascript. The quiz begins once the user clicks a submit button, but I want it to end after a specific duration that I define. I'm hesitant to rely solely on the client-side clock as it c ...

When utilizing the getIntersectionList function, IE 9 may experience a malfunction and cease functioning

I am currently working with SVG code and JavaScript, trying to achieve a specific intersection result. <svg id="svgSurface" width="500" height="500"> <defs> <marker id="Triangle" viewBox="0 0 20 20" refX="0" refY="0" markerUnits ...

Ways to set the initial value of an input[range] in Angular2 when the value exceeds 100

After encountering a similar issue with AngularJS, I posted a question on StackOverflow titled How to initialize the value of an input[range] using AngularJS when value is over 100. As I dive into learning Angular2, I am curious if it handles initializatio ...

Explain and suggest the necessary parameters for a function

Is there a way to clearly describe the parameters required by my function and make them visible when I am typing my code? For instance, if we consider the ExpressJS render function below, it effectively shows what the callback expects and will return. In ...

What can be done to prevent function from being treated as instance members within a controller's scope?

When using angularJS 1.3, the following code snippet showcases the functionality: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="http ...

Unable to reposition multiple THREE.Mesh objects in an array due to them being labeled as "read only" and "undefined"

Currently, I am delving into the world of three.js and managed to create a function that generates a 3x3x3 grid made of cubes. let cubes = []; function createGrid3x3(gridMaterial, positionZ) { for( let k = 1; k < 4; k++) { for( let j = 1; j ...

The x-axis values in Amchart are transitioning rather than shifting

I'm facing an issue with my x-axis values as I'm using real-time amcharts. The x-axis values change every 3 seconds, but instead of smoothly moving, they abruptly change to the next value. I would like it to slide smoothly like this example: htt ...

Obtain the document from the jQuery AJAX response

I am currently working on generating a word document on the server side in response to an ajax event. If there are no errors, I would like to trigger a download of the file. Is it possible to achieve this with jQuery ajax? My current code snippet looks l ...

Encountering a problem while trying to launch the development server for a React application using the npm-start

I followed the steps to create a react application using npx create-react-app myapp, but when I use npm start, it fails to start a developer server. Even after trying to reinstall the node_modules, the issue persists. I am using the latest versions of Reac ...

Can a graph effectively showcase data across a sporadically ordered timeline?

I have a collection of data stored in a database. While I have come across jpgraph during my research, I found that it may not be ideal for handling random time-sequencing and I am not a fan of their gantt chart layout. Do you have any recommendations for ...

Is it acceptable to incorporate Node.js modules for utilization in Next.js?

Here's a funny question for you. I am trying to generate UUID in my Next.js project without adding any unnecessary packages. So, I decided to import crypto in my component like this: import crypto from 'crypto'; After importing it, I used i ...

Update the div and table without waiting for a specific time interval when an event happens

Currently, I have a table within a div that includes both editing and deleting functionality for records. After deleting a record, I want the table to be automatically updated with fresh data without having to reload the page. Can someone please provide me ...

How can you change the width of <td> and <th> elements?

I am trying to keep a table header fixed at the top of a window, but for some reason, setting the widths of the <td> and <th> elements as shown below is not resulting in them having the same width (the column headers are misaligned with other c ...