简体   繁体   中英

Snake to Camel Case: [SOLVED] I'm stuck in the second string, when i concatenate can't skip the first char, to don't repeat de char[0]

The function only can solved with.map, i cant find the way to skip the first index of each string to solve the excercise.

`

let snakeToCamel = function(string) {
    // Your code here
    const result = string.split("_").map(char => char[0].toUpperCase() + char.toLowerCase());
    return result.join("");
};
console.log(snakeToCamel('snakes_go_hiss')); // 'SnakesGoHiss'
console.log(snakeToCamel('say_hello_world')); // 'SayHelloWorld'

`

This is my best trie, i google for answers but i dont find nothing with.map method, if anyone know where are my error i been so grateful. Thanks!

PD: I can't use.replace!

I SOLVED, REPLACING THE "... + char.toLowerCase()" to "char.substring(1).toLowerCase()"

Rather than index specifiers [] , I would use slice for this..

char.slice(0,1) get first char, char.slice(1) , get chars from index 1 to end..

eg.

 let snakeToCamel = function(string) { const result = string.split("_").map(char => char.slice(0,1).toUpperCase() + char.slice(1).toLowerCase()); return result.join(""); }; console.log(snakeToCamel('snakes_go_hiss')); // 'SnakesGoHiss' console.log(snakeToCamel('say_hello_world')); // 'SayHelloWorld'

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