博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
7kyu Jaden Casing Strings
阅读量:4580 次
发布时间:2019-06-09

本文共 1609 字,大约阅读时间需要 5 分钟。

题目:

Jaden Smith, the son of Will Smith, is the star of films such as The Karate Kid (2010) and After Earth (2013). Jaden is also known for some of his philosophy that he delivers via Twitter. When writing on Twitter, he is known for almost always capitalizing every word.

Jaden Smith是威尔史密斯的儿子,他是电影空手道小子(2010)和地球(2013)的明星。Jaden也因他的一些哲学而闻名,他通过Twitter发布了他的哲学。当他在Twitter上写作时,他几乎总是把每个词都大写。

Your task is to convert strings to how they would be written by Jaden Smith. The strings are actual quotes from Jaden Smith, but they are not capitalized in the same way he originally typed them.

你的任务是把字符串转换成Jaden Smith的写作方式。这些字符串是Jaden Smith的实际引用,但它们并没有像他最初输入的那样大写。

Example:

Not Jaden-Cased: "How can mirrors be real if our eyes aren't real"Jaden-Cased:     "How Can Mirrors Be Real If Our Eyes Aren't Real"

Sample Tests:

var str = "How can mirrors be real if our eyes aren't real";Test.assertEquals(str.toJadenCase(), "How Can Mirrors Be Real If Our Eyes Aren't Real");

答案:

// 1String.prototype.toJadenCase = function () {  var str = this;  var arr = str.toLowerCase().split(' ');  for(i = 0; i < arr.length; i++) {    arr[i] = arr[i].slice(0,1).toUpperCase() + arr[i].slice(1);  }  return arr.join(' ');};// 2String.prototype.toJadenCase = function () {    return this.split(" ").map((word) => {        return word.charAt().toUpperCase() + word.slice(1);    }).join(" ");}// 3  // ^    匹配字符串的开始  // \s匹配任意的空白符,包括空格,制表符(Tab),换行符,中文全角空格等String.prototype.toJadenCase = function () {    return this.replace(/(^|\s)[a-z]/g, function(x) {        return x.toUpperCase();    });}

 

转载于:https://www.cnblogs.com/tong24/p/7382649.html

你可能感兴趣的文章