Javascript02基础部分1。4
017StringsandTemplateLiterals
本讲主要讲字符串和模板字面量。
视频加载中。。。
字符串通过号可以拼接成句子。constfirstNameJonas;constjobteacher;constbirthYear1991;constyear2037;constjonasImfirstName,a(yearbirthYear)yearsoldjob!;console。log(jonas);
在控制台输出结果为:ImJonas,a46yearsoldteacher!
由于字符串拼接过程中,要管理空格非常不方便,可以使用ES6新增功能模板字面量。constfirstNameJonas;constjobteacher;constbirthYear1991;constyear2037;constjonasNewIm{firstName},a{yearbirthYear}yearold{job}!;console。log(jonasNew);
在控制台输出结果为:ImJonas,a46yearoldteacher!
模板字面量需要使用反引号,美元符号,大括号{}。
反引号内为整个字符串拼装内容。
美元符号大括号{}内,可以是定义的变量名称,可以是操作符计算。
使用模板字面量的格式拼装字符串,比使用号连接字符串,形式上要简洁方便。
console。log(Justaregularstring。。。);
对于单独一行的字符串,也可以直接使用反引号。不用考虑是使用单引号还是双引号。
在控制台输出结果为:Justaregularstring。。。
console。log(Stringwithmultiplelines);
在控制台输出结果为:Stringwithmultiplelines
对于多行字符串,需要借用换行和右斜线来控制。
console。log(Stringmultiplelines);
在控制台输出结果为:Stringwithmultiplelines
如果直接使用反引号,就可以简化这种写法。
018TakingDecisionsifelseStatements
本讲主要讲if和else语句的使用。
视频加载中。。。
实现一个程序,检查某人年龄是否大于等于18岁允许考驾照,如果年龄允许,程序打印信息到控制台;如果年龄不允许,程序打印还有几年才可以。
constage19;constisOldEnoughage18;if(isOldEnough){console。log(Sarahcandrivinglicense);}
在控制台输出结果为:Sarahcandrivinglicense
Sarah的年龄设定为19,在经过isOldEnough判断为真后,输出内容Sarah可以有驾照。
constage15;if(age18){console。log(Sarahcandrivinglicense);}else{constyearsLeft18age;console。log(Sarahistooyoung。Waitanother{yearLeft}years:));}
在控制台输出结果为:Sarahistooyoung。Waitanother3years:)
if后面的条件判断为真,执行第一个大括号内语句。if后面的条件为假,执行else后面大括号内的语句。if(){}else{}
if,else语句组合叫做控制语句。
constbirthYear1998;letcentury;if(birthYear2000){century20;}else{century21;}console。log(century);
在控制台输出结果为:20
birthYear为1998,在if后判断为真,所以century赋值为20。
constbirthYear2012;letcentury;if(birthYear2000){century20;}else{century21;}console。log(century);
在控制台输出结果为:21
birthYear为2012,在if后判断为假,不执行if后大括号语句,执行else后大括号内语句,所以century赋值为21。
019CodingChallenge2
视频加载中。。。
CodingChallenge2
UsetheBMIexamplefromChallenge1,andthecodeyoualreadywrote,andimproveit。
Yourtasks:
1。Printaniceoutputtotheconsole,sayingwhohasthehigherBMI。ThemessageiseitherMarksBMIishigherthanJohns!orJohnsBMIishigherthanMarks!
2。UseatemplateliteraltoincludetheBMIvaluesintheoutputs。Example:MarksBMI(28。3)ishigherthanJohns(23。9)!Hint:Useanifelsestatement
GOODLUCK
本次编码挑战,基于第一次BMI编码挑战的代码改进。
1、打印输出谁的BMI更高。例如:
MarksBMIishigherthanJohns!
或者
JohnsBMIishigherthanMarks!
2、使用模板字面量,在打印输出的时候,包含BMI的值。例如:
MarksBMI(28。3)ishigherthanJohns(23。9)!
constmassMark95;constheightMark1。88;constmassJohn85;constheightJohn1。76;constBMIMarkmassMarkheightMark2;constBMIJohnmassJohn(heightJohnheightJohn);console。log(BMIMark,BMIJohn);if(BMIMarkBMIJohn){console。log(MarksBMIishigherthanJohns!)}else{console。log(JohnsBMIishigherthanMarks!)}
在控制台输出:26。8786781349026727。44059917355372JohnsBMIishigherthanMarks!
constmassMark95;constheightMark1。88;constmassJohn85;constheightJohn1。76;constBMIMarkmassMarkheightMark2;constBMIJohnmassJohn(heightJohnheightJohn);console。log(BMIMark,BMIJohn);if(BMIMarkBMIJohn){console。log(MarksBMI({BMIMark})ishigherthanJohns({BMIJohn})!)}else{console。log(JohnsBMI({BMIJohn})ishigherthanMarks({BMIMark})!)}
在控制台输出:26。8786781349026727。44059917355372JohnsBMI(27。44059917355372)ishigherthanMarks(26。87867813490267)!