Loop function not working for vowel counter. How can i make a counter text bold

Matthew

My loop function for my vowels counter in not working correctly and I don't know where I have gone wrong I would like some assistance with it.

I am trying to get the most used vowel to be bold so say I have A = 34 and E = 45 so E should be bold like this E

But I don't know whether I am missing some code or if I have got something wrong in my code.

This is my JavaScript.

 function countVowels() {

     var text = document.getElementById("text").value;

     var arrayOfLetters = text.split("");

     // These are the counters for the program to find the vowels.
     var countA = text.match(/[Aa]/g).length;
     var countE = text.match(/[Ee]/g).length;
     var countI = text.match(/[Ii]/g).length;
     var countO = text.match(/[Oo]/g).length;
     var countU = text.match(/[Uu]/g).length;
     var countComma = text.match(/[,.!": ;?)(]/g).length;

     var bold = "<strong>";
     var vowels = new Array();
     vowels[0] = "countA";
     vowels[1] = "countE";
     vowels[2] = "countI";
     vowels[3] = "countO";
     vowels[4] = "countU";

     for (var i = 0; i < vowels.length; i++) {
         document.getElementById("result").innerHTML = i + vowels[i] + "<br />";

     }
     // This code will output the results.
     document.getElementById("result").innerHTML = "";
     document.getElementById("result").innerHTML += "Total Letters: " + arrayOfLetters.length + "<br />";
     document.getElementById("result").innerHTML += "A's: " + countA + "<br />";
     document.getElementById("result").innerHTML += "E's: " + countE + "<br />";
     document.getElementById("result").innerHTML += "I's: " + countI + "<br />";
     document.getElementById("result").innerHTML += "O's: " + countO + "<br />";
     document.getElementById("result").innerHTML += "U's: " + countU + "<br />";
     document.getElementById("result").innerHTML += "Punctuation: " + countComma + "<br />";
 }

This is my HTML.

<div style="text-align: center;">
    <h1> Vowel Counter </h1>
    Please enter text for your vowel count:
    <br>
    <textarea id="text" rows="10" style="width: 100%;"></textarea>
    <br>
    <button onclick="countVowels();">Count Vowels</button>
    <p id="result"></p>

And this is my fiddle.

Alphonso

You seem to be getting the count right. So the only problems left I guess are:

  1. Getting max count
  2. Displaying it in bold

I referred to this to get max count, and then some tweaks to your code to get the max count displayed in bold.

var vowels = new Array ();
vowels [0] = {"vowel" : "A", "count" : countA};
vowels [1] = {"vowel" : "E", "count" : countE};
vowels [2] = {"vowel" : "I", "count" : countI};
vowels [3] = {"vowel" : "O", "count" : countO};
vowels [4] = {"vowel" : "U", "count" : countU};
var maxCount = Math.max.apply(Math, vowels.map(function(o) {
    return o.count;
}));

for (var i = 0; i < vowels.length; i++) {
    document.getElementById("result").innerHTML += (vowels[i].count == maxCount? "<B>" : "") + vowels[i].vowel + "'s: " + vowels[i].count + "<br />";
}

See this updated fiddle.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I improve this vowel counter function for higher efficiency?

Vowel counter in a text file

Vowel counter not working in Pure js

How can I make a counter in HTML/CSS

How can I make text bold in Python?

How can I make part of the text bold?

How do I use a for loop counter as the input to a function?

Using a loop counter how can I dynamically create objects and use the counter in naming the objects?

How can I increment a counter only for working hours?

How can i use jar file into loop counter - Jmeter

How can i make a time counter using QTime()

How can i make a different counter for each photo in js?

How can I make a number counter app using textfield in flutter?

how can i make a unique request for `counter_cache`

How can I make this click counter work properly?

How can I make a counter conditionally on the rows? R

How can I make this program display a counter a different way?

How can I make a counter based on the amount of triggers

How can I make a correct answer counter in Python?

how can I reset the counter?

How to make an onscreen counter that blocks the function till a counter is done

How can I get a python counter to work in a function with multiple if statements?

Counter is working, but how to make it list counted values?

How to make a for loop break on counter in Django Templates?

Can I add a "Counter" within a FOR loop?

Is there a way i can make a counter in batch?

How to make a counter for healt and score in UI text

How do I keep a counter of a nested loop?

How do i reset Object loop "counter"?