Javascript String camelCase()

Description

Javascript String camelCase()


String.prototype.camelCase = function(){ 
      return this.split(' ').map( function(x){ return x.replace(/\b\w/g, l => l.toUpperCase()); } ).join(''); 
}

Javascript String camelCase()

'use strict';/*  w  w  w. ja  va 2s  .c o  m*/
String.prototype.camelCase = function () {
  return this.toString().split(' ').map(function(word){
     return word.charAt(0).toUpperCase() + word.slice(1);
  }).join('');
}

console.log("sad sdsd".camelCase());

Javascript String camelCase()

String.prototype.camelCase = function(){
  return this.split(' ').map((word) => (word[0] || '').toUpperCase() + word.slice(1)).join('');
}

console.log("hello case".camelCase());

Javascript String camelCase()

String.prototype.camelCase=function(){
  var x = this;/*from   www .  j  a  v a 2 s  . c  o m*/
  var res = x.split(' ');
  var d = "";
  for(var i = 0; i < res.length; ++i) {
    if (res[i].length > 0) {
      res[i] = res[i][0].toUpperCase() + res[i].slice(1)
      d = d + res[i];
    }
  }
  return d;
}

Javascript String camelCase()

String.prototype.camelCase = function() {
  return this.replace( /(\_[a-z])/g, function( $1 ) {return $1.toUpperCase().replace( "_", "" );} );
};

Javascript String camelCase()

// link to the task: https://www.codewars.com/kata/camelcase-method/train/javascript

String.prototype.camelCase = function(){
  if (this.length === ' ') {
    return this;/*from  w  ww  .  j av  a2  s  .c o m*/
  }

  var string = this.split(' ').filter((el) => !!el);

  for (var x = 0; x < string.length; x++) {
    string[x] = string[x][0].toUpperCase() + string[x].slice(1).toLowerCase();
  }

  return string.join('');
}

Javascript String camelCase()

String.prototype.camelCase = function() {
  var arr = this.split(" ");
  for (var i = 1; i < arr.length; i++) {
    arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1)
  }//from w w  w  . ja  v a2  s.c om
  return arr.join("");
}

console.log('your soul is mine'.camelCase());
console.log(' Get over      here! '.camelCase());

Javascript String camelcase()

String.prototype.camelcase = function() {
  return this.split("_").map(function(e) {
    return e.replace(/^([a-z])/g, function($1) {
      return $1.toUpperCase();
    });/*ww  w  . ja va2  s  . c  o  m*/
  }).join("");
};

Javascript String camelcase()

// Camelize string
String.prototype.camelcase = function() {
  return this.replace(/_/g, ' ').replace(/(?:^\w|[A-Z]|\b\w|\s+|_+)/g, function(match, index) {
    if (+match === 0) {
      return "";
    }/*from   www  .  ja  va2 s.c om*/
    return match.toUpperCase();
  });
};

Javascript String camelCase()

String.prototype.camelCase = function() { 
    return this.toLowerCase().replace(/[_-](.)/g, function(match, group1) {
        return group1.toUpperCase();
    });//from w w  w . ja v a 2 s. co m
}

Javascript String camelCase()

// Write simple .camelcase method (camel_case function in PHP) for strings.
// All words must have their first letter capitalized without spaces.

String.prototype.camelCase=function(){

  var strArr = Array.from(this).join('').split(' ');

  return strArr.map( word => {
    var uppCase = word.charAt(0).toUpperCase();
    var replaced = word.charAt(0);
    word = word.replace( replaced, uppCase );
    return word;//from  ww w .j  a va 2  s. c om
  }).join('');
}

Javascript String camelCase()

/* My approach:/* w w w.  j  ava2 s .com*/
  - split the String by ' '
  - loop through the words
  - join them
*/

String.prototype.camelCase=function(){
  arr = this.split(' ');
  for(let i = 0; i < arr.length; ++i){
    arr[i] = arr[i].charAt(0).toUpperCase()+arr[i].substring(1);
  }
  return arr.join('')
}


String.prototype.camelCase=function(){
  arr = this.split(' ')
  arr = arr.map(function(e){
    return e.charAt(0).toUpperCase() + e.substring(1);
  })
  return arr.join('')
}

