JavaScript 学习手册二:JS 数据类型
第1关:JavaScript 数据类型介绍
本关的编程任务是补全右侧代码片段中 Begin-End 中的代码,具体要求如下:
在函数 objectTest() 内部定义了六个变量a、b、c、d、e、f,并已经赋值,你需要判断它们各是什么数据类型;
变量 aType、bType、cType、dType、eType、fType 分别表示上面六个变量的数据类型的名字,你需要给他们赋值。比如变量 a 如果是数字类型,需要给变量 aType 赋值字符串 number;
可选的数据类型名如下:
number、string、bool、object、undefined 和 array 分别表示数字、字符串、布尔型、对象类型、undefined 还有数组。
解决:
function objectTest() {
var a = {name:“myName”,id:2};
var b = [1,true,2];
var c = 1;
var d = “1”;
var e = true;
var f;
var aType,bType,cType,dType,eType,fType;
//请在此处编写代码
/********** Begin **********/
aType=“object”;
bType=“array”;
cType=“number”;
dType=“string”;
eType=“bool”;
fType=“undefined”;
console.log(aType);
console.log(bType);
console.log(cType);
console.log(dType);
console.log(eType);
console.log(fType);
/********** End **********/
return aType+","+bType+","+cType+","+dType+","+eType+","+fType;
}
第2关:JavaScript 数据类型转换
本关的编程任务是补全右侧代码片段中 Begin-End 中的代码,具体要求如下:
完成函数 mainJs();
把函数三个参数(从左到右)依次转换为整数,整数和小数;
第一个参数既有可能是 12 这种纯整数的字符串形式,也有可能是 12a3 这种含有非数字字符的字符串;
第二个参数是 16 进制数字的字符串形式,如 af2;
第三个参数是纯小数的字符串形式,如 12.2;
具体请参见后续测试样例。
function mainJs(args1,args2,args3) {
//请在此处编写代码
/********** Begin /
var a=parseInt(args1);
var b= parseInt(args2,16);
var c= parseFloat(args3);
/ End **********/
return a+b+c;
}