A Guide to Adjusting Image Height with a Variable

I was tasked with creating a frequency count for each letter present in a sentence and then visualizing that frequency in a chart. To achieve this, I used a 1px by 1px gif image and stretched it to represent the normalized value (max 100px) based on the frequency of each letter. For instance, if I have the phrase "Cwm fjord bank glyphs vext quiz", each letter would be allotted 100px as every letter is utilized only once.

To accomplish this task, I created an array to track the occurrences of each word. By determining which word appears most frequently, I can establish the divisor required for each word. This divisor will then dictate the height of the image when it is generated.

function htmlChart() {
    var table = document.getElementById("table");
    input = document.getElementById("userInput").value;
    table.innerHTML = generateTable(input);
}

function generateTable(input) {
    var frequency = new Array(26);
    var letters = new Array(26);
    var freqPos = 0;
    var newInput = input.toUpperCase();
    var max = 0;
    var myHeight = 0;
    var test = 9000;
    var image = new Image();
    image.src = "orange.gif";
    for (i = 65; i < 91; i++) {
        //calculates the occurrence of each character and stores its value
        frequency[freqPos] = newInput.split(String.fromCharCode(i)).length - 1;
        freqPos++;
    }
    //identifies the letter with the highest occurrence
    for (i = 0; i < frequency.length - 1; i++) {
        if (frequency[i] > max) {
            max = frequency[i];
        }
    }

    table = input + "<table>";
    //first row 
    table += "<tr>";
    table += "<td>Letter Frequency 100px</td>";
    for (i = 0; i < frequency.length - 1; i++) {
        //utilizes myHeight to adjust the image height accordingly
        myHeight = (frequency[i] / max) * 100;
        table += '<td><img src = "orange.gif" id = "orange" alt = "25" height = myHeight + "px" width = "5"></td>';
    }
    table += "</tr>";
    //second row
    table += "<tr>";
    table += "<td></td>";
    for (i = 65; i < 91; i++) {
        table += "<td>" + String.fromCharCode(i) + "</td>";
    }
    table += "</tr>";
    table += "</table>";
    return table;
}

Although I've successfully created the frequency chart, I'm struggling with adjusting the height of the images according to the value stored in myHeight. Can someone provide guidance on how I can achieve this?

Answer №1

It appears that there is an issue with the concatenation. Please modify this

'<td><img src = "orange.gif" id = "orange" alt = "25" height = myHeight + "px" width = "5"></td>';

to

'<td><img src = "orange.gif" id = "orange" alt = "25" height="' + myHeight + 'px" width = "5"></td>';

Keep in mind that each string being concatenated should be enclosed in single quotes '.

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

Attempting to implement usedispatch hook in combination with userefs, however, encountering issues with functionality

I've been exploring the Redux useDispatch hook lately. I created a simple app for taking notes in a todo list format. However, I am facing an issue with useDispatch as it's not working for me and I keep encountering this error: Module build faile ...

Obtaining the base name of all subpaths within a given string

I'm fairly new to working with Vue and JS. Let's consider an array of objects where each object contains a "name" field, representing various processes or commands. For instance: /disks/tools/itools/odor/src/bin/lib/script.py -path /disks/tools/ ...

Transferring values from jQuery AJAX to Node.js

