Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
How could i camelCase a string using .replace method?

I am taking a JS course and my homework was to make a little program that camelCases values from 'textarea' input.

Dummy input:

underscore_case
 first_name
Some_Variable
  calculate_AGE
delayed_departure

My code looks something like this:

document.body.append(document.createElement('textarea'));
document.body.append(document.createElement('button'));


document.querySelector('button').addEventListener('click', function () {
  const text = document.querySelector('textarea').value; //Storing data in a variable
  const letsLower = text.toLowerCase(); //Lowecasing
  const arrayedValues = letsLower.split('\n');
  let camelCased = [];        //I created an emtpy array so i could changed values to it
  for (let v of arrayedValues) {
    v = v.trimStart();
    const up = v.indexOf('_') + 1; //Here i am detecting the character that should be changed
    v = v.replace(v[up], v[up].toUpperCase()); //replacing lower to upper character
    v = v.replace('_', ''); **deleting the underscore**

    camelCased.push(v); //pushing the changed element to the array
  }
  let emoji = '';
  for (let z of camelCased) {
    emoji += '✔️';
    console.log(`${z} ${emoji}`);
  }
});

Please ignore the check emoji, this was also part of exercise.

So it works untill there is a string that has the character i want to replace earlier in the string, then it just replaces the first instance (e.g. a_a will be Aa instead of aA). I know that .replace() method replaces only the first occurrence and this the explanation of the problem.

My question is how could I re-write the program using replace method? I already know how to solve this problem in another way, as was explained in the course, but I am really curious if it can work this way.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!