Javascript:02基础部分1.5

020 Type Conversion and Coercion

视频加载中...

本讲主要讲类型转换和类型强制转换。

数字转换为字符串,使用String函数

字符串转换为数字,使用Number函数

类型强制转换,语言自身实现

const inputYear = '1991';
console.log(inputYear + 18);

在控制台输出:

199118

字符串与数字用加号做计算,实际是连接字符串和数字。

const inputYear = '1991';
console.log(Number(iniputYear), inputYear);
console.log(Number(inputYear) + 18);

在控制台输出:

1991 "1991"
2009

使用Number函数可以将字符串类型转变为数字类型。类型转换不会改变原有变量或者值的类型。

console.log(Number('Jonas'));

在控制台输出:

NaN

使用Number函数转换字符串为数字类型,并不是说对所有的字符串都生效。对于名字或者单词这样的纯字符串,使用Number转换后得到NaN,意思是Not a Number,不是数字。

console.log(typeof NaN);

在控制台输出:

number

对于返回的结果NaN,实际上它也是number类型,只是通过它来表示无效的数字。

console.log(String(23), 23);

在控制台输出:

23 23

使用String函数可以把数字类型转换为字符串类型。在控制台输出的两个23,前一个是字符串类型,后一个是数字类型。它们的颜色上有区别。

console.log('I am ' + 23 + ' years old');
console.log('23' - '10' - 3);

在控制台输出:

I am 23 years old
10

对于字符串和数值用加号连接,会触发系统将数值类型强制转换为字符串。但是,并不是所有的操作符会触发数值强制转换为字符串。对于减号操作符,字符串23减去字符串10减去数值3,可以得到数值类型10。

let n = '1' + 1;
n = n - 1;
console.log(n);

在控制台输出:

10

对于乘法、除法操作符,对于数字加引号的字符串也可以强制转换为数字类型。所以,只有加号有特殊用法,将数字强制转换为字符串,用于连接字符串。


021 Truthy and Falsy Values

视频加载中...

本讲主要学习涉及布尔类型的转换。

五种假值:0,' ', undefined, null, NaN

console.log(Boolean(0));
console.log(Boolean(undefined));
console.log(Boolean('Jonas'));
console.log({});

在控制台输出:

false
false
true
true

const money = 0;
if (money) {
	console.log("Don't spend it all");
} else {
	console.log('You should get a job!');
}

在控制台输出:

You should get a job!

money变量为0,当if后作为布尔值进行处理的时候,强制类型转换为布尔值,因为0是假值,所以执行else后代码块内容。

let height;
if (height) {
	console.log("YAY! Height is defined");
} else {
	console.log('Height is UNDEFINED');
}

在控制台输出:

Height is UNDEFINED

变量height定义后未赋值,所以它的类型是undefined。

在if后将类型强制转换为布尔值,因为undefined为假,所以执行else后的代码块。

对于变量height如果初始化为0,也会得到同样的结果。因为0对于布尔值来说也是假。

所以,写代码要注意这样的问题。


022 Equality Operators_ == vs. ===

视频加载中...

本讲主要学习等号运算符,“ == ”与“ === ”使用差异。

if/else/else if补充用法。

不等运算符,“!=”和“ !== ”差异。

const age = 18;
if (age === 18) console.log('You just became an adult :D');

在控制台输出:

You just became an adult :D

变量age初始化为18,通过" === " 运算符比较相等,输出预期内容。如果变量age的初始值变为19,则通过" === " 运算符比较不相等,不会输出任何内容。

" === " 运算符的结果为true或者false。三个等号运算符两边值相等,则为true。

一个等号是赋值操作,三个等号是比较运算操作。

const age = 18;
if (age === 18) console.log('You just became an adult :D (strict)');
if (age == 18) console.log('You just became an adult :D (loose)');

在控制台输出:

You just became an adult :D (strict)
You just became an adult :D (loose)

const age = '18';
if (age === 18) console.log('You just became an adult :D (strict)');
if (age == 18) console.log('You just became an adult :D (loose)');

在控制台输出:

You just became an adult :D (loose)

修改变量age值为字符串18,可以看到控制台输出只有一行记录。对于“ === ”操作符不满足布尔值为真,对于“ == ”操作符,仍然满足。因为“ == ”操作符默认类型强制转换。

const favourite = prompt("What's your favourite number?");
console.log(favourite);

在弹出的提示框中输入数字23。

在控制台输出:

23

23在这里是字符串类型。

const favourite = Number(prompt("What's your favourite number?"));
console.log(favourite);
console.log(typeof favorite);
if (favorite === 23) {
	console.log('Cool! 23 is an amazing number!');
}

在弹出的提示框中输入数字23。

在控制台输出:

23
number
Cool! 23 is an amazing number!

使用Number函数,输入23是字符串类型转换为数字类型。“ === ”操作符严格比对数字23相等为真,所以执行if后代码块内容。

如果不使用Number函数进行类型转换,“ === ”操作符严格比对字符串23与数字23不相等,if判断为假,无任何输出。

