亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的代碼不返回字符最多的單詞?

為什么我的代碼不返回字符最多的單詞?

郎朗坤 2021-06-21 17:53:08
我試圖讓 JS 獲得最多字符的單詞,但由于某種原因,我的代碼總是返回“The”我已經嘗試了兩種代碼變體。變體 1:let sent = "The quick borwn fox jumped over the jazy dog";let word = findWord(sent);function findWord(sent){  splitSent=sent.split(" ");  let largest="";  for(i=0; i<splitSent.length;i++){    if(splitSent[i].length>largest){        largest=splitSent[i];    }  }return largest;}console.log(word)變體 2:let sent = "The quick borwn fox jumped over the jazy dog";let word = findWord(sent);function findWord(sent){  splitSent=sent.split(" ");  let largest="";  for(split of splitSent){    if(splitSent[i].length>largest){        largest=splitSent[i];    }  }return largest;}console.log(word)控制臺只打印第一個代碼中的“The”和第二個代碼中的“fox”
查看完整描述

1 回答

?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

因為你需要檢查largest.length不largest。


let sent = "The quick borwn fox jumped over the jazy dog";    

let word = findWord(sent);


    

function findWord(sent) {      

  splitSent = sent.split(" ");      

  let largest = "";      

  for (i = 0; i < splitSent.length; i++) {        

    if (splitSent[i].length > largest.length) {          

      largest = splitSent[i];        

    }      

  }      

  return largest;    

}


    

console.log(word);


查看完整回答
反對 回復 2021-06-24
  • 1 回答
  • 0 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號