此代码使用现有数组将该数据填充为空数据......
然后它创建一个循环,检查它们是否低于18或大于等于18。
var years = [1990, 2001, 1975, 2004, 1998, 1993];
var empty = [];
for (i = 0; i <= years.length - 1; i++) {
empty.push(years[i]);
}
console.log(empty);
for (a = 0; a <= empty.length - 1; a++) {
if (2018 - empty[a] < 18) {
console.log(empty[a] + ' is not eighteen or older: ' + 2018 - empty[a]);
} else {
console.log(empty[a] + ' is eighteen or older: ' + 2018 - empty[a]);
}
}
&#13;
答案 0 :(得分:0)
它与字符串连接有关。由于2018
是一个预期在这些控制台日志中进行数字加法的数字。我使用模板文字修改了你的代码,它工作正常。如果您不想使用模板文字,只需在最后2018 - empty[a]
的计算周围添加括号变为(2018 - empty[a])
。
var years = [1990, 2001, 1975, 2004, 1998, 1993];
var empty = [];
for (let i = 0; i < years.length; i += 1) {
empty.push(years[i]);
}
console.log(empty);
// using template literals
for (let a = 0; a < empty.length; a += 1) {
if (2018 - +empty[a] < 18) {
console.log(`${empty[a]} is not eighteen or older: ${2018 - empty[a]}`);
} else {
console.log(`${empty[a]} is eighteen or older: ${2018 - empty[a]}`);
}
}
// regular string concatenation
for (let a = 0; a < empty.length; a += 1) {
if (2018 - +empty[a] < 18) {
console.log(empty[a] + ' is not eighteen or older:' + (2018 - empty[a]));
} else {
console.log(empty[a] + ' is eighteen or older:' + (2018 - empty[a]));
}
}
&#13;
答案 1 :(得分:0)
您看到的错误与运算符,类型强制和评估顺序有关。
您的console.log
等同于下面显示的第一个+
。
你从一个数字开始,然后添加+ 2018
一个字符串,所以整个事情被强制转换成一个字符串
这将继续,所有内容都包含-
字符串,直到您点击减号+ 2018 - empty[a]
运算符为止
"2003 is not eighteen or older: 2018"
此时,减号不对字符串进行操作,并且到那里的值有一个字符串,例如:
NaN
所以尝试从字符串中减去一个数字会得到非数字"2003 is not eighteen or older: 2018" - empty[a]
console.log ( 5 + ' is five, subtract ' + 2 + ' is ' + 5-2 );
NaN
console.log ( 5 + ' is five, subtract ' + 2 + ' is ' + (5-2) );
5 is five, subtract 2 is 3
如果您对数字表达式进行括号,以便在与字符串连接之前对进行求值,则它应该有效:
console.log( empty[a] + ' is not eighteen or older: ' + (2018 - empty[a]) );
e.g。 var years = [1990, 2001, 1975, 2004, 1998, 1993];
var empty = [];
for (i = 0; i <= years.length - 1; i++) {
empty.push(years[i]);
}
console.log(empty);
for (a = 0; a <= empty.length - 1; a++) {
if (2018 - empty[a] < 18) {
console.log( empty[a] + ' is not eighteen or older: ' + (2018 - empty[a]) );
} else {
console.log( empty[a] + ' is eighteen or older: ' + (2018 - empty[a]) );
}
}
2018 - empty[a]
注意 - 如果您计算一次年龄,然后在console.log输出中使用它,这也不会有问题。实际上,您多次计算for (a = 0; a <= empty.length - 1; a++) {
let age = 2018 - empty[a];
if (age < 18) {
console.log(empty[a] + ' is not eighteen or older: ' + age);
...
。
var years = [1990, 2001, 1975, 2004, 1998, 1993];
var empty = [];
for (i = 0; i <= years.length - 1; i++) {
empty.push(years[i]);
}
console.log(empty);
for (a = 0; a <= empty.length - 1; a++) {
let age = 2018 - empty[a];
if (age < 18) {
console.log( empty[a] + ' is not eighteen or older: ' + age );
} else {
console.log( empty[a] + ' is eighteen or older: ' + age );
}
}
getDoc() {
return this.getError()
.retryWhen((errors: any) => {
return errors.switchMap((sourceError) => {
console.log(sourceError);
if (sourceError.status === 103) {
console.log('status is 103');
const notFoundError = new HttpErrorResponse({
status: 404
});
return errors.delay(5000).take(5).concat(Observable.throw(notFoundError));
}
return Observable.throw(sourceError);
});
})
.toPromise();
}
getError() {
console.log('getting error');
return Observable.throw(new HttpErrorResponse({
status: 103
}));
}