repl.it
@cmclau01/

underToCamelCase

JavaScript

function to convert underscored variable names to camelCase.

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/* 
Fullstack Academy - JavaScript JumpStart

Challenge - under_score to camelCase

Write a function to convert a variable name from under_score format to camelCase.
Make sure you support an unlimited number of underscores in the input!
*/

// Examples:

// underToCamel("first_name"); // should return firstName
// underToCamel("income_tax"); // should return incomeTax

// Write Code Below
//1. create function with parameter; 2. create variable that will store '_ '. 3. create loop to iterate through string characters e.g. char[1]; 4. create conditional statement that will check argument for '_ '; 5. write code that will replace '_ ' with char[i].toUpperCase();

function underToCamel(underName) {
  debugger;
  let foundUnder = false;
  let camelName = '';
  for (i = 0; i < underName.length; i++) {
    if (underName[i] === '_') {
      foundUnder = true;
    }  else if (foundUnder) {
          camelName += underName[i].toUpperCase();
          foundUnder = false;
        } else {
          camelName += underName[i];
        }
      }
      return camelName;
  } 
  

underToCamel("first_name");



Native Browser JavaScript
?