0

I have to make an app, that takes a user input field, and then populates a table, according to some calculations. However, I don't understant how can I do this calculations to appear as a result in the field of the table. Can someone help me please? Here is my code;

function countbase() {
  let count = 0;

  var input = document.getElementById('sequence').value;


  const inputLen = input.length;
  document.getElementById('bcount').value = GC_Content;
}
table,
th,
td {
  border: 1px solid black;
  border-collapse: collapse;
}
<h2>Calculater example</h2>

<label for="sequence">  Name field</label>
<br>
<br>
<textarea id="sequence" name="sequence" placeholder="Enter sequence" rows="2" cols="50">
    </textarea>
<br><br>
<button onclick="countbase();">Check </button>
<br><br>

<table style="width:100%" id="values">
  <tr>
    <th colspan="2">&nbsp;Results</th>
  </tr>
  <tr>
    <td align="center"><span><b>Lenght Count:</b></span></td>
    <td align="center"><span id="bcount"></span></td>

  </tr>
  <tr>
    <td align="center"><span><b>Word Count  :</b></span></td>
    <td><b><span id="GC"></span></b></td>
  </tr>
</table>

0

2 Answers 2

1

From your line:

document.getElementById('bcount').value = GC_Content;

Here GC_Content isn't defined, I guess you trying to set the inputLen variable.

That said, to change the content of a <span>, use innerHTML:
Change span text?

To get the 'word count', we can use split(' ') to create an array of the input string, then set the .length of that array as the content of the <span>

function countbase(){

  // Get input element
  const input = document.getElementById('sequence').value;
 
  // Set length
  document.getElementById('bcount').innerHTML = input.length;
  
  // Set word count
  document.getElementById('GC').innerHTML = input.split(' ').length;
}
table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
}
<h2>Calculater example</h2>
    
<label for="sequence">  Name field</label>
<br>
<br>
<textarea id="sequence" name="sequence" placeholder= "Enter sequence" rows="2" cols="50">
</textarea>
<br><br>
<button onclick="countbase();">Check </button>  
<br><br>

<table style="width:100%" id="values">
    <tr>
        <th colspan="2">&nbsp;Results</th>
    </tr>
    <tr >
        <td align="center"><span><b>Lenght Count:</b></span></td>
        <td align="center"><span id="bcount"></span></td>
        
    </tr>
    <tr>
        <td align="center"><span><b>Word Count  :</b></span></td>
        <td><b><span id="GC"></span></b></td>
    </tr>
  </table>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help,this works, but what if I want to add more functions, meaning that I want another function that counts vowels? should I add it to the function already created, or can I create another function apart from that?
0

You can use Array.split() method and then get length of result array let GC_Content = input.split(" ").length //splits input by spaces and count length of result array

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.