rissa146 Posted January 14, 2016 Share Posted January 14, 2016 I need to Create a function that takes a string as its input and returns the number of vowels that appear in the string. Can you write the steps for completing this action or provide a youtube video that does? Link to comment Share on other sites More sharing options...
davej Posted January 14, 2016 Share Posted January 14, 2016 Have you looked at the tutorial? http://www.w3schools.com/js/default.asp The obvious approach would be to step through the string and accumulate totals. var str = "zquiabcdefghijklmnopqrstuvwxyzzzzui"; function countVowels(str){ var i; var len = str.length; var vowel_cnt = 0; var ch; for(i=0 ; i<len ; i++){ ch = str.charAt(i).toUpperCase(); if ((ch == 'A')||(ch == 'E')||(ch == 'I')||(ch == 'O')||(ch == 'U')){ vowel_cnt++; } }//end for return vowel_cnt; }//end of function Link to comment Share on other sites More sharing options...
rissa146 Posted January 17, 2016 Author Share Posted January 17, 2016 Thanks. I tried this var str = "clarissa" function someStr(str){ var i; var vLent = str.length var cntArr = 0 var ch; for (i=0, i<vLent, i++){ ch str.charAt(i).toUpperCase(); if ((ch == 'A') || (ch == 'E') || (ch == 'I') || (ch == 'O') || (ch == 'U')) { cntArr++; } } return cntArr; } I know there is an error. I just don't know where. I am very very new at this coding and any point in the right direction will help Link to comment Share on other sites More sharing options...
rissa146 Posted January 17, 2016 Author Share Posted January 17, 2016 I just saw my error. var str = "clarissa"; function someStr(str){ var i; var vLent = str.length; var cntArr = 0; var ch; for (i=0; i<vLent; i++){ ch = str.charAt(i).toUpperCase(); if ((ch == 'A') || (ch == 'E') || (ch == 'I') || (ch == 'O') || (ch == 'U')) { cntArr++; } } return cntArr; } //to call function someStr(str) Link to comment Share on other sites More sharing options...
rissa146 Posted January 17, 2016 Author Share Posted January 17, 2016 someStr(str) <-3 Link to comment Share on other sites More sharing options...
rissa146 Posted January 17, 2016 Author Share Posted January 17, 2016 Thanks For your code function countVowels(str){var i;var len = str.length;var vowel_cnt = 0;var ch; You are passing a string into the array called len and then increment by 1. is this correct? Link to comment Share on other sites More sharing options...
davej Posted January 17, 2016 Share Posted January 17, 2016 Are you having some problem with this? The following seems to work... <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"/> <title>title</title> <style> </style> <script> window.onerror = function(a,b,c){ alert('Javascript Error: '+a+'\nURL: '+b+'\nLine Number: '+c); return true; } </script> </head> <body> <div id="out"> </div> <script> 'use strict'; var mystr = "clarissaeiou"; function someStr(str){ var i; var vLent = str.length; var cntArr = 0; var ch; for (i=0; i<vLent; i++){ ch = str.charAt(i).toUpperCase(); if ((ch == 'A') || (ch == 'E') || (ch == 'I') || (ch == 'O') || (ch == 'U')) { cntArr++; } } return cntArr; }//end of func document.getElementById('out').innerHTML = 'Number of vowels in ['+ mystr +'] = ' + someStr(mystr); </script> </body> </html> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now