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

Question #267176

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



1
Expert's answer
2021-11-17T06:28:29-0500
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);
}

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