迅雷前端求"ful"后缀那道题这样做对吗
考完才做出来,醉了,求大神看对不?
String.prototype.get = function(suffix){ var arr = this.split(" "); var obj = {}; var sortable = []; for(var i = 0; i < arr.length; i++){ if(arr[i].substring(arr[i].length-3,arr[i].length) == suffix){ if(!obj[arr[i]]){ obj[arr[i]] = 1; } else{ obj[arr[i]] ++; } } } for (var vehicle in obj){ sortable.push([vehicle, obj[vehicle]]) } obj = sortable.sort(function(a, b) { return b[1] - a[1]; }) var str = []; for(var i = 0 ; i < obj.length; i++){ str.push(obj[i][0]+"="+obj[i][1]); } return str.join(','); } console.log(('asful arf asful werful').get("ful"));
#迅雷##前端工程师#
提示