老师 这样写可以吗?

来源:3-16 编程练习

weibo__没趣味_03683219

2021-06-16 09:03:37

相关代码:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>toUpperCase和toLowerCase</title>
</head>
<body>
<script>
var str="Never deter till tomorrow that which you can do today";
var index=str.indexOf('tomorrow');//补充代码
var strNew=str.substr(index,8);//补充代码
var upperStr=strNew.toUpperCase(0);//补充代码
document.write(str.substring(0,index) + upperStr + str.substring(index+8));

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


写回答

1回答

好帮手慕久久

2021-06-16

同学你好,代码是对的,可以优化一下:

建议把“tomorrow”用变量存储起来、字符串长度用length属性获取,如下:

http://img.mukewang.com/climg/60c95f9d099287f011310359.jpg

​这样优化后,只要修改targetWords的值,代码就能转化其他单词,更通用。比如转换“Never”:

http://img.mukewang.com/climg/60c95fe209847f4f10470294.jpg

http://img.mukewang.com/climg/60c95fea0966e63005620089.jpg

祝学习愉快!



0

0 学习 · 15276 问题

查看课程