第一题:封装一个输入半径求圆的面积的函数
var banJing = parseInt(prompt("请输入圆的半径")); var x = m(banJing); alert(x); function m(banJing){ if(banJing > 0){ var mianJi = banJing * banJing * Math.PI; } return mianJi; }
第二题:封装一个显示时间的函数
var x = shiJian(); function shiJian(){ var mydate = new Date(); var y = mydate.getFullYear(); //提取月份:提取的数值 +1即为当前的月份 var m = mydate.getMonth(); //提取日期 var d = mydate.getDate(); //提取当前时间:几时几分与上下午 var h = mydate.getHours(); var fen = mydate.getMinutes(); var s = mydate.getSeconds(); var z = y + "-" + (m + 1) + "-" + d + " " + h + ":" + fen + ":" + s; alert(z); return z; }
第三题、取数组中的最大值
var m = max(); function max(){ var attr = [12,65,22,01,98,32,32]; var x = 0; for(i = 0; i < attr.length;i++){ if(x < attr[i]){ x = attr[i]; }else{ x = x; } } alert("数组中最大值为:" + x); }
第四题、去重复值
var shuzu = [1,5,4,4,7,6,6]; quchong(shuzu); function quchong(shuzu){ var attr=[]; for(var i =0;i<shuzu.length;i++){ if(attr.indexOf(shuzu[i]) < 0){ attr.push(shuzu[i]); } }document.write(attr); }