0

I'm new to JS and would like to know how to refactor this simple code so that I can pass in strings to count the number of "e" in a string.

function countE() {
  var count = 0;
  var str = "eee";
  var charLength = str.length;

  for (i =0; i <= charLength; i++){
      if(str.charAt(i) == "e"){
          count++;
      }
  }
   console.log(count);
}

I would like to execute this function where I can do something like this:

countE('excellent elephants');

which would log 5.

7
  • Are you asking how can you pass arguments to a function? Commented Mar 30, 2014 at 20:04
  • Yes, that's exactly what I'm trying to do. Commented Mar 30, 2014 at 20:04
  • 2
    developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions Commented Mar 30, 2014 at 20:05
  • 1
    function countE(str) Commented Mar 30, 2014 at 20:06
  • 1
    if you want to make your function shorter: function countE(str) { return str.match(/e/g).length; } Commented Mar 30, 2014 at 20:12

4 Answers 4

2
function countE(str) {
  if(typeof(str)==='undefined') str = 'eee';
  var count = 0;
  var charLength = str.length;

  for (i =0; i <= charLength; i++){
      if(str.charAt(i) == "e"){
          count++;
      }
  }
  console.log(count);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Now you can pass an argument or have the function assume 'eee' if no argument is given
You could also use the cleaner str = str || "eee" but I thought that the if(typeof(str)==='undefined') str = 'eee'; was easier to understand
1

If you want to make your function body shorter, you can do the following:

function countE(str) { 
    return str.match(/e/g).length; 
}

And even more sophisticated:

function count(what) {
    return function(str) {
        return str.match(new RegExp(what, 'g')).length;
    };
}

// now you can do the this
var countE = count('e');
var resultE = countE('excellent elephants');

var countL = count('l');
var resultL = countL('excellent elephants');

3 Comments

I think you are over-complicating things for a beginner with your delegates.
I want to give him some inspiration what is possible with javascript. I know it's too much for a beginner, but maybe in a few days/weeks he goes back here and then learns something new.
Fair enough, though it might be better with just another parameter. I don't see the point unless you are actually using the delegate.
0

If I understand your comment correctly, you want to do something like this:

function countE(inString) {
  var count = 0;
  var str = inString ? inString : "eee";
  var charLength = str.length;

  for (i =0; i <= charLength; i++){
      if(str.charAt(i) == "e"){
          count++;
      }
  }
   console.log(count);
}

Comments

0

You can also use a regular expression

function countE(str) {
   var count = str.match(/e/g).length;
   console.log(count);
}

or

function countE(str) {
   console.log(str.match(/e/g).length);
}

Comments

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.