Back to Freecodecamp

Challenge 114: Camel to Snake

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69162d64f96574d9bb629eff.md

latest1.0 KB
Original Source

--description--

Given a string in camel case, return the snake case version of the string using the following rules:

  • The input string will contain only letters (A-Z and a-z) and will always start with a lowercase letter.
  • Every uppercase letter in the camel case string starts a new word.
  • Convert all letters to lowercase.
  • Separate words with an underscore (_).

--hints--

toSnake("helloWorld") should return "hello_world".

js
assert.equal(toSnake("helloWorld"), "hello_world");

toSnake("myVariableName") should return "my_variable_name".

js
assert.equal(toSnake("myVariableName"), "my_variable_name");

toSnake("freecodecampDailyChallenges") should return "freecodecamp_daily_challenges".

js
assert.equal(toSnake("freecodecampDailyChallenges"), "freecodecamp_daily_challenges");

--seed--

--seed-contents--

js
function toSnake(camel) {

  return camel;
}

--solutions--

js
function toSnake(camel) {
  return camel
    .replace(/([A-Z])/g, "_$1")
    .toLowerCase();
}