javascript - Need help debugging vowel counting JS -


this question exact duplicate of:

i need debug segment of code class , have fixed number of things i'm not sure why doesnt work. supposed count number of vowels in phrase , return them in div element believe. returns "undefined vowels"

here html

<!doctype html> <html> <!-- vowels.html --> <head>     <meta charset="utf-8">     <title>vowels</title>        <link rel="stylesheet" href="../css/easy.css">           <script src="vowels.js"></script> </head>  <body>     <header>         <h1>i'd buy vowel</h1>     </header>      <main>         <label>             type phrase here:             <input type='text' id='textbox'> </input>         </label>         <button id='countbtn' type='button'> <!--changed countbutton countbtn-->             count vowels (a,e,i,o,u)         </button>          <div id='outputdiv'>         </div>     </main>      <footer>         <hr>          <p>&copy; uo cis 111 2015 april&trade; llc</p>     </footer>    </body> </html> 

and here js

function countvowels() {     var textbox, phrase, i, plength, letter, vowelcount; //removed alert count vowels     textbox = document.getelementbyid('textbox'); //corrected spelling of   element     phrase = textbox.value;     phrase = phrase.tolowercase;  //switched lower case     for(i = 0; < phrase.length; i+= 1)  {         letter = phrase[i];         if (letter == 'a' ||  letter == 'e' || letter == 'i' || letter == 'o' ||  letter == 'u') { //fixed spelling of letter. added = in letter = 'e'             vowelcount = vowelcount + 1;            }     }     alert(vowelcount + ' vowels');     var outarea = document.getelementbyid('outputdiv'); //corrected     outputdiv instead of outputid , put document. in front of getelement     outarea.innerhtml = vowelcount + ' vowels in ' + phrase; }  function init(){     alert('init vowels');     var counttag = document.getelementbyid('countbtn'); //switched semi-   colon , condensed single line     counttag.onclick = countvowels; }  window.onload = init; 

here jsfiddle

you can use regexp slimmer code: http://jsfiddle.net/4o67u3js/

html:

<p id = "text">     lorem ipsum dolor sit amet. </p> <p id = "result"># of vowels: <span></span></p> 

js:

$(function() {     var vowelscount = $("#text").text().match(/[aeiou]/gi).length;     $("#result > span").html(vowelscount); }); 

here's more algorithmic solution. and, yes defines function on prototype , opposed practice can rewrite function imperatively.

plain js:

var str = "lorem ipsum dolor sit amet.";  string.prototype.vowelscount = function() {     var str = this.tolowercase(),         len = str.length,         index = 0,         vowels = ["a", "e", "i", "o", "u"],         count = 0;      for( ; index < len; vowels.indexof(str[index++]) !== -1 ? count++ : count);      return count; };  console.log(str.vowelscount()); 

Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -