<!DOCTYPE html>
<html><head><meta charset="utf-8"><title></title></head><body><script>function fn(n){for(let i = 0; i< n; i++){for(let j = 0; j < n; j++){console.log(i)}}}//递归函数:简单说就是自己调用自己的函数,一定要有种终止条件//定义函数// function fn(num){// console.log(num)// num += 1// if(num >=100){// console.log(num)// return num// }// fn(num)// }// //调用// let num = 0// fn(num)function sum(n) {if (n == 1) {return 1}return sum(n - 1) + n}console.log(sum(100))console.log("程序结束.")//假如n == 100//sum(99) + 100//sum(98) + 99 + 100//sum(97) + 98 + 99 +100//1 + 2 + 3 + ... 100// 1 1 2 3 5 8 13 21 34 55//求斐波那契数列的第n项function fib(n) {if (n == 1 || n == 2) {return 1}return fib(n - 1) + fib(n - 2)}console.log(fib(10))</script></body>
</html>
数组
js中的数组不是一段连续的空间,他是一个双向的链表结构,每一个元素会记录前后元素的内存地址,所以删除或者插入元素只需要修改前后元素中保存的地址即可,非常方便,这一点上优于C。
但是要访问元素的效率比起c差很多。
咱们的数组也是由于许多好用的属性和方法。
常用的方法和属性如下
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title></title></head><body><script>//如何创建数组,直接创建一个数组的实例赋值给变量//var arr = []//创建方式2//var arr = new Array()//创建方式3//var arr = Array(1, 2, 3, 4, 5, 6)//增//push新增元素值数组的尾部//arr.push(7)//arr.unshift(0)//删// arr.pop()// arr.shift()//改// arr[0] = 100//查询//let arr2 = [7, 8, 9, 10]//拼接数组concat//join通过指定的字符串来拼接数组中每一个元素//split是通过指定的字符串将目标字符串分解成数组let arr = [1,2,3,4,5]// let str = arr.join("*!@#¥%")// let arr2 = str.split("*!@#¥%")// console.log(str)// console.log(arr2)// //reverse反转// console.log(arr.reverse())//splice可以用来在指定位置插入多个元素,也可以用来删除指定位置多个元素arr.splice(2,2)console.log(arr)//sort 可以用来给数组排序 排序方法可以自定 return a-b 升序 b-a降序let arrary = [5,3,7,1,2,99,130]console.log(arrary.sort(function(a,b){return a-b// b-a}))</script></body>
</html>
Math
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title></title></head><body><script>console.log(Math.PI)console.log(Math.E)//floor向下去整console.log(Math.floor(16.99999999))//ceil向上取整console.log(Math.ceil(16.000000001))//四舍五入console.log(Math.round(13.5))//abs取绝对值console.log(Math.abs(-100))let arr = [1, 7, 3, 8, 9, 10]//max方法取最大值console.log(Math.max(1, 7, 8, 9, 0, 101, 123123, 33))//min方法区最小值console.log(Math.min(1, 7, 8, 9, 0, 101, 123123, 33))//math.random随机数 范围[0,1)console.log(Math.random() * 10 + 10)//开平方sqrtconsole.log(Math.sqrt(100))//幂次运算console.log(Math.pow(1e200,(1/100)))</script></body>
</html>
date模块
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title></title></head><body><div id="msg"></div><script>//获取当前系统时间let date = new Date()console.log(date)//指定一个时间// date = new Date("2000-1-1 00:00:00")// console.log(date)//获取年份let year = date.getFullYear()console.log(year)//获取月份let month = date.getMonth()console.log(month + 1)//获取几号let dateOfMonth = date.getDate()console.log(dateOfMonth)//获取星期几let day = date.getDay()console.log(day)//时分秒let hour = date.getHours()let minute = date.getMinutes()let second = date.getSeconds()let millsec = date.getMilliseconds()console.log(hour + ":" + minute + ":" + second + ":" + millsec)let div = document.getElementById("msg")let intervalId = setInterval(() => {date = new Date()let hour = date.getHours()let minute = date.getMinutes()let second = date.getSeconds()let millsec = date.getMilliseconds()// document.write(date)div.innerHTML = hour + ":" + minute + ":" + second + ":" + millsec}, 100)clearInterval(intervalId)</script></body>
</html>
倒计时代码
<!DOCTYPE html>
<html><head><meta charset="utf-8"><title></title></head><body><div id="msg"></div><script>let count_down = 5let msg = document.getElementById("msg")let id = setInterval(()=>{msg.innerHTML = count_downcount_down--if(count_down == 0){clearInterval(id)msg.innerHTML = "火箭发射"}},1000)</script></body>
</html>