-1

I am doing JavaScript courses and got stuck at this question.

Define a function named countLetters that receives two parameters:

1) sentence  - type: string
2) character - type: string

The function should return a count of every character present in sentence.

HINT: You can access a single character in a string just like accessing an element in an array - myString[3] would access the third character

Here is what I have done so far.

function countLetters(sentence, character) {
 for(let i=0; i <= sentence.length; i++) {
  let count = character[i];
  return sentence.match(count).length;
 }
}

Thank you!

Yangshun Tay
  • 49,270
  • 33
  • 114
  • 141
B.Lee
  • 9
  • 1
  • So what is your question – Ngo Tuan Jan 08 '18 at 07:25
  • `if (same_character(the_character_to_find, the_current_character)) then increment_counter` .. and then return the counter after looking at *all* characters. *Do not* call any string methods/properties, except for `length` and `str[x]` to solve this. (While it could be done with `match`, attempting to use it in the loop indicates lack of basic principals .. get those down first :}) – user2864740 Jan 08 '18 at 07:25
  • Possible duplicate of [Count the number of occurrences of a character in a string in Javascript](https://stackoverflow.com/questions/881085/count-the-number-of-occurrences-of-a-character-in-a-string-in-javascript) – Rex Low Jan 08 '18 at 08:29

4 Answers4

0

Traverse through the sentence and increase the counter if the character is found

function countLetters(sentence, character) {

 var count = 0
 for(let i=0; i < sentence.length; i++) {
     if (sentence.charAt(i) === character)
          counter++;    
 }

 return counter 

}
Salman A
  • 262,204
  • 82
  • 430
  • 521
Charlie
  • 22,886
  • 11
  • 59
  • 90
0

function countLetters(sentence, character) {
var count = 0;
 for(let i=0; i <= sentence.length; i++) {
  if(character == sentence[i]){
    count++;
  }
 }
 return count;
}

console.log(countLetters("The quick brown fox jumps over the lazy dog","o"));

You can access a single character in a string just like accessing an element in an array

It means you can use access each character of string like "sentence[i]" and then compare with the desired character like character == sentence[i].

Lalit
  • 1,354
  • 1
  • 8
  • 13
0

The algorithm is simple. Maintain a local variable to count the number of occurrences of a letter. As you loop through each character in the sentence, if it matches character, increment the counter. Lastly, return the value of the counter

function countLetters(sentence, character) {
  let count = 0;
  for (let i = 0; i < sentence.length; i++) {
    if (sentence[i] === character) {
      count++;
    }
  }
  return count;
}

console.log(countLetters('apple', 'p')); // 2
console.log(countLetters('watermelon', 'e')); // 2
console.log(countLetters('mississippi', 's')); // 4

Here's a one-liner just for fun:

function countLetters(sentence, character) {
  return sentence.split('').reduce((acc, char) => acc + Number(char === character), 0);
}

console.log(countLetters('apple', 'p')); // 2
console.log(countLetters('watermelon', 'e')); // 2
console.log(countLetters('mississippi', 's')); // 4
Yangshun Tay
  • 49,270
  • 33
  • 114
  • 141
-1

Considering that only one character needed to be checked for count in a string.

This code can help.

 var mstring = My name is jhone doe;
 var mchar = o;
 function countLetters(string, char){
     var count = 0;
     if(string[i] == char){
        count++;
     }
     return count;
  }
 console.log(countLetters(mstring, mchar));

It will output 2. As o appears 2 times in the given string.

Lalit Sharma
  • 354
  • 2
  • 12