今天学了for循环还有对昨天if语句的拓展
if语句判断是否是闰年
<script> //判断是否是普通闰年还是世纪闰年 var nianFen = prompt('请输入年份') if (isNaN(nianFen*1)==true|| nianFen<0) { alert('您输入的年份有误') }else if (nianFen % 4 == 0 && nianFen % 100 !=0) { alert('是普通闰年') }else if ( nianFen % 400 == 0){ alert('是世纪闰年') }else { alert('不是闰年') } //判断是否是闰年 // var year= prompt('请输入年份')*1 // if (isNaN(year) ==true) { // alert('您输入的年份有误') // }else { // if ((year% 4 ===0 && year% 100 !== 0) || (year% 4 ===0 && year% 100 === 0)) { // alert('是闰年') // }else { // alert('不是闰年') // } // } </script>
typeof判断数据类型
<script> //typeof 测试变量类型 var x = 10; console.log(typeof x);//number var y = 'hello' console.log(typeof y);//string var z = true console.log(typeof z)//boolean; var a = undefined; console.log(typeof a);//undefined var b = null; console.log(typeof b);//object var obj = { username:'Jie', age:18 } console.log(typeof obj);//object var arr = [1,2,3,4] console.log(typeof arr);//object // var res = prompt('请输入年龄'); // console.log(typeof res );//string var res2 = confirm('确定删除吗?') console.log(typeof res2);//boolean </script>
拼接字符串和反引号
<script> var product = { pName:'MI11', pPrice:1999 } var price = product.pPrice; var pname = product.pName; //拼接字符串 document.write(price + '元起') document.write('<p class="price">'+price+'元起</p>') document.write('<p class="pname">'+pname+'手机</p>') document.write( '<div><p class="pname">'+pname+'手机</p> <p class="price">'+price+'元起</p> </div>' ) //反引号 document.write(` <div> <p class="pname">${pname}手机</p> <p class="price">${price}元起</p> </div> `) </script>
数组
<script> // 数组: 用来表示一组数据的集合 // 元素 索引(从0开始) var arr = ['a','b','c','d']; //1.根据索引访问数组中的元素 console.log(arr[1]); //2.修改数组中元素 arr[1] = 'b1'; //console.log(arr); //3.添加元素 arr[5] = 'e'; console.log(arr); console.log(arr[4]);//undefined //4.length 属性 获取数组的长度(元素的个数) console.log(arr.length);//6 </script>
运算
<script> var x = 10; var y = '5'; //var z = x+y;//105 此处是拼接字符串 //var z = x - y ;//5 - 有隐式转换的作用 // true=>1 false=>0 //var x = x - y + true;//6 var z = x * y - 'true' //NaN console.log(z); </script>
for循环语句
<script> //循环 // for (let how = 0; how < 100; how++) { // console.log(hello world);//重复执行代码 // } //for 循环的执行流程 /* var i= 0 if(i<100){执行花括号中的代码 } i++; if(i<100){执行花括号中的代码 } i++; ... if(i<100){执行花括号中的代码 } i++; if(i<100) 不符合条件 终止循环 */ // for (let ou = 0; ou < 11; ou+=2) { // console.log(ou); // } // for (let ou = 0; ou < 11; ou++) { // if (ou%2 ==0) { // console.log(ou); // } // } var html = '<ul>'; for (let i = 1; i < 11; i++){ html += `<li>item${i}</li>` } html = html+'</ul>'; document.write(html) //求0-100之间数的和 var b = 0 for (let a = 0; a <=10; a++) { b = b+a } console.log(b); </script>
遍历数据
<script> // var nameArr = ['小米1','小米2','小米3','小米4','小米5','小米6','小米7'] // for (let i = 0; i < nameArr.length; i++) { // var pname = nameArr[i]; // document.write(`<p>${pname}</p>`) // } var proArr = [ { pname:'小米1', price:1999, imgUrl:'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/61454401f855cf5ed64747a6ac04bae5.jpg?thumb=1&w=250&h=250&f=webp&q=90' },{ pname:'小米2', price:2199, imgUrl:'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/8909080ddb0851af0b87530e2927844f.jpg?thumb=1&w=250&h=250&f=webp&q=90' },{ pname:'小米11', price:3999, imgUrl:'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/5dc32ec73299ff79556dcd1cf0f0ac11.png?thumb=1&w=250&h=250&f=webp&q=90' },{ pname:'小米10', price:2599, imgUrl:'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/eb69512d9d6430d865d457ec52eebb51.png?thumb=1&w=250&h=250&f=webp&q=90' },{ pname:'小米8', price:2399, imgUrl:'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/34caee2c867bfd8c5e0dc2d8c663e121.jpg?thumb=1&w=250&h=250&f=webp&q=90' },{ pname:'小米6', price:999, imgUrl:'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/cb554f30eaa0316b0a736c3d59f21584.png?thumb=1&w=250&h=250&f=webp&q=90' },{ pname:'小米7', price:1699, imgUrl:'https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/3b7fbb6fe07907c60f08358adab5adfe.png?thumb=1&w=250&h=250&f=webp&q=90' }, ]; //要求:将以上数组上 产品的名称 显示到页面上 document.write(`<ul>`) for (let i = 0; i < proArr.length; i++) { var a = proArr[i].pname; var b = proArr[i].imgUrl; var c = proArr[i].price; document.write(` <li> <img src="${b}"> <h6>${a}</h6> <p>${c}元起</p> </li> `) } document.write('</ul>') </script>
遍历数组
<script> //遍历数组 利用for循环 获取数组中的每一组元素 var arr = ['a','b','c','d','e','f']; for (let a = 0; a < arr.length; a++) { console.log(arr[a]); } var price = [100,200,300,400,500] for (let x = 0; x < price.length; x++) { console.log(price[x]*0.8); } //[100,200,300,400,500] => [80,160,240,320,400] for (let y = 0; y < price.length; y++) { price[y] = price[y]*0.8 } console.log(price); </script>
今天留下的作业很难 真的很难感觉