JavaScript: Create a new string from a given string, removing the first and last characters of the string if the first or last character are 'P'
JavaScript Basic: Exercise-67 with Solution
Remove First/Last 'P' Characters in String
Write a JavaScript program to create a new string from a given string. This program removes the first and last characters of the string if the first or last character is 'P'. Return the original string if the condition is not satisfied.
The program creates a new string by removing the first and last characters if either is 'P'. If neither condition is met, the program returns the original string.
Visual Presentation:

Sample Solution:
JavaScript Code:
Output:
ython ython JavaScript
Live Demo:
Flowchart:

ES6 Version:
For more Practice: Solve these Related Problems:
- Write a JavaScript program that removes the first and last characters of a string if they are the letter 'P' (case-insensitive), and returns the modified string.
- Write a JavaScript function that checks if the first or last character of a string equals 'P' or 'p' and removes them accordingly.
- Write a JavaScript program that processes a string by conditionally stripping off the boundary 'P' characters, leaving the string unchanged if the condition is unmet.
Improve this sample solution and post your code through Disqus.
Previous: JavaScript program to display the city name if the string begins with "Los" or "New" otherwise return blank.
Next: JavaScript program to create a new string taking the first and last n characters from a given string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.