calyxofheld
calyxofheld

Reputation: 2128

function that checks the number of times a character is used in a string

I'm writing a function that takes a string as an argument, checks it for a given character (say "B" in this case), and then returns an integer that reflects the number of times that character appeared. I'm aware that this can be done using regex and such, but the tutorial I'm using has so far made no mention of regex. Code time:

function countBs(string) {
  var i = 0;
  var n = 0;
  var position = string.charAt(n);

  while (i < string.length) {
    if (string.charAt(n) == "B")
      n += 1;
      i++; //This line causes the following else statement to throw a syntax error. But it's the only way I can think of to have the loop continue iteration *while* checking for equivalence to "B" 
    else
      i++;
    return n;
  }

}

And then check with console.log(countBs("ABBA"));

Upvotes: 0

Views: 94

Answers (3)

Victor Oni
Victor Oni

Reputation: 379

Here's my answer

function countBs(Str) 
{
  let char = "B" ;
  return  String(Str).split(char).length - 1; 

 } 
function countChar(Str, char) 
{
  return  String(Str).split(char).length - 1; 

 } 

Upvotes: 1

user663031
user663031

Reputation:

Your code is quite broken.

function countBs(string) {
  var i = 0;
  var n = 0;
  // var position = string.charAt(n); // REMOVE--NOT NECESSARY

  while (i < string.length) {    
    if (string.charAt(i) == "B")      // i, NOT n
      n++;                            // CONSISTENCY IN ADD-ONE SYNTAX
      // i++;                         // INCREMENT ONCE BELOW
    //else
      i++;                   
  }
  return n;                           // MUST GO OUTSIDE THE LOOP
}

Correct code would therefore be:

function countBs(string) {
  var i = 0;
  var n = 0;

  while (i < string.length) {      
    if (string.charAt(i) == "B") n++;
    i++;
  }
  return n;                        
}

There's nothing particularly wrong with using a while loop, but a for would be more natural:

function countBs(str) {
  var n = 0;
  for (var i = 0; i < str.length; i++) if (str[i]== "B") n++;
  return n;                        
}

Modern JS

For your reference, in modern JS, you could avoid the loops and variables. First, let's write a separate checking function:

function isB(c) { return c === 'B'; }

Then write

function countBs(str) {
    return str . split('') . filter(isB) . length;
}

or, using reduce:

function countBs(str) { 
    return str.split('').reduce(function(cnt, c) {
        return cnt + isB(c);
    }, 0);
}

or, although you said you didn't want to use regexps:

function countBs(str) {
    return (str.match(/B/g) || []) . length;
}

If you are writing in an ES6 environment, then using array comprehensions

function countBs(str) {
    return [for (c of str) if (isB(c)) c] . length;
}

Upvotes: 1

Ayushi Jha
Ayushi Jha

Reputation: 4023

Try wrapping it in curly braces:

if (string.charAt(n) == "B")
  {    n += 1;
       i++;
  } 

An else requires a previous if, and no other statements in between. i++ was outside the if.

Upvotes: 1

Related Questions