We need all two test cases can be came when code was run. I want exact outputs for all two test cases.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);
if the input string doesn't include the substring then else console the input string.
Input:
Language
air
JavaScript
S
Expected:
Script
input:
Language
air
Expected:
Language
let arr1 = ['Language', 'air', 'JavaScript', 'S'];
let arr2 = ['Language', 'air'];
stringSlicing(arr1);
stringSlicing(arr2);
function stringSlicing(arr) {
let res = '';
let resDefault = '';
for (let i = 0; i < arr.length; i = i + 2) {
let inputString = arr[i];
let substring = arr[i + 1];
if (inputString.includes(substring)) {
let index = inputString.indexOf(substring);
let slicedString = inputString.slice(index, inputString.length);
res += slicedString + ' ';
} else {
resDefault += inputString + ' ';
}
}
if (res.length == 0) {
res = resDefault;
}
console.log(res);
}
Comments
Leave a comment