const favourite = Number(prompt("What's your favourite number?"));
console.log(favourite);
console.log(typeof favorite);
if (favorite === 23) {
	console.log('Cool! 23 is an amazing number!');
} else if (favourite === 7) {
	console.log('7 is also a cool number')
} else if (favourite === 9) {
	console.log('9 is also a cool number')
} else {
	console.log('Number is not 23 or 7 or 9')
}

在弹出的提示框中输入数字9。

在控制台输出:

9
number
9 is also a cool number

除了了if/else之外,还可以继续使用else if来增加分支判断。如果if后面判断条件为假,不是立即进入else代码块,而是进入else if后的判断条件继续检查。

const favourite = Number(prompt("What's your favourite number?"));
console.log(favourite);
console.log(typeof favorite);
if (favorite === 23) {
	console.log('Cool! 23 is an amazing number!');
} else if (favourite === 7) {
	console.log('7 is also a cool number')
} else if (favourite === 9) {
	console.log('9 is also a cool number')
} else {
	console.log('Number is not 23 or 7 or 9')
}
if (favourite !== 23) console.log('Why not 23?');

在弹出的提示框中输入数字9。

在控制台输出:

9
number
9 is also a cool number
Why not 23?

建议使用!== 是严格检查不匹配。输入变量值为9与23不等,所以输出Why not 23?


023 Boolean Logic

视频加载中...

本讲主要学习布尔逻辑。

逻辑运算符:AND(&&)、 OR(||)、 NOT(!)

A是TRUE,B是TRUE,A&&B为TRUE。

A是TRUE,B是FALSE,A&&B为FALSE。

A是FALSE,B是TRUE,A&&B为FALSE。

A是FALSE,B是FALSE,A&&B为FALSE。


A是TRUE,B是TRUE,A||B为TRUE。

A是TRUE,B是FALSE,A||B为TRUE。

A是FALSE,B是TRUE,A||B为TRUE。

A是FALSE,B是FALSE,A||B为FALSE。


A是TRUE,!A是FALSE。

A是FALSE, !A是TRUE。


024 Logical Operators

视频加载中...

本讲主要学习逻辑操作验证。

const hasDriverLicense = true; // A
const hasGoodVision = true; // B
console.log(hasDriverLicense && hasGoodVision);

在控制台输出:

true

两个变量都为true,进行与操作还是true。

const hasDriverLicense = true; // A
const hasGoodVision = false; // B
console.log(hasDriverLicense && hasGoodVision);

在控制台输出:

false

一个变量为true,一个变量为false,进行与操作是false。

const hasDriverLicense = true; // A
const hasGoodVision = false; // B
console.log(hasDriverLicense && hasGoodVision);
console.log(hasDriverLicense || hasGoodVision);

在控制台输出:

false
true

一个变量为true,一个变量为false,进行与操作是false。

const hasDriverLicense = true; // A
const hasGoodVision = false; // B
console.log(hasDriverLicense && hasGoodVision);
console.log(hasDriverLicense || hasGoodVision);
console.log(!hasDriversLicense);

在控制台输出:

false
true
false

!就是NOT操作,true的!操作结果是false。

const hasDriverLicense = true; // A
const hasGoodVision = false; // B
console.log(hasDriverLicense && hasGoodVision);
console.log(hasDriverLicense || hasGoodVision);
console.log(!hasDriversLicense);
if (hasDriversLicense && hasGoodVision) {
	console.log('Sarah is able to drive!');
} else {
	console.log('Someone else should drive...');
}

在控制台输出:

false
true
false
Someone else should drive...

因为hasGoodVision为false,所以if后的条件判断为false,执行else后代码块内容。

const hasDriverLicense = true; // A
const hasGoodVision = true; // B
console.log(hasDriverLicense && hasGoodVision);
console.log(hasDriverLicense || hasGoodVision);
console.log(!hasDriversLicense);
if (hasDriversLicense && hasGoodVision) {
	console.log('Sarah is able to drive!');
} else {
	console.log('Someone else should drive...');
}

在控制台输出:

true
true
false
Sarah is able to drive!

因为hasDriversLicense和hasGoodVision都为true,所以if后的条件判断为true,执行if后代码块内容。

const hasDriverLicense = true; // A
const hasGoodVision = true; // B
console.log(hasDriverLicense && hasGoodVision);
console.log(hasDriverLicense || hasGoodVision);
console.log(!hasDriversLicense);
if (hasDriversLicense && hasGoodVision) {
	console.log('Sarah is able to drive!');
} else {
	console.log('Someone else should drive...');
}
const isTired = false; // C
console.log('hasDriverLicense && hasGoodVision && isTired');

在控制台输出:

true
true
false
Sarah is able to drive!
false

因为isTired为false,hasDriversLicense和hasGoodVision为true,三个变量相与,只要有一个false,最终为false。

