1

Write a function snakeToCamel(str) that takes in a snake_cased string and returns the string CamelCased. snake_case is a string where each word is separated with underscores (_). CamelCase is a string where the first char of each word is capitalized, all other characters lowercase.

Examples:

snakeToCamel('snakes_go_hiss'); // => 'SnakesGoHiss'
snakeToCamel('say_hello_world'); // => 'SayHelloWorld'
snakeToCamel('bootcamp_prep_is_cool'); // => 'BootcampPrepIsCool'
snakeToCamel('BOOtcamp_PREP_iS_cOol'); // => 'BootcampPrepIsCool'

function snakeToCamel(str) {
    var newString = '';
    var words = str.split('_');

    for (i = 0; i < words.length; i++) {
        for (j = 0; j < words.length; j++) {
            if (j === 0) {
                newString += words[i][j].toUpperCase();
            } else {
                newString += words[i][j].toLowerCase();
            }
        }
    }
    return newString;
}
VisioN
  • 143,310
  • 32
  • 282
  • 281
aperri
  • 11
  • 1
  • Does this answer your question? [Javascript method for changing snake\_case to PascalCase](https://stackoverflow.com/questions/44082153/javascript-method-for-changing-snake-case-to-pascalcase) – Sunil Chaudhary Dec 07 '19 at 19:51
  • 1
    looks like homework, however code looks reasonable. – Loopo Dec 07 '19 at 22:20
  • I agree with @Loopo in that this looks like homework, and I think it would be good if @aperri where to ask a specific question as to where they're having issues or getting stuck... Suggestion one, the _`for (i = 0; i < words.length; i++) {...}`_ and other loop probably should be _`for (let i = 0; i < words.length; i++) {...}`_, to avoid assigning globally scoped `i` and `j`. Suggestion two, `words` likely should be `const` not `var` assigned. – S0AndS0 Dec 09 '19 at 04:20
  • One more thing @aperri, I think ya may want to look into how to convert [a string into a char array](https://stackoverflow.com/a/33233956/2632107), there are a few ways but the linked to one likely will be the most concise for correcting one of your loops ;-) – S0AndS0 Dec 09 '19 at 04:30

4 Answers4

1

I think you need to have your second loop iterate over the length of the word not the length of the words array.

function snakeToCamel(str) {
  var newString = '';
  var words = str.split('_');

  for (i = 0; i < words.length; i++) {
    for (j = 0; j < words[i].length; j++) { // <--- the ith word's length
      if (j === 0) {
        newString += words[i][j].toUpperCase();
      } else {
        newString += words[i][j].toLowerCase();
      }
    }

  }
  return newString;
}
console.log(snakeToCamel('snakes_go_hiss'));
console.log(snakeToCamel('say_hello_world'));
console.log(snakeToCamel('bootcamp_prep_is_cool'));
console.log(snakeToCamel('BOOtcamp_PREP_iS_cOol'));
Sunil Chaudhary
  • 4,481
  • 3
  • 22
  • 41
Loopo
  • 2,204
  • 2
  • 28
  • 45
1

Function for converting snake_cased to camelCased

Use the map function for converting first letter of each words to uppercase then join each words

function snakeToCamel(str){
    newString=""
    var words=str.split(_)
    var uppercasedWords=words.map(word=>{
        return word.charAt(0).toUpperCase+word.slice(1);
    })
    for(i=0;i<=uppercasedWords.length;i++){
        newString+=uppercasedWords[i]
    }
    return newString;
}
Jibin Francis
  • 348
  • 2
  • 14
0

If you want to convert to PascalCase (the first letter is Uppercase), refer to this:

Javascript method for changing snake_case to PascalCase

If you want to convert to camelCase (the first letter is lowercase), refer to this:

https://catalin.me/javascript-snake-to-camel/

Nicola Lepetit
  • 756
  • 8
  • 25
0

As an alternative solution.

Define a function that converts a word to be first letter uppercase and the rest lowercase (I called it title as that's what its called in python). Then split the string, map the words and rejoin it.

function title(s){
    if(s.length == 0) return s;

    return s[0].toUpperCase() + s.substring(1).toLowerCase();
}

function snakeToCamel(str) {
    return str.split('_').map(title).join('');
}

[
    'snakes_go_hiss',
    'say_hello_world',
    'bootcamp_prep_is_cool',
    'BOOtcamp_PREP_iS_cOol'
].map(snakeToCamel).forEach(x => console.log(x));

Output:

SnakesGoHiss
SayHelloWorld
BootcampPrepIsCool
BootcampPrepIsCool
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61