Correction status:Uncorrected
Teacher's comments:
//js变量声明 函数定义
var num1 = 111;
var num2 = 222;
//js的输出
//alert()系统弹窗 会阻塞浏览器运行
//console.log()控制台输出 不阻塞浏览器运行
function demo(){
console.log(num1);
console.log(num2);
}
demo();
//流程控制
function myfunc(){
var score = 60;
var myscore = 98;
if(myscore>90){
alert('优秀');
}else if(myscore<=90 && myscore>70){
alert('一般');
}else if(myscore<=70 && myscore>=60){
alert('及格');
}else{
alert('不及格');
}
}
function myfunc2(){
var score = 60;
var myscore = 98;
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('不及格');
}
}
function myfunc3(){
var score = 60;
var myscore = 98;
if (myscore>90) {
return alert('优秀');
}
if (myscore<=90 && myscore>=70) {
return alert('一般');
}
if (myscore<=70 && myscore>=60) {
return alert('及格');
}
if (myscore<60) {
return alert('不及格');
}
}
function loop1(){
for(var i=0;i<10;i++){
console.log('i='+i);
}
}
function loop2(){
var flag = 10;
while(flag>0){
flag--;
console.log(flag);
}
}
function loop3(){
var flag = 10;
do{
flag--;
console.log(flag);
}
while(flag>0);
}
<input type="text" id="username" value="" placeholder="请输入您的年龄">
<button onclick="save()">提交</button>
<script type="text/javascript">
function save(){
var age = document.getElementById('username').value;
age = parseInt(age);
if(isNaN(age)){
alert('年龄转换失败');
return;
}
if(age > 100 || age < 0){
alert('请确认您的年龄');
return;
}
console.log(typeof(age));
alert('您的年龄是'+age);
}
//类型转换
var age = '20';
console.log(typeof(age)); //string
console.log(isNaN(age)); //false
age = parseInt(age);
console.log(typeof(age)); //number
console.log(isNaN(age)); //false
var age = 'abcd';
console.log(isNaN(age)); //true
var age = '111aaa';
console.log(isNaN(age)); //true
通过简单的学习,初步了解了JavaScript的一些基本概念以及与其他编程语言的区别。