老師 有什麼可以改進?
<!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>8
收起
正在回答
1回答
同学你好,测试当没有‘草’字的时候结果如下:

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

自己再测试下,祝学习愉快!
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星