const hasDriverLicense = true; // A
const hasGoodVision = true; // B
console.log(hasDriverLicense && hasGoodVision);
console.log(hasDriverLicense || hasGoodVision);
console.log(!hasDriversLicense);
const isTired = true; // C
console.log('hasDriverLicense && hasGoodVision && isTired');
if (hasDriversLicense && hasGoodVision && !isTired) {
	console.log('Sarah is able to drive!');
} else {
	console.log('Someone else should drive...');
}

在控制台输出:

true
true
false
true
Someone else should drive...

如果isTired初始值为true,则if后判断条件为false,所以执行else后代码块。

const hasDriverLicense = true; // A
const hasGoodVision = true; // B
console.log(hasDriverLicense && hasGoodVision);
console.log(hasDriverLicense || hasGoodVision);
console.log(!hasDriversLicense);
const isTired = false; // C
console.log('hasDriverLicense && hasGoodVision && isTired');
if (hasDriversLicense && hasGoodVision && !isTired) {
	console.log('Sarah is able to drive!');
} else {
	console.log('Someone else should drive...');
}

在控制台输出:

true
true
false
true
Someone else should drive...

如果isTrue初始值为false,if后判断条件为true,则继续执行if后代码块。


025 Coding Challenge #3

视频加载中...

Coding Challenge #3

There are two gymnastics teams, Dolphins and Koalas. They compete against each other 3 times. The winner with the highest average score wins a trophy!

有两个体操队,海豚队和考拉队。他们进行三次比赛。平均分最高的队伍获胜。

Your tasks:

1. Calculate the average score for each team, using the test data below

每队使用以下测试数据进行平均分计算。

2. Compare the team's average scores to determine the winner of the competition, and print it to the console. Don't forget that there can be a draw, so test for that as well (draw means they have the same average score)

比较两队的平均分来决定获胜方,打印到控制台。不要忘了两队平均分可能相同的情况。

3. Bonus 1: Include a requirement for a minimum score of 100. With this rule, a team only wins if it has a higher score than the other team, and the same time a score of at least 100 points. Hint: Use a logical operator to test for minimum score, as well as multiple else-if blocks

奖励1:需要包含最低分100。提示,使用逻辑操作来测试最低分,也可以用else-if块结构。

4. Bonus 2: Minimum score also applies to a draw! So a draw only happens when both teams have the same score and both have a score greater or equal 100 points. Otherwise, no team wins the trophy

奖励2:分数大于或等于100的时候,也要考虑相同的情况。否则,没有队伍获胜。

Test data:

Data 1: Dolphins score 96, 108 and 89. Koalas score 88, 91 and 110

数据1:海豚队分数96,108和89。考拉队分数88,91和110。

Data Bonus 1: Dolphins score 97, 112 and 101. Koalas score 109, 95 and 123

奖励1数据:海豚队分数97,112和101。考拉队分数109,95和123。

Data Bonus 2: Dolphins score 97, 112 and 101. Koalas score 109, 95 and 106

奖励2数据:海豚队分数97,112和101。考拉队分数109,95和106。

GOOD LUCK

const scoreDolphins = (96 + 108 + 89) / 3;
const scoreKoalas = (88 + 91 + 110) / 3;
console.log(scoreDolphins, scoreKoalas);

控制台输出:

97.66666666666667 96.33333333333333

按照数据1给出的分数,分别计算海豚队和考拉队的平均分。

const scoreDolphins = (96 + 108 + 89) / 3;
const scoreKoalas = (88 + 91 + 110) / 3;
console.log(scoreDolphins, scoreKoalas);
if(scoreDolphins > scoreKoalas) {
	console.log('Dolphins win the trophyt');
} else if (scoreKoalas > scoreDolphins) {
	console.log('Koalas win the trophyt');
} else if (scoreKoalas === scoreDolphins) {
	console.log('Both win the trophy!');
}

控制台输出:

97.66666666666667 96.33333333333333
Dolphins win the trophyt

通过if-else if控制结构来分别判断三种情况,输出不同的结果。

const scoreDolphins = (97 + 112 + 81) / 3;
const scoreKoalas = (109 + 95 + 86) / 3;
console.log(scoreDolphins, scoreKoalas);
if(scoreDolphins > scoreKoalas && scoreDolphins >= 100) {
	console.log('Dolphins win the trophyt');
} else if (scoreKoalas > scoreDolphins && scoreKoalas >= 100) {
	console.log('Koalas win the trophyt');
} else if (scoreKoalas === scoreDolphins && scoreDolphins >= 100 && scoreKoalas >= 100) {
	console.log('Both win the trophy!');
} else {
	console.log('No one wins the trophy ')
}

控制台输出:

96.66666666666667 96.66666666666667
No one wins the trophy 

构造两个队伍平均分相同,并且都小于100的条件,得到else最后一条输出。

修改以上分数值,则可以测试不同数据对应到符合条件的输出内容。

展开阅读全文

页面更新:2024-05-14

标签:控制台   字符串   数值   变量   函数   分数   类型   操作   代码   数字   基础

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top