简体   繁体   中英

why does this code change the string but again revert back to original state

function capital(b){
    let letter = b[0].toUpperCase();
    return letter + b.slice(1);
}

function correction(a){
    let place = a.indexOf('_');
    let part1 = a.slice(0, place);
    let part2 = a.slice(place+1);
    a = part1 + capital(part2);
    if (a.includes('_'))
        correction(a);
    return a;
}

When I call correction("Hey_there_How_are_you") , I see in while debugging that it becomes "HeyThereHowAreYou" but again becomes as original string itself. What could be the problem?

You need to return the result of the call in your conditional so that it can be used again.

 function capital(b) { let letter = b[0].toUpperCase(); return letter + b.slice(1); } function correction(a) { let place = a.indexOf('_'); let part1 = a.slice(0, place); let part2 = a.slice(place + 1); a = part1 + capital(part2); if (a.includes('_')) { return correction(a); } return a; } let x = correction("Hey_there_How_are_you"); console.log(x);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM