WeIrD StRiNg CaSe

本文介绍了一种将字符串中每个单词的偶数索引字符转为大写、奇数索引字符转为小写的实现方法。通过多种JavaScript函数演示了不同解决方案,并分析了每种方法的特点。

Description:

Write a function toWeirdCase (weirdcase in Ruby) that accepts a string, and returns the same string with all even indexed characters in each word upper cased, and all odd indexed characters in each word lower cased. The indexing just explained is zero based, so the zero-ith index is even, therefore that character should be upper cased.

The passed in string will only consist of alphabetical characters and spaces(' '). Spaces will only be present if there are multiple words. Words will be separated by a single space(' ').

Examples:

toWeirdCase( "String" );//=> returns "StRiNg"
toWeirdCase( "Weird string case" );//=> returns "WeIrD StRiNg CaSe"

解题:

function write(string){
var nstr='';
var result='';
var n='';
    string.split(' ').forEach(function(items,index){
           items.split('').forEach(function(item ,index){
              nstr +=(index % 2 == 0 ? item.toUpperCase() : item.toLowerCase());
          });
       result += nstr + ' ';
     })
   console.log(result);
    result.split(' ').forEach(function(item,index){    
              console.log(item+','+result.split(' ')[index-1]);      
              n+= item.replace(result.split(' ')[index-1],' ');
   })
     return n;
}
write('This is a test'); 

大神解法:

function toWeirdCase(string){
  return string.split(' ').map(function(word){
    return word.split('').map(function(letter, index){
      return index % 2 == 0 ? letter.toUpperCase() : letter.toLowerCase()
    }).join('');
  }).join(' ');
}

少了两层循环 ,我还是js函数用的少

function toWeirdCase(string){
  return string.replace(/(\w{1,2})/g,(m)=>m[0].toUpperCase()+m.slice(1))
}

这....

function toWeirdCase(string) {
  var i = 0;
  return [].map.call(string.toLowerCase(), function(char) {
    if (char == " ") { i = -1; }
    return i++ % 2 ? char : char.toUpperCase();
  }).join('');
}
function toWeirdCase(string){
  return string.split(/ /g).map(function (word) { return word.split('').map(function (c, i) { return i % 2 ? c.toLowerCase() : c.toUpperCase(); }).join('') }).join(' ');
}

都能明白,也没啥高深的东西 ,方法多解要多练

转载于:https://www.cnblogs.com/shaoxiablogs/p/6073308.html

``` public class CaseArithmetic extends Case { public static String LIN_RESULT = "LIN_RESULT"; protected Bundle mInfo[]; public static int Repeat = 1; public static int Round = 3; CaseArithmetic(Context context) { super(context, "CaseArithmetic", "com.oem.runin.TesterArithmetic", Repeat, Round); mType = "mflops"; String [] _tmp = { "numeric", "mflops", "scientific", }; mTags = _tmp; generateInfo(); } @Override public String getTitle() { return mContext.getString(R.string.title_arithmetic); } @Override public String getDescription() { return mContext.getString(R.string.description_arithmetic); } private void generateInfo() { mInfo = new Bundle[Repeat]; for (int i = 0; i < mInfo.length; i++) { mInfo[i] = new Bundle(); } } @Override public void clear() { super.clear(); generateInfo(); } @Override public void reset() { super.reset(); generateInfo(); } @Override public String getResultOutput() { String result = "\n"; for (int i = 0; i < mInfo.length; i++) { result += TesterArithmetic.bundleToString(mInfo[i]); result += "\n"; } return result; } /* * Get Average RunIn */ @Override public double getBenchmark(Scenario s) { double total = 0; int length = mInfo.length; for (int i = 0; i < length; i++) { total += mInfo[i].getDouble(TesterArithmetic.MFLOPS); } return total / length; } @Override public ArrayList<Scenario> getScenarios () { ArrayList<Scenario> scenarios = new ArrayList<Scenario>(); Scenario s = new Scenario(getTitle(), mType, mTags); s.mLog = getResultOutput(); for (int i=0; i<mInfo.length; i++) s.mResults.add(mInfo[i].getDouble(TesterArithmetic.MFLOPS)); scenarios.add(s); return scenarios; } @Override protected boolean saveResult(Intent intent, int index) { Bundle info = intent.getBundleExtra(LIN_RESULT); if (info == null) { Log.i(TAG, "Weird! cannot find LinpackInfo"); return false; } else { mInfo[index] = info; } return true; } }```解释代码 ,具体怎么使用的?
03-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值