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 ( || ).
Sample Input 1
['teacher', 'friend', 'cricket', 'farmer', 'rose', 'talent', 'trainer']
t
r
Sample Output 1
[ 'teacher', 'farmer', 'talent', 'trainer' ]
Sample Input 2
['dream', 'player', 'read', 'write', 'trend']
p
d
Sample Output 2
[ 'player', 'read', 'trend' ]
i want code in between write code here
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});
process.stdin.on("end", (_) => {
inputString = inputString.trim().split("\n").map((str) => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
/* Please do not modify anything above this line */
function main() {
const stringsArray = JSON.parse(readLine().replace(/'/g, '"'));
const startString = readLine();
const endString = readLine();
/* Write your code here */
}
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});
process.stdin.on("end", (_) => {
inputString = inputString.trim().split("\n").map((str) => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
/* Please do not modify anything above this line */
function main() {
const stringsArray = JSON.parse(readLine().replace(/'/g, '"'));
const startString = readLine();
const endString = readLine();
/* Write your code here */
const outputString = stringsArray.filter( item => item[0] == startString || item[item.length - 1] == endString );
console.log(outputString);
}
Comments
Leave a comment