Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
These tutorials I am going through are killing me on Arrays using some built in functions.

I need to know how to assign multiple changes using the map function. It seems I can only do one change while the rest are ignored.

It needs to pass these tests and it fails because I don't toLowerCase first.

console.log(snakeToCamel('snakes_go_hiss')); // 'SnakesGoHiss'
console.log(snakeToCamel('say_hello_world')); // 'SayHelloWorld'
console.log(snakeToCamel('app_academy_is_cool')); // 'AppAcademyIsCool'
console.log(snakeToCamel('APp_ACADEMY_iS_cOol')); // 'AppAcademyIsCool'


What I have tried:

It doesn't use toLowerCase first. X(

function snakeToCamel(str) {
    // Your code here

    let answer = str.split("_").map(function(word) {
        return word[0].toUpperCase() + word.slice(1,word.length);
    })
    return answer.join("");
};
Posted
Updated 25-Dec-22 3:29am
v5
Comments
Richard MacCutchan 4-Oct-22 4:38am    
Because you do not have a call to toLowerCase.
Chris Aug2022 4-Oct-22 4:39am    
Yeah, clearly. I can't figure out how to add it.

It works fine for me - if I feed your code into an online Javascript Tester[^] :
JavaScript
function snakeToCamel(str) {
    // Your code here

    let answer = str.split("_").map(function(word) {
        return word[0].toUpperCase() + word.slice(1,word.length);
    })
    return answer.join("");
}
snakeToCamel("hello_world");
I get what I would expect:
"HelloWorld"
Check your calling code and see exactly what you are passing to the function.
 
Share this answer
 
v2
Comments
Chris Aug2022 4-Oct-22 4:21am    
Yes, however, it won't work if you have an input like "ApPaCaDeMy OnLiNe SuCkS"
OriginalGriff 4-Oct-22 4:52am    
So call toLowerCase before you call split ... why is this a problem for you?
If you want to do it for spaces as well as underlines, you will need a regex instead of a string separator.
Chris Aug2022 4-Oct-22 4:55am    
I have no idea why that answer was so elusive. I literally put .toLowerCase everywhere else.
OriginalGriff 4-Oct-22 5:06am    
:DEasy to overlook!
function snakeToCamel(str) {
    // Your code here

    let answer = str.split("_").map(function(word) {
        return word[0].toUpperCase() + word.slice(1,word.length).toLowerCase();
    })
    return answer.join("");


You Can just add toLowerCase at the end once you slice the second part of the word and it works fine.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900