Correction status:Uncorrected
Teacher's comments:
<script>
//变量
var a = '张明轩';
console.log(a);
//for循环
for(var i=0;i<10;i++){
console.log(i);//输出1-9
}
//while循环
var i=0;
while(i<10){
i++; //输出0-10
console.log('while'+i);
i++; //输出0-9
}
//do while 循环
var i=0;
do{
i++;
console.log('do'+i);//输出 1-10;先执行后判断,故含10
}while(i<10)
//函数与变量
var abc = '张明轩';
function show()
{
var abc = '张明朗';
function run()
{
console.log(abc); //张明朗
}
run();
console.log(abc); //张明朗
}
show();
console.log(abc); //张明轩
//三元 三目运算符
var num1 = 10;
var num2 = 20;
console.log(num1>num2 ? 'num1大于num2' : 'num1小于num2');
//函数与判断
function show1()
{
$result = 70;
if($result>90){
console.log('优秀');
}elseif($result>80 && $result<=90){
console.log('良好');
}else if($result>60 && $result<=80){
console.log('合格');
}else{
console.log('差');
}
}
show1();
function show2()
{
var myscore = 71;
switch( true ){
case myscore>90:console.log('优秀');
break;
case myscore<=90 && myscore>70:console.log('良好');
break;
case myscore<=70 && myscore>60:console.log('及格');
break;
default:console.log('不及格');
}
}
show2();
//推荐使用一目了然模式
function show3()
{
if(myscore>90){
console.log('优秀');
}
if(myscore<=90 && myscore>70){
console.log('良好');
}
if(myscore<=70 && myscore>=60){
console.log('及格');
}
if(myscore<60){
console.log('不及格');
}
}
show3();
//数组循环
function show4()
{
var arr = ['a','b','c','dd'];
for(let i=0;i<arr.length;i++){
console.log(arr[i]);
}
}
show4();
parseInt() 函数可解析一个字符串,并返回一个整数。
var aa = '123abc';
console.log(aa); //123abc
console.log(parseInt(aa)); //123
break 与 continue
var bb = 10;
while(bb>0){
bb--;
if(bb == 7){
break; //这里如果是continue
}
console.log(bb); // 9 8
}
var bb = 10;
while(bb>0){
bb--;
if(bb == 7){
continue;
}
console.log(bb); // 9 8 6 5 4 3 2 1 0
}
</script>
<input type="text" name="" id="age">
<button onclick="save()">提交</button>
<script>
function save(){
var age = document.getElementById('age').value;
age = parseInt(age); //字符串转换成数字
if(isNaN(age)){ // isNaN() 函数用于检查其参数是否是非数字值。
return alert('请输入数字');
}
if(age>100 || age<0){
console.log('请输入正确的年龄');
return false;
}
console.log(age);
}
</script>