Javascript String camelCase()

// Camel Case Method 

/*/*w ww.ja va2  s. c o m*/
 Write simple .camelcase method (camel_case function in PHP) for strings. All words must have their first letter capitalized without spaces.
*/

// Solution

String.prototype.camelCase=function(){
    var a = this.split(' ');
    return a.map(function(val){
      return val.replace(/^[a-z]/, camelCaseFirstLetter);
    }).join('');
}

function camelCaseFirstLetter(a){
  return a.toUpperCase();
}

Javascript String camelCase()

/*/*from w ww . ja v  a 2 s. c o  m*/
"hello case".camelCase() => HelloCase
"camel case word".camelCase() => CamelCaseWord
Don't forget to rate this kata! Thanks :)
ALGORITHMSFUNDAMENTALSSTRINGS
*/
String.prototype.camelCase=function(){
    return this.split(' ').map((word)=>{return word.charAt(0).toUpperCase() + word.slice(1)}).join('')
}

Javascript String camelCase()

String.prototype.camelCase = function() {
  return this.trim().replace(/(^|\s)\w/g, (match) =>
    match.trim().toUpperCase(),/* w  ww .  j av  a2  s.c  o m*/
  );
};

Javascript String camelcase()

String.prototype.camelcase = function() {
    var result = "";
    var prevChar = undefined;
    for (var i = 0; i < this.length; i++){
        var currentChar = this.charAt(i);
        if (i == 0 || prevChar == " ") {
            result += currentChar.toUpperCase();
        } else {// w w  w. ja  v  a 2  s .com
            result += currentChar;
        }
        prevChar = currentChar;
    }
    return result;
};

module.exports = String;

Javascript String camelcase()

String.prototype.camelcase = function()
{
    var result ="";
    var prevchar = undefined;
    for (var i = 0; i < this.length; i ++)
    {//w ww. j  ava 2 s. c  o m
        var currentChar = this.charAt(i);
        if (i == 0 || prevchar == " ")
        {
            result += currentChar.toUpperCase();
        }
        else
        {
            result += currentChar;
        }
        prevchar = currentChar;
    }
    return result;
};

module.exports = String;

Javascript String camelCase()

// Write simple chainable .camelcase method for strings.
// All words must have their first
// letter capitalized without spaces.
///* www . java2  s.c  o  m*/
// For instance:
//
// "hello case".camelCase() => HelloCase
// "camel case word".camelCase() => CamelCaseWord
//

String.prototype.camelCase = function () {
  return this
    .split(' ')
    .map((word) =>
      word
        .split('')
        .map( (letter, index) => (index === 0)
         ? letter.toUpperCase()
         : letter )
        .join(''))
  .join('');
}


console.log(
  "hello case".camelCase(), // => HelloCase
  "camel case word".camelCase() // => CamelCaseWord
);

Javascript String camelCase()

String.prototype.camelCase = function() {
 var result = "";
 var prevChar = undefined;
 for (var i = 0; i < this.length; i++) {
  if(i == 0 || prevChar == " ") {
   result += this.charAt(i).toUpperCase();
  } else {/*from ww  w. j  a v a 2s.com*/
   result += this.charAt(i);
  }
  prevChar = this.charAt(i);
 }
 return result;
};

Javascript String camelCase()

// Write simple .camelcase method (camel_case function in PHP) for strings. All
// words must have their first letter capitalized without spaces.

// For instance://from  w w  w  . j a va2 s  . co  m
// "hello case".camelCase() => HelloCase
// "camel case word".camelCase() => CamelCaseWord

String.prototype.camelCase = function () {
  var splitArr = this.split(' ');
  var output = '';
  for (var i=0; i<splitArr.length; i++) {
    if (splitArr[i].match(/^[A-Z]+$/i)) {
      output = output + splitArr[i][0].toUpperCase() + splitArr[i].slice(1);
    }
  }
  return output;
};
console.log("test case".camelCase(), "TestCase");
console.log("camel case method".camelCase(), "CamelCaseMethod");
console.log("say hello ".camelCase(), "SayHello");
console.log(" camel case word".camelCase(), "CamelCaseWord");
console.log("".camelCase(), "");



PreviousNext

Related