老師 有什麼可以改進?

老師 有什麼可以改進?

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>搜索内容是否有敏感字"草"</title>
</head>
<body>
  <script>
  //补充代码

  let weedNum
  input = prompt("請輸入搜索內容")
  if (input.indexOf("草")==-1) {
    document.write("敏感字草首次出現的位置是-1")
  }else{
    for(let i = 0; i<input.length; i++){
      if(input.charAt(i)==="草"){
        if (weedNum===undefined) {
          weedNum=i
        }
        document.write('敏感字草在'+i+'位置被發現'+'<br>')      
      }
    }
  }
  
  document.write('敏感字草首次出現的位置是'+weedNum) 
  


  




</script>
</body>
</html>


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,测试当没有‘草’字的时候结果如下:

http://img1.sycdn.imooc.com//climg/5ef71b7109ee214c08620110.jpg

感觉这样提示不友好,可以把-1位置改为‘没有敏感字’。参考修改

http://img1.sycdn.imooc.com//climg/5ef71bf2097886d513050827.jpg

自己再测试下,祝学习愉快!

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师