使用forEach()方法
function maxLong (str){
var arr = str.split(" ");
var max = 0;
var maxword = "";
arr.forEach((ele)=>{
if(ele.length>max){
max = ele.length;
maxword = ele;
}
});
console.log(maxword,max);
}
maxLong("wo shi fengyifan wo jinnianshi ershibasuidale wobiyeyuzhongbeidaxue");
输出结果: