Blogger Information
Blog 5
fans 0
comment 0
visits 3174
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
javascript变量、函数,流程控制,循环,类型转换作业_2019_12_17
Original
526 people have browsed it
  1. javascript变量、函数的定义

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>demo1</title>
    8. </head>
    9. <body>
    10. <script>
    11. //使用关键字var定义一个变量
    12. var myNum1 = 10;
    13. alert(myNum1);
    14. //使用关键字function定义一个函数
    15. function disPlay(num) {
    16. console.log(num);
    17. }
    18. //调用刚创建的display函数,并传入实参
    19. disPlay(myNum1);
    20. </script>
    21. </body>
    22. </html>
  1. javascript流程控制if else switch

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>demo3</title>
    6. </head>
    7. <body>
    8. <script>
    9. /*
    10. 小明妈妈和小明说:如果你考到90分以上,我就给你买汽车玩具,考到60分到90分之间
    11. 就带你去吃肯德基,如果你考60分以下就回来挨打
    12. */
    13. function mamaSay(grade) {
    14. if(grade>=90){
    15. alert("买玩具汽车");
    16. }
    17. if(grade<90&&grade>=60){
    18. alert("去吃肯德基");
    19. }
    20. if(grade<60){
    21. alert("回家挨打!");
    22. }
    23. }
    24. function mamaSay2(grade) {
    25. if (grade >= 90) {
    26. alert("买玩具汽车");
    27. } else if (grade < 90 && grade >= 60) {
    28. alert("去吃肯德基");
    29. } else {
    30. alert("回家挨打!");
    31. }
    32. }
    33. function mamaSay3(grade) {
    34. /*不知为何,我在case中放入表达式会走默认。所以
    35. 我就把grade强制转换为int类型了*/
    36. grade=parseInt(grade/10);
    37. switch (grade) {
    38. case 10:
    39. case 9:
    40. alert("买玩具汽车");
    41. break;
    42. case 8:
    43. case 7:
    44. case 6:
    45. alert("去吃肯德基");
    46. break;
    47. default:
    48. alert("回家挨打!");
    49. }
    50. }
    51. var mygrade = 90;
    52. mamaSay3(mygrade);
    53. </script>
    54. </body>
    55. </html>
  1. javascript三种循环

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>demo4</title>
    6. </head>
    7. <body>
    8. <script>
    9. var arr = [12,15,17,64,84,61,24,56];
    10. for(i=0;i<arr.length;i++){
    11. console.log(arr[i]);
    12. }
    13. var i = 0;
    14. while (i<arr.length){
    15. console.log(arr[i]);
    16. i++
    17. }
    18. i = 0;
    19. do {
    20. console.log(arr[i]);
    21. i++;
    22. }while (i<arr.length);
    23. </script>
    24. </body>
    25. </html>
  1. 数据类型转换:parseInt、isNaN函数的使用
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>demo5</title>
  6. </head>
  7. <body>
  8. <script>
  9. var number = "135";
  10. number = parseInt(number);
  11. console.log(number.constructor===Number); //控制台显示ture,转换成功
  12. var str = "西门大官人";
  13. console.log(isNaN(number));//控制台显示false,表示变量number是数字,js自动转换为数字了
  14. console.log(isNaN(str));//控制台显示ture,表示变量str不是数字
  15. </script>
  16. </body>
  17. </html>
Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post