020TypeConversionandCoercion 视频加载中。。。 本讲主要讲类型转换和类型强制转换。 数字转换为字符串,使用String函数 字符串转换为数字,使用Number函数 类型强制转换,语言自身实现 constinputYear1991;console。log(inputYear18); 在控制台输出:199118 字符串与数字用加号做计算,实际是连接字符串和数字。 constinputYear1991;console。log(Number(iniputYear),inputYear);console。log(Number(inputYear)18); 在控制台输出:199119912009 使用Number函数可以将字符串类型转变为数字类型。类型转换不会改变原有变量或者值的类型。 console。log(Number(Jonas)); 在控制台输出:NaN 使用Number函数转换字符串为数字类型,并不是说对所有的字符串都生效。对于名字或者单词这样的纯字符串,使用Number转换后得到NaN,意思是NotaNumber,不是数字。 console。log(typeofNaN); 在控制台输出:number 对于返回的结果NaN,实际上它也是number类型,只是通过它来表示无效的数字。 console。log(String(23),23); 在控制台输出:2323 使用String函数可以把数字类型转换为字符串类型。在控制台输出的两个23,前一个是字符串类型,后一个是数字类型。它们的颜色上有区别。 console。log(Iam23yearsold);console。log(23103); 在控制台输出:Iam23yearsold10 对于字符串和数值用加号连接,会触发系统将数值类型强制转换为字符串。但是,并不是所有的操作符会触发数值强制转换为字符串。对于减号操作符,字符串23减去字符串10减去数值3,可以得到数值类型10。 letn11;nn1;console。log(n); 在控制台输出:10 对于乘法、除法操作符,对于数字加引号的字符串也可以强制转换为数字类型。所以,只有加号有特殊用法,将数字强制转换为字符串,用于连接字符串。 021TruthyandFalsyValues 视频加载中。。。 本讲主要学习涉及布尔类型的转换。 五种假值:0,,undefined,null,NaN console。log(Boolean(0));console。log(Boolean(undefined));console。log(Boolean(Jonas));console。log({}); 在控制台输出:falsefalsetruetrue constmoney0;if(money){console。log(Dontspenditall);}else{console。log(Youshouldgetajob!);} 在控制台输出:Youshouldgetajob! money变量为0,当if后作为布尔值进行处理的时候,强制类型转换为布尔值,因为0是假值,所以执行else后代码块内容。 letheight;if(height){console。log(YAY!Heightisdefined);}else{console。log(HeightisUNDEFINED);} 在控制台输出:HeightisUNDEFINED 变量height定义后未赋值,所以它的类型是undefined。 在if后将类型强制转换为布尔值,因为undefined为假,所以执行else后的代码块。 对于变量height如果初始化为0,也会得到同样的结果。因为0对于布尔值来说也是假。 所以,写代码要注意这样的问题。 022EqualityOperatorsvs。 视频加载中。。。 本讲主要学习等号运算符,与使用差异。 ifelseelseif补充用法。 不等运算符,!和!差异。 constage18;if(age18)console。log(Youjustbecameanadult:D); 在控制台输出:Youjustbecameanadult:D 变量age初始化为18,通过运算符比较相等,输出预期内容。如果变量age的初始值变为19,则通过运算符比较不相等,不会输出任何内容。 运算符的结果为true或者false。三个等号运算符两边值相等,则为true。 一个等号是赋值操作,三个等号是比较运算操作。 constage18;if(age18)console。log(Youjustbecameanadult:D(strict));if(age18)console。log(Youjustbecameanadult:D(loose)); 在控制台输出:Youjustbecameanadult:D(strict)Youjustbecameanadult:D(loose) constage18;if(age18)console。log(Youjustbecameanadult:D(strict));if(age18)console。log(Youjustbecameanadult:D(loose)); 在控制台输出:Youjustbecameanadult:D(loose) 修改变量age值为字符串18,可以看到控制台输出只有一行记录。对于操作符不满足布尔值为真,对于操作符,仍然满足。因为操作符默认类型强制转换。 constfavouriteprompt(Whatsyourfavouritenumber?);console。log(favourite); 在弹出的提示框中输入数字23。 在控制台输出:23 23在这里是字符串类型。 constfavouriteNumber(prompt(Whatsyourfavouritenumber?));console。log(favourite);console。log(typeoffavorite);if(favorite23){console。log(Cool!23isanamazingnumber!);} 在弹出的提示框中输入数字23。 在控制台输出:23numberCool!23isanamazingnumber! 使用Number函数,输入23是字符串类型转换为数字类型。操作符严格比对数字23相等为真,所以执行if后代码块内容。 如果不使用Number函数进行类型转换,操作符严格比对字符串23与数字23不相等,if判断为假,无任何输出。 constfavouriteNumber(prompt(Whatsyourfavouritenumber?));console。log(favourite);console。log(typeoffavorite);if(favorite23){console。log(Cool!23isanamazingnumber!);}elseif(favourite7){console。log(7isalsoacoolnumber)}elseif(favourite9){console。log(9isalsoacoolnumber)}else{console。log(Numberisnot23or7or9)} 在弹出的提示框中输入数字9。 在控制台输出:9number9isalsoacoolnumber 除了了ifelse之外,还可以继续使用elseif来增加分支判断。如果if后面判断条件为假,不是立即进入else代码块,而是进入elseif后的判断条件继续检查。 constfavouriteNumber(prompt(Whatsyourfavouritenumber?));console。log(favourite);console。log(typeoffavorite);if(favorite23){console。log(Cool!23isanamazingnumber!);}elseif(favourite7){console。log(7isalsoacoolnumber)}elseif(favourite9){console。log(9isalsoacoolnumber)}else{console。log(Numberisnot23or7or9)}if(favourite!23)console。log(Whynot23?); 在弹出的提示框中输入数字9。 在控制台输出:9number9isalsoacoolnumberWhynot23? 建议使用!是严格检查不匹配。输入变量值为9与23不等,所以输出Whynot23? 023BooleanLogic 视频加载中。。。 本讲主要学习布尔逻辑。 逻辑运算符:AND()、OR()、NOT(!) A是TRUE,B是TRUE,AB为TRUE。 A是TRUE,B是FALSE,AB为FALSE。 A是FALSE,B是TRUE,AB为FALSE。 A是FALSE,B是FALSE,AB为FALSE。 A是TRUE,B是TRUE,AB为TRUE。 A是TRUE,B是FALSE,AB为TRUE。 A是FALSE,B是TRUE,AB为TRUE。 A是FALSE,B是FALSE,AB为FALSE。 A是TRUE,!A是FALSE。 A是FALSE,!A是TRUE。 024LogicalOperators 视频加载中。。。 本讲主要学习逻辑操作验证。 consthasDriverLicensetrue;AconsthasGoodVisiontrue;Bconsole。log(hasDriverLicensehasGoodVision); 在控制台输出:true 两个变量都为true,进行与操作还是true。 consthasDriverLicensetrue;AconsthasGoodVisionfalse;Bconsole。log(hasDriverLicensehasGoodVision); 在控制台输出:false 一个变量为true,一个变量为false,进行与操作是false。 consthasDriverLicensetrue;AconsthasGoodVisionfalse;Bconsole。log(hasDriverLicensehasGoodVision);console。log(hasDriverLicensehasGoodVision); 在控制台输出:falsetrue 一个变量为true,一个变量为false,进行与操作是false。 consthasDriverLicensetrue;AconsthasGoodVisionfalse;Bconsole。log(hasDriverLicensehasGoodVision);console。log(hasDriverLicensehasGoodVision);console。log(!hasDriversLicense); 在控制台输出:falsetruefalse !就是NOT操作,true的!操作结果是false。 consthasDriverLicensetrue;AconsthasGoodVisionfalse;Bconsole。log(hasDriverLicensehasGoodVision);console。log(hasDriverLicensehasGoodVision);console。log(!hasDriversLicense);if(hasDriversLicensehasGoodVision){console。log(Sarahisabletodrive!);}else{console。log(Someoneelseshoulddrive。。。);} 在控制台输出:falsetruefalseSomeoneelseshoulddrive。。。 因为hasGoodVision为false,所以if后的条件判断为false,执行else后代码块内容。 consthasDriverLicensetrue;AconsthasGoodVisiontrue;Bconsole。log(hasDriverLicensehasGoodVision);console。log(hasDriverLicensehasGoodVision);console。log(!hasDriversLicense);if(hasDriversLicensehasGoodVision){console。log(Sarahisabletodrive!);}else{console。log(Someoneelseshoulddrive。。。);} 在控制台输出:truetruefalseSarahisabletodrive! 因为hasDriversLicense和hasGoodVision都为true,所以if后的条件判断为true,执行if后代码块内容。 consthasDriverLicensetrue;AconsthasGoodVisiontrue;Bconsole。log(hasDriverLicensehasGoodVision);console。log(hasDriverLicensehasGoodVision);console。log(!hasDriversLicense);if(hasDriversLicensehasGoodVision){console。log(Sarahisabletodrive!);}else{console。log(Someoneelseshoulddrive。。。);}constisTiredfalse;Cconsole。log(hasDriverLicensehasGoodVisionisTired); 在控制台输出:truetruefalseSarahisabletodrive!false 因为isTired为false,hasDriversLicense和hasGoodVision为true,三个变量相与,只要有一个false,最终为false。 consthasDriverLicensetrue;AconsthasGoodVisiontrue;Bconsole。log(hasDriverLicensehasGoodVision);console。log(hasDriverLicensehasGoodVision);console。log(!hasDriversLicense);constisTiredtrue;Cconsole。log(hasDriverLicensehasGoodVisionisTired);if(hasDriversLicensehasGoodVision!isTired){console。log(Sarahisabletodrive!);}else{console。log(Someoneelseshoulddrive。。。);} 在控制台输出:truetruefalsetrueSomeoneelseshoulddrive。。。 如果isTired初始值为true,则if后判断条件为false,所以执行else后代码块。 consthasDriverLicensetrue;AconsthasGoodVisiontrue;Bconsole。log(hasDriverLicensehasGoodVision);console。log(hasDriverLicensehasGoodVision);console。log(!hasDriversLicense);constisTiredfalse;Cconsole。log(hasDriverLicensehasGoodVisionisTired);if(hasDriversLicensehasGoodVision!isTired){console。log(Sarahisabletodrive!);}else{console。log(Someoneelseshoulddrive。。。);} 在控制台输出:truetruefalsetrueSomeoneelseshoulddrive。。。 如果isTrue初始值为false,if后判断条件为true,则继续执行if后代码块。 025CodingChallenge3 视频加载中。。。 CodingChallenge3 Therearetwogymnasticsteams,DolphinsandKoalas。Theycompeteagainsteachother3times。Thewinnerwiththehighestaveragescorewinsatrophy! 有两个体操队,海豚队和考拉队。他们进行三次比赛。平均分最高的队伍获胜。 Yourtasks: 1。Calculatetheaveragescoreforeachteam,usingthetestdatabelow 每队使用以下测试数据进行平均分计算。 2。Comparetheteamsaveragescorestodeterminethewinnerofthecompetition,andprintittotheconsole。Dontforgetthattherecanbeadraw,sotestforthataswell(drawmeanstheyhavethesameaveragescore) 比较两队的平均分来决定获胜方,打印到控制台。不要忘了两队平均分可能相同的情况。 3。Bonus1:Includearequirementforaminimumscoreof100。Withthisrule,ateamonlywinsifithasahigherscorethantheotherteam,andthesametimeascoreofatleast100points。Hint:Usealogicaloperatortotestforminimumscore,aswellasmultipleelseifblocks 奖励1:需要包含最低分100。提示,使用逻辑操作来测试最低分,也可以用elseif块结构。 4。Bonus2:Minimumscorealsoappliestoadraw!Soadrawonlyhappenswhenbothteamshavethesamescoreandbothhaveascoregreaterorequal100points。Otherwise,noteamwinsthetrophy 奖励2:分数大于或等于100的时候,也要考虑相同的情况。否则,没有队伍获胜。 Testdata: Data1:Dolphinsscore96,108and89。Koalasscore88,91and110 数据1:海豚队分数96,108和89。考拉队分数88,91和110。 DataBonus1:Dolphinsscore97,112and101。Koalasscore109,95and123 奖励1数据:海豚队分数97,112和101。考拉队分数109,95和123。 DataBonus2:Dolphinsscore97,112and101。Koalasscore109,95and106 奖励2数据:海豚队分数97,112和101。考拉队分数109,95和106。 GOODLUCK constscoreDolphins(9610889)3;constscoreKoalas(8891110)3;console。log(scoreDolphins,scoreKoalas); 控制台输出:97。6666666666666796。33333333333333 按照数据1给出的分数,分别计算海豚队和考拉队的平均分。 constscoreDolphins(9610889)3;constscoreKoalas(8891110)3;console。log(scoreDolphins,scoreKoalas);if(scoreDolphinsscoreKoalas){console。log(Dolphinswinthetrophyt);}elseif(scoreKoalasscoreDolphins){console。log(Koalaswinthetrophyt);}elseif(scoreKoalasscoreDolphins){console。log(Bothwinthetrophy!);} 控制台输出:97。6666666666666796。33333333333333Dolphinswinthetrophyt 通过ifelseif控制结构来分别判断三种情况,输出不同的结果。 constscoreDolphins(9711281)3;constscoreKoalas(1099586)3;console。log(scoreDolphins,scoreKoalas);if(scoreDolphinsscoreKoalasscoreDolphins100){console。log(Dolphinswinthetrophyt);}elseif(scoreKoalasscoreDolphinsscoreKoalas100){console。log(Koalaswinthetrophyt);}elseif(scoreKoalasscoreDolphinsscoreDolphins100scoreKoalas100){console。log(Bothwinthetrophy!);}else{console。log(Noonewinsthetrophy)} 控制台输出:96。6666666666666796。66666666666667Noonewinsthetrophy 构造两个队伍平均分相同,并且都小于100的条件,得到else最后一条输出。 修改以上分数值,则可以测试不同数据对应到符合条件的输出内容。