JavaScript基础练习(一)
16lz
2021-01-22
- 加法的案例改为 可以做加减乘除、求余五种运算
- 为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时?
(function(a){
alert("战士连续作战"+parseInt(a/24)+"天"+a%24+"小时");
}(89));- 小明要到美国旅游,可是那里的温度是以华氏度为单位记录的。它需要一个程序将华氏温度(80度)转换为摄氏度,并以华氏度和摄氏度为单位分别显示该温度。
(function(a){
alert("华氏温度("+a+"度)"+" " +"摄氏度"+"("+tmp_trasform(a)+")");
}(80));
function tmp_trasform(tmp){
return parseInt(5/9.0*(tmp-32)*1000)/1000;
}
- vark=-2;
- 入职薪水10K,每年涨幅5%,50年后工资多少?
(function(a){
var total=a;
for(i=1;i<=50;i++)
total+=total*0.05;
alert(total);
}(10));
- 根据输入的数字,判断是否为闰年
(function(a){
alert(a%400==0||(a%100!=0&&a%4==0)?"闰年":"不是闰年")
}(2017));
- 根据输入的成绩,判定成绩的等级A\B\C\D
(function(a){
alert(core_(parseInt(a/10)))
}(100));
function core_(core){
switch(core){
case 10:
case 9: return 'A';
case 8: return 'B';
case 7: return 'C';
case 6: return 'D';
}
}
- 根据输入的数字,返回星期几
- 根据输入月份,输出每月天数
(function(a){
alert(month_(a))
}(10));
function month_(month){
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: return 31;
case 2:
if(year%400==0||(year%100!=0&&year%4==0))
return 29;
else
return 28;
default:return 30;
}
}
- 判断一个整数,属于哪个范围:大于0;小于0;等于0
(function(a){
alert( a+"的值 : "+(a>0?"大于零":a==0?"等于零":"小于零"));
}(-100));
- 判断一个整数是偶数还是奇数,并输出判断结果
(function(a){
alert( a+"的值 : "+(a%2?"是奇数":"是偶数"));
}(1001));
- 开发一款软件,根据公式(身高-108)*2=体重,可以有10斤左右的浮动。来观察测试者体重是否合适
(function(height,weight){
var tmp=(height-108)*2
alert(tmp+10>=weight?"体重合格":tmp-10<=weight?"体重合格":"体重不合格");
}(180,120));
var x =2;switch(x){case 1: console.log(1);case 2: console.log(2);default:break;case 3: console.log(3);}这段小程序的输出结果是? 结果 2
var x =2;
switch(x){
default:break;
case 1: console.log(1);
case 2: console.log(2);
case 3: console.log(3);
}
结果是:2 3
- 根据一个数字日期,判断这个日期是这一年的第几天
var date_="20140211";
var year=parseInt(Number(date_)/10000);
var month=parseInt(Number(date_)/100)%100;
var day=Number(date_)%100;
var total=0;
for(var i=month-1;i>0;i--)
total+=month_(i);
console.log(total+=day);
function month_(month){
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: return 31;
case 2:
if(year%400==0||(year%100!=0&&year%4==0))
return 29;
else
return 28;
default:return 30;
}
}
- 计算器案例
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title></title>
<style>
div{height:500px;width:500px;margin:50px auto;background#ccc;}
button{height:20px;width:20px;background:#eee;}
</style>
</head>
<body>
<div>
<label>请输入第一个数<input type="text" /></label><br/>
<label>请输入第二个数<input type="text" /></label><br/>
<button onclick="ac(this)">+</button>
<button onclick="ac(this)">-</button>
<button onclick="ac(this)">*</button>
<button onclick="ac(this)">/</button>
<br/><br/>
<input type="text" />
</div>
<script>
var arr = document.getElementsByTagName("input");
function ac(this_){
var a=arr[0].value;
var b=arr[1].value;
var a_t=a;
var b_t=b;
//alert(Boolean((a=parseInt(a))&&(b=parseInt(b))));
//((a=Number(a_t))&&(b=parseInt(b_t)))
//x((a=parseInt(a_t))&&(b=Number(b_t)))
if(!(((a=Number(a_t))&&(b=Number(b_t)))||((a=Number(a_t))&&(b=parseInt(b_t)))||((a=parseInt(a_t))&&(b=Number(b_t)))||((a=parseInt(a_t))&&(b=parseInt(b_t)))
)){
alert("请输入正确的数字");
return;
}
var res=0;
switch(this_.innerHTML){
case '+' : res=a+b;break;
case '-' : res=a-b;break;
case '*' : res=a*b;break;
case '/' : res=a/b;break;
default: break;
}
arr[2].value=res;
}
</script>
</body>
</html>
- 编写一个页面表单,使用JS判断输入内容是否合法
<styletype="text/css">*{padding:0;margin:0;}html,body{width:100%;height:100%;background:#9CBC2C;font-family:"微软雅黑";}ul,ol,input{list-style:none;border:none;}.main{width:500px;margin:50pxauto;}.main.step{padding-left:43px;color:#384313;font-size:16px;font-weight:bold;text-shadow:01px1px#c0d576;line-height:40px;}.mainulli{width:400px;height:25px;padding-left:10px;padding-top:5px;padding-bottom:5px;background:rgba(255,255,255,.3);margin:auto;border:solid2px#e3ebc3;border-color:rgba(255,255,255,.6);border-radius:5px;margin-bottom:4px;}.mainullilabel{font-size:13px;width:110px;color:#111111;padding-left:5px;}.mainulliinput{border-radius:3px;border:#FC3solid1px;width:250px;height:25px;outline:none;padding-left:5px;margin-left:35px;}.main.savebtn{display:block;width:140px;height:40px;line-height:40px;text-align:center;color:#E3EBC3;font-weight:bold;cursor:pointer;border-radius:5px;margin:10pxauto;box-shadow:1px1px1px0pxgray;background:linear-gradient(#50B9FD,#1158A8);}</style><body><divclass="main"><spanclass="step">Step1:用户详细资料</span><ul><li><label>用户名称:</label><inputtype="text"placeholder="请输入用户名"/></li><li><label>邮件地址:</label><inputtype="text"placeholder="example@qq.com"/></li><li><label>练习电话:</label><inputtype="text"placeholder="13539008452"/></li></ul><spanclass="step">Step2:家庭住址(收货地址)</span><ul><li><label>详细地址:</label><inputtype="text"placeholder="请输入用户名"/></li><li><label>邮政编码:</label><inputtype="text"placeholder="example@qq.com"/></li><li><label>国 家:</label><inputtype="text"placeholder="13539008452"/></li></ul><spanclass="savebtn">保存</span></div></body> |
更多相关文章
- 字体图标的引入和通过媒体查询改变导航样式
- HTML样式和常用选择器
- 字体图标的引用和自定义样式/媒体查询的使用
- 数据库的CURD操作、PDO本质与原理的学习
- CSS之伪类选择器和简单盒子简单案例
- 伪类选择器与盒模型常用属性
- 伪类选择器-结构伪类、根据位置选择匹配
- 7.4——常用标签与应用场景之表格与单元格
- css伪类选择器和盒模型