2

I had a JavaScript interview last wednesday, and I had trouble with one of the questions. Maybe you guys can give me hand with it?

The question was: how would you go about this printing var a and s to the console, in camel case, with the help of a prototype function...

var s = “hello javier”;
var a = “something else”;

String.prototype.toCamelCase = function() {
/* code */ 

return capitalize(this); 


};

...so the result is the same as doing this?

console.log(s.toCamelCase());
console.log(a.toCamelCase());

>HelloJavier 
>SomethingElse

Thanks!

Johnny D.
  • 21
  • 1
  • 3
    So it's not prototypes, but rather about implementing `capitalize` function? – dfsq Oct 17 '15 at 17:46
  • 1
    Can you include an attempt you've made? Or, perhaps explain what you've figured out from the question and at what point you're struggles with it begin? Afraid asking for a full solution isn't what SO is for. We're here to help with code you've written. – Jonathan Lonowski Oct 17 '15 at 17:59

2 Answers2

2

var s = 'hello javier';
var a = 'something else';

String.prototype.toCamelCase = function() {
  return capitalize(this);
};

function capitalize(string) {
  return string.split(' ').map(function(string) {
    return string.charAt(0).toUpperCase() + string.slice(1);
  }).join('');
}
console.log(a.toCamelCase());
console.log(s.toCamelCase());

Reference How do I make the first letter of a string uppercase in JavaScript?

Community
  • 1
  • 1
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
  • Probably good enough for an interview, but in ECMAScript convention you'd want to not capitalize the first word (since that's reserved for constructors and singletons). – Touffy Oct 17 '15 at 17:59
  • In your solution, the first letter is capitalized. Edit: Touffy beat me to it – Craig Burton Oct 17 '15 at 18:02
  • 1
    The example output given shows the first letter should be capitalized. It may not be idiomatic for JavaScript, but is appropriate for the question. – Jonathan Lonowski Oct 17 '15 at 18:03
  • 1
    @CraigBurton, in the OPs question, the requirement is for each word to have the first letter capitalized. I think they mistakenly call it camel case – AmmarCSE Oct 17 '15 at 18:04
  • Ah I missed that part. In that case, the thing they want to add to the String prototype is poorly named. But in my experience with technical interviews, that may actually have been part of the test. :/ – Craig Burton Oct 17 '15 at 18:06
  • @Touffy Note that capitalization in JavaScript isn't "reserved" by the language. It's only a convention among humans, that colleagues and static analysis tools will complain about. But, `var Foo = 'bar'; console.log(Foo);` is entirely valid code. – Jonathan Lonowski Oct 17 '15 at 18:06
  • @JonathanLonowski I did say it was about convention. However, "Although the first letter of a camel case compound word may or may not be capitalized, the term camel case generally implies lowercase first letter" (https://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms) – Touffy Oct 17 '15 at 18:10
1

I would go with something like this:

var s = "hello javier";
var a = "something else";

String.prototype.toCamelCase = function() {  
  function capitalize(str){
    var strSplit = str.split(' ');

    // starting the loop at 1 because we don't want
    // to capitalize the first letter
    for (var i = 1; i < strSplit.length; i+=1){
      var item = strSplit[i];

      // we take the substring beginning at character 0 (the first one)
      // and having a length of one (so JUST the first one)
      // and we set that to uppercase.
      // Then we concatenate (add on) the substring beginning at
      // character 1 (the second character). We don't give it a length
      // so we get the rest.
      var capitalized = item.substr(0,1).toUpperCase() + item.substr(1);

      // then we set the value back into the array.
      strSplit[i] = capitalized;
    }
    return strSplit.join('');
  }

  return capitalize(this); 

};

// added for testing output
console.log(s.toCamelCase());
console.log(a.toCamelCase());
Craig Burton
  • 188
  • 1
  • 9
  • As pointed out in the comments of AmmarCSE's answer, my solution does not capitalize the first letter, which is a requirement of the exercise. – Craig Burton Oct 17 '15 at 18:07