Answer to Question #172144 in HTML/JavaScript Web Application for manikanta

Question #172144

String Starts or Ends with given String


Given an array

stringsArray of strings, and startString, endString as inputs, write a JS program to filter the strings in stringsArray starting with startString or ending with endString.


Quick Tip

You can use the array method filter() and logical operator OR ( || ).



function main() {

 const stringsArray = JSON.parse(readLine().replace(/'/g, '"'));

 const startString = readLine();

 const endString = readLine();


 /* Write your code here */

}


1
Expert's answer
2021-03-16T06:27:06-0400
function main() {
    const stringsArray = JSON.parse(readLine().replace(/'/g, '"'));
    const startString = readLine();
    const endString = readLine();
    /* Write your code here */

    const outputArray = stringsArray.filter( item => item.startsWith(startString) || item.endsWith(endString) );

    console.log(outputArray);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS