Answer in Web Application for Chandra sena reddy #167130
October 24th, 2022
JavaScript
Split and Replace
Given three strings
inputString, separator and replaceString as inputs. Write a JS program to split the
inputString with the given separator and replace strings in the resultant array with the replaceString whose length is greater than 7.
Quick Tip
- You can use the string method split()
- You can use the array method map()
Input
- The first line of input contains a string inputString
- The second line of input contains a string separator
- The third line of input contains a string replaceString
Output
- The output should be a single line containing the strings separated by a space
Sample Input 1
JavaScript-is-amazing
–
Programming
Sample Output 1
Programming is amazing
Sample Input 2
The&Lion&King
&
Tiger
Sample Output 2
The Lion King
let inputString = prompt('Enter input string', '');
let separatorString = prompt('Enter separator', '');
let replaceString = prompt('Enter replace string', '');
separator(inputString, separatorString, replaceString);
function separator(inputString, separatorString, replaceString ) {
let arr = inputString.split(separatorString);
let res = arr.map(item => item.length > 7 ? item = replaceString : item = item);
alert(res.join(' '));
}