w3resource

JavaScript: Check whether a given string contains equal number of p's and t's

JavaScript Basic: Exercise-55 with Solution

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

This JavaScript program checks if a given string has an equal number of 'p' and 't' characters. It counts the occurrences of each character in the string and then compares the two counts to determine if they are equal.

Visual Presentation:

JavaScript: Check whether a given string contains equal number of p's and t's

Sample Solution:

JavaScript Code:

// Define a function named equal_pt with parameter str
function equal_pt(str) { 
  // Replace all characters except 'p' with an empty string and store the result in str_p
  var str_p = str.replace(/[^p]/g, "");

  // Replace all characters except 't' with an empty string and store the result in str_t
  var str_t = str.replace(/[^t]/g, "");

  // Get the length of str_p and store it in p_num
  var p_num = str_p.length;

  // Get the length of str_t and store it in s_num
  var s_num = str_t.length;

  // Return true if the lengths of str_p and str_t are equal; otherwise, return false
  return p_num === s_num;
}

// Log the result of calling equal_pt with the given strings to the console
console.log(equal_pt("paatpss"));
console.log(equal_pt("paatps"));

Output:

false
false

Live Demo:

See the Pen JavaScript - Check whether a given string contains equal number of p's and t's - basic-ex-55 by w3resource (@w3resource) on CodePen.


Flowchart:

Flowchart: JavaScript - Check whether a given string contains equal number of p's and t's

ES6 Version:

// Define a function named equal_pt with parameter str
const equal_pt = (str) => {
    // Use regular expressions to replace characters other than 'p' with an empty string
    const str_p = str.replace(/[^p]/g, "");

    // Use regular expressions to replace characters other than 't' with an empty string
    const str_t = str.replace(/[^t]/g, "");

    // Get the lengths of the modified strings
    const p_num = str_p.length;
    const s_num = str_t.length;

    // Return whether the lengths of the 'p' and 't' strings are equal
    return p_num === s_num;
};

// Log the result of calling equal_pt with the given strings to the console
console.log(equal_pt("paatpss"));
console.log(equal_pt("paatps"));

Improve this sample solution and post your code through Disqus.

Previous: JavaScript program to count the number of vowels of a given string.
Next: JavaScript program to divide two positive numbers and return a string with properly formatted commas.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/javascript-exercises/javascript-basic-exercise-55.php