[英]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. function只能用.map来解决,我找不到跳过每个字符串的第一个索引来解决练习的方法。
` `
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.这是我最好的 trie,我用谷歌搜索答案,但我没有找到任何东西。map 方法,如果有人知道我的错误在哪里,我将非常感激。 Thanks!
谢谢!
PD: I can't use.replace! PD:我不能使用。更换!
I SOLVED, REPLACING THE "... + char.toLowerCase()" to "char.substring(1).toLowerCase()"我解决了,将“... + char.toLowerCase()”替换为“char.substring(1).toLowerCase()”
Rather than index specifiers []
, I would use slice for this..而不是索引说明符
[]
,我会为此使用 slice ..
char.slice(0,1)
get first char, char.slice(1)
, get chars from index 1 to end.. char.slice(0,1)
获取第一个字符, char.slice(1)
获取从索引 1 到结束的字符。
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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.