小练习之求100以下的被5整除的数字的和,以及他们的平均值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>作业:求100一下的被5整除的数字的和,及他们的平均值</title> </head> <body> </body> <script> var arr = []; //声明空数组,用来装符合条件的数组 for (var i = 1; i < 100; i++) { if (i % 5 == 0) { arr.push(i); //吧符合条件的数据装入数组 } } var sum = 0; for (var i = 0; i < arr.length; i++) { //ba数组的所有元素求和 sum += arr[i]; } alert(sum); alert(sum / arr.length); </script> </html>
排序练习
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>排序练习</title> </head> <body> </body> <script> function sortNumber(a,b) { return b - a; } var arr = [3,5,58,34,99,43,77]; var newArr = arr.sort(sortNumber); document.write(newArr); var newARR = arr.reverse(); document.write("<br>"); document.write(newARR); </script> </html>
传值和传引用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>传值和传引用</title> </head> <body> </body> <script> /*基本数据类型: 传值*/ /*var zhangHeight = 180; var liHeight = zhangHeight; zhangHeight = 185; alert(liHeight);*/ // /*引用数据类型: 传引用*/ // var zhangSan = [180, 75]; // var liSi = zhangSan; // zhangSan[0] = 185; // zhangSan[1] = 80; // alert(liSi[0]); // alert(liSi[1]); //传值和传引用运行一下就明白了 栈和堆 </script> </html>
转换成字符串
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>转换成字符串</title> </head> <body> </body> <script> // var num = new Number(5); // var num = 5; //这两句话意义相同 // alert(Number.MAX_VALUE); //计算机数字中最大值 // alert(Number.MIN_VALUE); // alert(Number.NEGATIVE_INFINITY); //无穷小 // alert(Number.POSITIVE_INFINITY); //无穷大 // // // var i = 5; // var j; // var result = i +j; // alert(result); //not a number NaN var i = 5; var result = i.toString(); //把数字转换成字符串 alert(typeof (result)); var arr = ["zhangsan","lisi"]; var result1 = arr.toString(); console.log(typeof result1); //把数组转化成字符串 var b = true; var result2 = b.toString(); //把布尔类型的转化成字符串 console.log(result2); </script> </html>
字符串
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>字符串</title> </head> <body> </body> <script> // var s = "杰瑞教育"; // console.log(s.length); //把字符串当作是字符数组 // // var s1 = "ccy"; // console.log(s1.toUpperCase()); //把小写转换成大写 // // var s2 = "JREDU"; // console.log(s2.toLowerCase()); //吧大写转换成小写 // // var s3 = "jerei"; // console.log(s3.charAt(2)); //获取字符串中制定索引处的字符 // // var s4 = "HelloWorld"; // //返回字某个字符串第一次出现的索引位置, // //参数1:要查找的子元素 // //参数2:查找的开始位置 // console.log(s4.indexOf("o",5)); //subString:与数组的slice方法类似,用于截取字符串的某一段子字符 //参数1:截取的开始索引(包含) //参数2:截取的结束索引(不包含) var s5 = "this is js"; result = s5.substring(5, 7); console.log(result); </script> </html>
日期属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>日期</title> </head> <body> <div id="time"></div> </body> <script> setInterval(function () { var date = new Date(); var year = date.getFullYear(); //获取年 console.log(year); var month = date.getMonth() + 1; //获取月份 +1 console.log(month); var day = date.getDate(); //获取日期 console.log(day); var hour = date.getHours(); //获取时 console.log(hour); var min = date.getMinutes(); //获取分 console.log(min); var sec = date.getSeconds(); //获取秒 console.log(sec); var mSec = date.getMilliseconds(); //获取毫秒 console.log(mSec); var week = date.getDay() == 0 ? 7 : date.getDay(); console.log(week); var s = year + "年" + month + "月" + day + "日" + " " + hour + ":" + min + ":" + sec + ":" + mSec + " " + "星期" + week document.getElementById("time").innerHTML = s; }, 1000); </script> </html>
数学类
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>数学类</title> </head> <body> </body> <script> var pi = Math.PI; console.log(pi); var r = Math.random(); //随机生成一个0-1之间的数 console.log(Math.floor(r*11)); //floor;向下取整 忽略小数取整 var i = -3.5; console.log(Math.abs(i)); //取绝对值 console.log(Math.floor(i)); //floor;向下取整 忽略小数取整 console.log(Math.ceil(i)); //ceil:向上取整 小数不为0.整数就+1 console.log(Math.round(i)); //四舍五入取值 var result = Math.max(8,9,23,444,555); //比较多个数值得最大值 var result2 = Math.min(8,9,23,444,555); //比较多个数值得最小值 console.log(result); console.log(result2); var i2 = 5; var result3 = Math.pow(i2,3); console.log(result3); </script> </html>
对象!!!
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>对象</title> </head> <body> </body> <script> var zhangSan = new Object(); zhangSan.name = "zhangsan"; zhangSan.age = "25"; zhangSan.height = "160"; zhangSan.eat = function () { alert("wohuichi!"); }; var zhangSan = { name: "zhangSan", age: "25", height: "160", eat: function () { alert("wohuichi!"); } }; //于上方一样 delete zhangSan.name; //删除对象属性 showMessage(zhangSan); function showMessage(person) { console.log(person.name); console.log(person.age); console.log(person.height); } </script> </html>