Jump to content

Help for Steps for Creating A Function


rissa146

Recommended Posts

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...