2
2
Implement String.prototype.toLowerCase()
Description
The toLowerCase method returns the value of the string converted to lowercase. toLowerCase does not affect the value of the string itself.
Conditions Let count chars inside function and ignore all bootstrap code:
String.prototype.toLowerCase = function(){
// code that count here
}
Other languages are welcome but JavaScript implemantation is target of this question. Let's just focus on ASCII charachters .
Example input and output:
"sOmeText".toLowerCase(); => "sometext"
Example implementation:
String.prototype.toLowerCase = function(){
return this.split('').map(function(c){
var cc = c.charCodeAt(0);
if (cc > 64 && cc < 91) {
return String.fromCharCode(cc + 32);
}
return c;
}).join('');
}
Winning condition? I saw
String.prototype.toLowerCase.apply(new Date());on StackOverflow with a quick search; what is the input going to be? – beary605 – 2013-02-15T01:00:28.080@beary605 I've updated the question. I hope it's clear now. – Mohsen – 2013-02-15T04:22:40.757
1The example answer is buggy. It doesn't even handle the whole of Latin 1. – Peter Taylor – 2013-02-15T07:15:07.610
1It might want to be specified that the answer only has to work on ASCII characters. Otherwise, things may get out of hand quickly. – Mr. Llama – 2013-02-15T17:21:40.373
updated again. JavaScript supports unicode chars and involving that can be confusing. How would you lowercase
Ú? – Mohsen – 2013-02-15T21:13:56.2831
ú, obviously. That's whatString.prototype.toLowerCase()does. – Peter Taylor – 2013-02-15T22:14:08.013