JS基础语法

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//建立一个HTML文件,把下方代码放入尝试,打开浏览器按F12,看输出结果

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

1. 打印

alert('打印内容');
document.write('打印内容');

2. 查看数据类型

let num = 10;
console.log(num);
console.log(typeof num);
console.log(typeof null);

3. 数据类型转换

类型转换
let x = 'abc';
let z = typeof x;
console.log(z);

显示NaN
let y = Number(x);
console.log(y);

确实是数字类型
let m = typeof y;
console.log(m);

let x = '100.123';
console.log(x);
console.log(typeof x);

let y = parseInt(x);
console.log(y);
console.log(typeof y);

let m = parseFloat(x);
console.log(m);
console.log(typeof m);

4. 判断闰年

let year = 2080;
if(year%4===0 && year%100!==0 || year%400===0){
console.log(year+'是闰年');
}else{
console.log(year+'不是闰年');
}

5. 今天是今年第几天

let year = 2023;
let month = 4;
let day = 12;
let totalDays = 0;
switch(month){
case 12: totalDays=totalDays+30;
case 11: totalDays=totalDays+31;
case 10: totalDays=totalDays+30;
case 9: totalDays=totalDays+31;
case 8: totalDays=totalDays+31;
case 7: totalDays=totalDays+30;
case 6: totalDays=totalDays+31;
case 5: totalDays=totalDays+30;
case 4: totalDays=totalDays+31;
case 3: if(year%4===0 && year%100!==0 || year%400===0){
totalDays=totalDays+29;
}else{
totalDays=totalDays+28;
}
case 2: totalDays=totalDays+31;
totalDays=totalDays+day;
}
console.log(totalDays);
//102天了!

6. 10的阶乘

let i = 1;
let number=1;
while(i<10){
number =number * (i+1);
i++;
console.log(i);
}
console.log(number);

7. 10的阶乘

let i = 9;
let total = 10;
while(i>0){
total = total * i;
console.log(i);
i--;
}
console.log(total);

8. 弹窗输入

       //输入的内容,就是函数运算的结果
let result = prompt('你是不是狗!');
console.log(result);

let answer;
while(answer !== '是'){
answer = prompt('你是不是狗?');
}
alert('真的狗');
let answer;
do{
answer = prompt('你是不是狗?');
}while(answer !== '是');
alert('真的狗');

9. for循环实现输出正方形

for(let j=0;j<9;j++){
for(let i=0;i<9;i++){
// document.write('*&nbsp&nbsp&nbsp');
}
document.write('<br/>');
}

10. while循环实现输出正方形

let j=0;
while(j<=9){
let i=0;
while(i<=9){
document.write('*&nbsp&nbsp&nbsp');
i++;
}
while(i<=9){
document.write('*&nbsp&nbsp&nbsp');
i++;
}
document.write('<br/>');
j++;
}

11. for循环实现输出三角形

for(let j=1;j<=9;j++){
for(i=1;i<=j;i++){
document.write('*&nbsp&nbsp&nbsp');
}
document.write('<br/>');
}

12. while循环实现输出三角形

let j = 1;
while(j<=9){
let i = 1;
while(i<=j){
document.write('*&nbsp&nbsp&nbsp');
i++;
}
document.write('<br/>');
j++;
}

13. 九九乘法表

//九九乘法表
let sum;
for(let j=1;j<=9;j++){
for(i=1;i<=j;i++){
sum=i*j;
document.write(i+'*'+j+'='+sum+'&nbsp&nbsp&nbsp');
}
document.write('<br/>');
}

14. 最大公约数

let x = prompt('第一个数');
let y = prompt('第二个数');
let j;
let m=x>y?y:x;
for(let i=1;i<=m;i++){
if(x%i===0&&y%i===0){
j=i;
}
}
document.write(j);

15. 最大公约数

let x = prompt('第一个数');
let y = prompt('第二个数');
for(let i=(x>y?y:x);i>=1;i--){
if(x%i===0&&y%i===0){
document.write(i);
break;
}
}

16. 最小公倍数

let m = prompt('第一个数');
let n = prompt('第二个数');
for(let i=(m>n?m:n);i<=m*n;i++){
if(i%m===0 && i%n===0){
document.write(i);
break;
}
}

17. 最小公倍数

let m = Number(prompt('第一个数'));
let n = Number(prompt('第二个数'));
let larger = m>n?m:n;
for(let i=larger;i<=m*n;i=i+larger){
if(i%m===0 && i%n===0){
document.write(i);
break;
}
document.write(i);
}

18. 函数,10的阶乘

function factorial(n){
let sum=1;
while(n>=1){
sum =sum*n;
n--;
}
document.write(sum);
}

factorial(10);

19. 带返回值的函数,10!,封装函数

function factorial(n){
let sum=1;
while(n>=1){
sum =sum*n;
n--;
}
return sum;
}
document.write(factorial(10));

20. 函数,10!不同输出方式

        //一个函数完成两件事?把函数看成是一个值,就要借助ruturn
function factorial(n){
let sum=1;
while(n>=1){
sum =sum*n;
n--;
}
return sum;
}
document.write(factorial(12));//调用函数,赋值为10,计算结果为10的阶乘,并显示在页面上
console.log(factorial(10));//显示在控制台上

21. 函数递归

function factorial(n){
if(n===1) return 1; //设置折返点!不然无穷向下递进,没有归的过程!1的阶乘不需要计算
return n*factorial(n-1);
}
document.write(factorial(10));

22. 求斐波那契数列第几位

function fbl(n){
if(n===1 || n===2) return 1;
return fbl(n-1)+fbl(n-2);
}
let m=Number(prompt('请输入需要求的斐波那契数列第几位'));
document.write(fbl(m));

23. 冒泡排序

let a=[6,4,7,3,1,9,2,5,8];
let aleng = a.length;
for(let k=aleng;k>=1;k--){
for(let i=0,j=1;i<k;i++,j++){
let cup;
if(a[i]>a[j]){
cup = a[i];
a[i] = a[j];
a[j] = cup;
}
}
console.log(a[k]);
}
console.log(a);

24. 冒泡排序

let a=[6,4,7,3,1,9,2,5,8];
let aleng = a.length;
for(let k=aleng;k>1;k--){ //外层循环是,需要排(数组长度)-1个最大值!
for(let i=0;i<k-1;i++){ //内层循环是遍历一遍,排好一个最大值,第k-1个数与k交换
let cup;
if(a[i]>a[i+1]){ //if语句复制判断交换,j可以不用设置,使用i+1;
cup = a[i];
a[i] = a[i+1];
a[i+1] = cup;
}
}
}
console.log(a);

25. 选择排序

let a=[6,4,7,3,1,9,2,5,8];
console.log(a);
let sizenum = a.length;
let cup;
for(j=0;j<sizenum-1;j++){ //让变量j当设置的最小值下标!
let min = j;
for(i=j+1;i<sizenum;i++){ //循环一轮,找出这轮最小值下标
if(a[min]>a[i]){
min=i;
}
}
cup = a[j]; //把这轮最小值与设定的最小值交换!
a[j] = a[min];
a[min] = cup;
}
console.log(a);

26. 数组遍历

let arr=[1,2,3,4,5,6,7,8]
arr.forEach(function(item,index,arr){
console.log(item);
console.log(index);
console.log(arr);
})

27. 数组映射

let arr=[1,2,3,4,5,6,7,8]
let retnum=arr.map(function(item,index,arr){
return item*10; //以return的方式书写映射条件
})
console.log(retnum);

28. 数组过滤

let arr=[1,2,3,4,5,6,7,8]
let retnum=arr.filter(function(item,index,arr){
return item>5; //以return的方式书写过滤`条件`
})
console.log(retnum);

29. 随机数

let X = Number(prompt('第一个数'));
let Y = Number(prompt('第二个数'));
let num = X+Math.random()*(Y-X+1);
document.write(num);
document.write('<br/>');
document.write(Math.floor(num));

30. 时间戳

//两个时间戳相减,再换算成时间即可
//创建时间变量
let timeBegin = new Date(1996,2,19,6,10,53);
let timeEnd = new Date(2023,4,19,18,9,26);
let birth = new Date()
//转换为时间戳
let chMS1 = timeBegin.getTime();
let chMS2 = timeEnd.getTime();
let num = chMS2-chMS1;
console.log(timeBegin);
console.log(timeEnd);
console.log('已经活了'+num+'秒了!');
birth.setTime(num);
console.log(birth);
year = birth.getFullYear() - 1970;
month = birth.getMonth() - 1;
date = birth.getDate() - 1;
hour = birth.getHours();
minutes = birth.getMinutes();
seconds = birth.getSeconds();
console.log('现在是'+year+'岁'+month+'月'+date+'天'+hour+'时'+minutes+'分'+seconds+'秒');
//转换为时间,毫秒换算成秒
let s = Math.ceil(num/1000);
//秒再换算成天
let day = Math.floor(s/(60*60*24));
//在天的基础上,天除不尽的,再想办法换算成小时、以此类推,再换算成分钟、秒
//以秒计算的,余数的单位也是秒,在秒的基础上换算成小时
let hours = Math.floor((s%60*60*24)/(60*60));
//以秒计算的,余数的单位也是秒,余数的单位是秒,在秒的基础上换算成分钟
let minutes = Math.floor((s%60*60)/60);
//以秒计算的,余数的单位也是秒,余数的单位是秒,不用换算
let seconds = s%60;
console.log(day+'天'+hours+'时'+minutes+'分'+seconds+'秒');

31. 两个日期的时间差

function spaceTime(time1,time2){
let s = Math.ceil((time2.getTime()-time1.getTime())/1000);
let day = Math.floor(s/(60*60*24));
let hours = Math.floor((s%60*60*24)/(60*60));
let minutes = Math.floor((s%60*60)/60);
let seconds = s%60;
return {day:day,hours:hours,minutes:minutes,seconds:seconds}
}
let timeBegin = new Date(1996,2,19,6,10,53);
let timeEnd = new Date(2023,4,19,18,9,26);
console.log(spaceTime(timeBegin,timeEnd));

32. 浏览器页面大小

console.log(window.innerHeight);
console.log(window.innerWidth);

33. 循环计时

let i = 0;
let timeclock = setInterval(function(){
console.log(i++);
},1000);
//滑动滚动条时,停止计时
window.onscroll = function(){
clearInterval(timeclock);
}
//改变可视窗口大小时,停止计时
window.onresize = function(){
clearTimeout(timeclock);
}