Answer to Question #265116 in HTML/JavaScript Web Application for swsq

Question #265116

String Slicing: Given two strings inputString and subString as inputs, write a JS program to slice the inputString if it includes the subString.Input: The first line of input contains a string inputString. The second line of input contains a string subString. Output: The output should be a sliced string or inputString (if the inputString does not include the subString)

.Follow the below approach to solve the String Slicing. Check Whether the input string includes the substring using if condition and includes().if the input string includes the substring then,Find the index of the substring in the input string, using the indexof() and store the index in a variable.slice the input string, using the slice() form index to the length of the input string and store in the variable slicedString. const slicedString = inputString.slice(index, inputString.length); console the variable sliceString.if the input string doesn't include the substring then else console the input string


1
Expert's answer
2021-11-13T01:18:15-0500
'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(string => {
 return string.trim();
 });
 
 main(); 
});


function readLine() {
 return inputString[currentLine++];
}


function main() {
 const inputString = readLine();
 const subString = readLine();


 let index = aqString.find(inputString, subString);
 if (isNaN(index) && index <= inputString.length) {
 console.log(inputString.split(index));
 }
}

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