Sunday, April 18, 2021

35. Write a JavaScript program to check a given string contains 2 to 4 numbers of a specified character.

35. Write a JavaScript program to check a given string contains 2 to 4 numbers of a specified character.


function check_char(str, char)
 {
  ctr = 0;
  for (let i = 0; i < str.length; i++)
  {
    if (str.charAt(i) == char) {
      ctr++;
    }
  }
  return (ctr >= 2 && ctr <= 4);
}

console.log(check_char("Python", "y"));
console.log(check_char("JavaScript", "a"));
console.log(check_char("Console", "o"));

No comments: