判断语句
JavaScript中的if-else语句与C++、Python、Java中类似。
test.html中的内容为:- [/code]
- [list]
- [*][size=4]使用输入输出来写:[/size]
- [/list]test.js中的内容为:
- [code]let input = document.querySelector(".input");
- let run = document.querySelector("button");
- let output = document.querySelector("pre");
- function main() {
- // 给<run>元素添加监听事件。当触发“click”时,执行function()函数
- run.addEventListener("click", function(){
- let score = parseInt(input.value); // 获取textarea中的input的值(输入)
- let res;
- if (score >= 85) {
- res = "A";
- } else if (score >= 70) {
- res = "B";
- } else if (score >= 60) {
- res = "C";
- } else {
- res = "D";
- }
-
- output.innerHTML = res; // 展示pre内的标签内容(输出)
- })
- }
- export {
- main
- }
复制代码 test.html中的内容为:- <body>
- 输入:
- <br>
- <textarea name="" id="" cols="30" rows="10"></textarea>
- <br>
- <button>Run</button>
- <br>
- <pre></pre>
-
- </body>
复制代码JavaScript中的逻辑运算符也与C++、Java中类似:
- | && | 表示 | 与 |
- | ---- | ----| ---- |
- | || | 表示 | 或 |
- | ---- | ----| ---- |
- | ! | 表示 | 非 |
复制代码循环语句
JavaScript中的循环语句与C++中类似,也包含for、while、do while循环。
for循环:
- [/code][size=4]枚举对象或数组时可以使用:[/size]
- [list]
- [*]for-in循环,可以枚举数组中的下标,以及对象中的key
-
- [*]for-of循环,可以枚举数组中的值,以及对象中的value
- [/list]
- [indent][size=5]while循环:[/size]
- [/indent][code]
复制代码do while循环:
do while语句与while语句非常相似。唯一的区别是,do while语句限制性循环体后检查条件。不管条件的值如何,我们都要至少执行一次循环(无条件执行一次)。
[code][/code]
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |