function main(text){ //去掉标点符号 var arrText = text.split(''); for(var i=0; i<arrText.length; i++){ var index = arrText[i].charCodeAt(); if(index<65 && (index != 32) ){ arrText.splice(i,1); } } //始终用第一个单词进行比较,不断更改数组长度 text2 = arrText.join(''); var arr = text2.split(' '); var char = arr[0]; var count = 0; var len = arr.length; while(len>1){ console.log(char); for(var i=0; i<len; i++){ if(arr[i] == char){ count++; arr.splice(i,1); } } len = arr.length; console.log('-------------------'+count); char = arr[0]; count = 0; } } //var text = 'She is not afraid of changeWhile most people rather continue on living unfulfilled lives as long as their comfort zone remains intact, the empowered woman is all about embracing change. She understands growth cannot happen without change. She understands that change is the gift life offers you to choose your destiny. Therefore, she is not afraid of change because it is her stepping stone towards success. '; main(text);
点赞 评论

相关推荐

牛客网
牛客企业服务