Q:

Write a JavaScript program to check whether a given string contains equal number of p's and t's

0

Write a JavaScript program to check whether a given string contains equal number of p's and t's.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

function equal_pt(str)
{ 
  var str_p = str.replace(/[^p]/g, "");

  var str_t = str.replace(/[^t]/g, "");

  var p_num = str_p.length;
  var s_num = str_t.length;

  return p_num === s_num;
         
}
console.log(equal_pt("paatpss"));
console.log(equal_pt("paatps"));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now