Is there a way to successfully pass a variable from jQuery to nodejs without getting the [object Object] response? I want to ensure that nodejs can return a string variable instead. $('.test').click(function(){ var tsId = "Hello World"; ...

Bring in a function by its name from the ts-nameof package that is not declared in the d.ts export

Recently, I came across a captivating package that caught my interest and I would love to incorporate it into my TypeScript application: https://github.com/dsherret/ts-nameof However, upon attempting to import the nameof function, I realized it was not be ...

Unable to access cordova.js for Cordova vibration feature

I'm attempting to create a basic Cordova app for Android that involves vibrating the phone when a button is clicked, but I can't seem to get it to work. I've been using PhoneGap and have included the cordova-plugin-vibration, but I keep gett ...

Efficiently rendering a million elements on an HTML canvas (and replicating the render from the server)

I'm currently working on developing an HTML application using a canvas as the base. The canvas will consist of a large grid, around 1500 x 700 in size, totaling to over 1 million cells. The main concern is how to efficiently render this grid without ...

Using Bootstrap Select with a callback function

I am currently working with 2 Bootstrap Select dropdowns. One dropdown contains a list of countries, while the other contains a list of states. The country list is static and loads when the page is loaded. On the other hand, the state list is populated dyn ...

choosing from dropdown menu items

Learn HTML <table> <tr> <td>ENTER PRINCIPLE AMOUNT</td> <td><input type="text" name="principle" size="7"></td> </tr> <tr> <td>ENTER RATE OF INTEREST</td> <td><input type="text" na ...

Tips for changing the state of a toggle button with JavaScript

I need help creating a toggle button. To see the code I'm working on, click here. In my JavaScript code, I am reading the value of a 'checkbox'. If the value is true, I add another div with a close button. When the close button is clicked, ...

Issue with displaying record attribute as a text variable

I'm currently attempting to retrieve the attribute odata.type from a record so that I can adjust my EditForm based on its value. Strangely, when I utilize a constant to achieve this and print it with console.log, it appears as follows: ƒ HWType(_ref ...

Switching out the month name with its corresponding numerical representation

I recently transformed the date format from "24 Feb 2014" to "Feb-24-2014" using this code snippet: var dateStart = date; var arr = dateStart.split(' '); console.log(arr[1]+"-"+arr[0]+"-"+arr[2]); Now I'm trying to figure out h ...

The CSS star rating system is malfunctioning

I'm struggling to implement a rating star system in my project. I have experimented with various code snippets, but none seem to be functioning properly. Take a look at the outcome: https://i.sstatic.net/g3DvH.png ...

Having trouble getting CSS absolute and relative positioning to work with my div slider carousel

I'm currently working on creating a slider carousel using VUE, where the CSS classes (3 divs) are looped through. However, I am facing an issue where every time a div fades out, the next slider creates a duplicate slider at the bottom, resulting in tw ...

Encountering a DiscordAPIError[10062] when attempting to retrieve user points from the database due to an unknown interaction

content: "Congratulations, you have been successfully verified!", ephemeral: true, }); } } else if (interaction.customId === "giverole") { const userPoints = await findUser(interaction.member ...

Collaborate by sharing local storage with other systems

One of my systems (x.x.x.x: 8000) creates a localstorage after logging in. Now, when a user interacts with another system (x.x.x.x: 8001) by clicking a specific button, the information stored in the initial system's localstorage (x.x.x.x: 8000) is nee ...

Unzipping a zip file using the Command Line

I'm encountering an issue with the JSON code not working on the server. It appears that this problem may be due to Node.js being version 14 on Heroku, while the latest version is 16. Everything runs smoothly on my local computer but throws an immediat ...

Jquery failing to execute when navigating to a new page using Ajax request

I am facing an issue with my Index page. When I directly run the PHP and jQuery combined file named one.php, everything works perfectly. However, when I try to call the same page via Ajax, it fails to run correctly. Sample Code: index.php <!doctype h ...

Guide on using jQuery to update data in a form upon submission

Below is the form code: <div id="myForm"> <form method="post" id="contact-form" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"> <div class="row uniform"> <div class="6u 12u$(medium)"&g ...

What's the best way to show floating point numbers in a concise format while also maintaining the ability to perform calculations within this Vue app?

I'm currently developing a compact calculator application using Vue 3 and implementing some custom CSS. For the most part, everything seems to be functioning correctly, except for when the results are long numbers that extend beyond the display limit ...

The Discord.js collector seems to have no intention of ending, completely disregarding its keys in order

Help Needed with Discord Message Collector Issue I encountered a problem with my code that assigns ranks on Discord. It seems that the message collector is not responding to keys that should stop it from collecting messages. Can anyone offer assistance in ...