请问我这个函数错在哪,为什么不能运行啊?


function assertEquals(s){

  var arr=s.split('');

if(arr.length<=2){

return s

}else{

var b=arr.legth%2

if(b==0){

return arr[arr.length/2-1]+arr[arr.length/2];

}else{

       arr[Math.floor(arr.length/2)]

      }

}}


我想实现返回函数给定的字符串变量  如果字符串字母偶数个  返回中间两个,如:assertEquals(abcdef)  返回cd

如果函数字符串字母奇数个  返回中间的一个  如  assertEquals(abcde)  返回c


慕仰6432972
浏览 1320回答 2
2回答

qq_青枣工作室_0

你的错误:var b=arr.legth%2    // 这里的lenth拼写错误,应该是lengtharr[Math.floor(arr.length/2)]    // 这里缺少return。修正后:function assertEquals(s){ var arr=s.split(''); if(arr.length<=2){ return s }else{ var b=arr.length%2 if(b==0){ return arr[arr.length/2-1]+arr[arr.length/2]; }else{ return arr[Math.floor(arr.length/2)] } } }再看我写的,<script> function assertEquals(s){     var middle = parseInt(s.length/2),         isEven = s.length%2==0;     return s.substr(isEven?middle-1:middle, isEven?2:1); } </script>

慕瓜9220888

LTFEZDGJPKZRXZFIMCNVHFLTXXVLWYUXNRVAKPLXZCQNERIKJOBOFPFQSOUUTGORPRDMSMIVKFPVUUXHDGOWIEEEJMMINMRBWWZZBQJONWDJEJLDFMZJIAZHAZCZWZJEXPORLHYRJMBAGQTDHFEAWJIZIOWIHMLIVIJBHMBBBGTTOTVXRXUFRMVUMSFJCLGAMVLYUUWAWFHUHKHYQCRDKTNXMNJFAXKUPDKWCXRKRMEJLDZTQXVOJMMOBELWBGIKQTMPJPBWFHUKXKQOVQJCFFZFNQVHANAAYMLRZWGNUWOODWMPCGKCBRXAGATRCRAPPCVMVNFBXTTWDDMCXDWLIVEZSBGGWBEKZTJMGSDEUHADYLOLYODCITTGTKZRJEXHUADOTZLNRUUXTPLUGCWZZNYHAMPJZSKFVKZRERUWBXJLPLFOKYHWSNADKMKGBEDKNGLHDYYXURELGUGPSPVMVUZGPDRUVOWSIRLXFJGLFBXBLJNEPXWXPXYDHWKQWDTKVTENSYREB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GWNVXTLLLLLWHNZVOBKT

慕田峪3555374

检查你标点符号
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript