JavaScript Day2

本文介绍了JavaScript中的for循环和while循环的使用场景,展示了如何进行断点调试。接着讲解了数组的增删改查操作,包括push、unshift、pop、shift和splice方法。此外,文章还涵盖了函数的概念,强调其在代码复用中的重要性,以及函数参数、返回值和作用域的相关知识。
摘要由CSDN通过智能技术生成

JavaScript Day2(基础)

for 循环

断点调试需要重新刷新页面

for循环和while循环有什么区别呢:

当如果明确了循环的次数的时候推荐使用for循环

当不明确循环的次数的时候推荐使用while循环

for (let i = 1; i <= 3; i++){
    document.write(`${i}天<br>`)
    for (let j = 1; j <=5; j++){
        document.write(`记住了第${j}个单词<br>`)
    }
}
let row = +prompt('请输入行数')
let col = +prompt('请输入列数')
//外层循环打印行数
for (let i = 1; i <=row; i++){
    //里面循环打印几个星星
    for (let j = 1; j <= col; j++){
        document.write('☆')
    }
    //进行换行显示
    document.write('<br>')
}
//九九乘法表
document.write('<table style="border-collapse:collapse">')
for (let i = 1; i <= 9; i++){
    document.write('<tr>')
    for (let j = 1; j <= i; j++){
        document.write(`<td style="border: 2px black solid; padding: 5px">${j}×${i}=${j*i}</td>`)
    }
    document.write('</tr>')
}

数组

数组.push()是在数组末尾添加一个或多个元素,返回值是数组的长度

let arr = [1,2,3]
arr.push(4,5,'6')
console.log(arr) //(6) [1, 2, 3, 4, 5, '6']
console.log(arr.push(7)) //7
let arr = [2,0,6,1,77,0,52,0,25,7] //筛选数组, >= 10
let newArr = []
for (i = 1; i < arr.length; i++){
    if (arr[i] >= 10){
        newArr.push(arr[i])
    }
}
console.log(newArr)

数组.unshift()是在数组开头添加一个或多个元素,返回值是数组的长度

let arr = [1,2,3]
arr.unshift(4,5,'6')
console.log(arr) //(6) [4, 5, '6', 1, 2, 3]
console.log(arr.unshift(7)) //7

数组.pop()方法从数组中删除最后一个元素,并返回该元素的值

数组.shift()方法从数组中删除第一个元素,并返回该元素的值

数组.splice()方法删除指定元素:

arr.splice(起始位置,删除几个元素)

let arr = [1,2,3,4,5,6]
console.log(arr.pop()) //6
console.log(arr) //(5) [1, 2, 3, 4, 5]
console.log(arr.shift()) //1
console.log(arr) //(4) [2, 3, 4, 5]
console.log(arr.splice(1,1)) //[3]
console.log(arr) //(3) [2, 4, 5]

splice另外两种用法:

  1. 增加 arr.splice(开始位置,0,插入的内容)

  2. 替换的内容 arr.splice(开始位置,要删除元素的数量,要插入的任意多个元素**)**

改、查

数组[下标值]

实例

实例一

柱状图

css

* {
    margin: 0;
    padding: 0;
}
.box {
    display: flex;
    width: 700px;
    height: 300px;
    border-left: 1px solid pi
    border-bottom: 1px solid 
    margin: 50px auto;
    justify-content: space-ar
    align-items: flex-end;
    text-align: center;
}
.box>div {
    display: flex;
    width: 50px;
    background-color: pink;
    flex-direction: column;
    justify-content: space-be
}
.box div span {
    margin-top: -20px;
}
.box div h4 {
    margin-bottom: -35px;
    width: 70px;
    margin-left: -10px;
}

js

let arr = []
for(let i = 1; i <=4; i++){
    arr.push(prompt(`请输入第${i}季度的数据:`))
}
console.log(arr)
document.write('<div class="box">')
for (let j = 0; j< arr.length; j++){
    document.write(`
    <div style="height: ${arr[j]}px;">
    <span>${arr[j]}</span>
    <h4>第${j + 1}季度</h4>
    </div>
    `)
}
document.write('</div>')

效果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-13R5VTl2-1678810218494)(C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\image-20230311205407573.png)]

实例二

冒泡排序:
let arr = [5,42,32,32,1]
for (let i = 1; i <= arr.length-1; i++) {
    for (let j = 0; j <= arr.length-i-1; j++){
        if (arr[j] > arr[j+1]){
            let temp = arr[j]
            arr[j] = arr[j+1]
            arr[j+1] = temp
        }
    }
}
console.log(arr.sort())
sort 排序:
let arr = [5,42,32,32,1]
arr.sort(function (a,b){
    return a - b
})
console.log(arr)
//sort 降序排列
arr.sort(function (a,b){
    return b - a
})
console.log(arr)

函数

实现代码复用

命名规范基本与变量名命名一致,前缀为动词

函数的复用代码和循环重复代码不同之处:

循环代码写完即执行,不能很方便控制执行位置;

函数随时调用,随时执行,可重复调用.

函数参数:

函数声明时,小括号里面的是形参,形式上的参数;

函数调用时,小括号里面的是实参,实际的参数.

参数默认值

程序更严谨

// function getSum(x,y){
//     document.write(x + y)
// }
// getSum() //NaN,undefined + undefined
function getSum(x = 0,y = 0){
    document.write(x + y)
}
getSum() //0

return

在函数体中使用return关键字能将内部的执行结果交给函数外部使用

return后面代码不会再被执行,会立即结束当前函数,所以return后面的数据不要换行写

return函数可以没有return,这种情况函数默认返回值为undefined

使用数组可以return多个值

//打印数组最大值及最小值
function getArrValue(arr = []){
    let max = arr[0]
    let min = arr[0]
    for (let i = 1; i < arr.length; i++){
        if (arr[i] > max) {
            max = arr[i]
        }
        if (arr[i] < min) {
            min = arr[i]
        }
    }
    return [max, min] //返回数组
}
let newArr = getArrValue([11,1,23,2,34])
console.log(`数组的最大值是: ${newArr[0]}`)
console.log(`数组的最小值是: ${newArr[1]}`)

tips:

  1. 形参作用:本质上就是在函数内部声明变量

​ 实参作用:给形参赋值

  1. 两个相同的函数后面的会覆盖前面的函数

  2. 在Javascript中实参的个数和形参的个数可以不一致

​ 如果形参过多会自动填上undefined (了解即可);

​ 如果实参过多那么多余的实参会被忽略(函数内部有一个arguments,里面装着所有的实参).

作用域

局部变量或者块级变量没有let声明直接赋值的当全局变量看(强烈不提倡)

还有一种特殊情况,函数内部的形参可以当做局部变量看

访问原则:在能够访问到的情况下先局部,局部没有,再找全(作用域链:采取就近原则的方式来查找变量最终的值)

匿名函数

函数表达式
let fn = function (x, y) {
    console.log(x + y)
}
fn(1, 2)

函数表达式和具名函数的不同:

具名函数的调用可以写到任何位置

函数表达式必须先声明后使用

立即执行函数

防止变量影响

无需调用,立即执行,其实本质已经调用了

多个立即执行函数之间用分号隔开

//方式1 (function(){})();
(function (x, y){
    console.log(x + y)
})(1, 2);
//方式2 (function(){}());
(function(x, y){
    console.log(x + y)
}(1, 3));

倒计时基础铺垫

//1.用户输入
let second = +prompt('请输入秒数:')
//2. 封装函数
function getTime(t) {
    //3. 转换
    //小时: h = parseInt(总秒数 / 60 / 60 % 24)
    //分钟: m = parseInt(总秒数 / 60 % 60)
    //秒数: s = parseInt(总秒数 % 60)
    let h = parseInt(t / 60 / 60 % 24)
    let m = parseInt(t / 60 % 60)
    let s = parseInt(t % 60)
    h = h < 10 ? '0' + h : h
    m = m < 10 ? '0' + m : m
    s = s < 10 ? '0' + s : s
    // console.log(h, m, s)
    return `转换完毕之后是${h}小时${m}分钟${s}`
}
let str = getTime(second)
document.write(str)

补充:

&& || 逻辑中断

&& 左边为false就短路

|| 左边为true就短路

console.log(11 && 22) //22,都是真,返回最后一个真值
console.log(undefined && 20) //undefined
console.log(null && 20) //null
console.log(0 && 20) //0
console.log(11 || 22) //11,都是真,返回第一个真值
console.log(false || 20) //20
console.log(undefined || 20) //20
//变相设置函数形参默认值
function fn(x, y){
    x = x || 0
    y = y || 0
    console.log(x + y)
}
fn() //x没有传值为undefined,逻辑运算为false,||结果取后面的值即0
fn(1,2) //x为1逻辑运算为true,||逻辑中断
(function flexible(window, document) {
//window.devicePixeLRatio 获取当前设备的dpr
//获取不到,则默认取值为1
//移动端 获取为2,则执行2
var dpr = window.devicePixelRatio || 1
}(window, document))

if () Boolean()

//以下结果全为false
console.log(Boolean(''))
console.log(Boolean(0))
console.log(Boolean(undefined))
console.log(Boolean(null))
console.log(Boolean(false))
console.log(Boolean(NaN))

减法–(像大多数数学运算一样)只能用于数字

它会使空字符串""转换为0

null经过数字转换之后会变为0

undefined经过数字转换之后会变为NaN

console.log('' - 1) //-1
console.log('as' - 1) //NaN
console.log(null - 1) //-1
console.log(null + 1) //1
console.log(undefined - 1) //NaN
console.log(NaN - 1) //NaN
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值