Javascript 学习JS。。。似乎被困在这个练习中。。。不确定我是什么';我做错了
这是一个在线课程的练习。。。不知道我做错了什么。我应该做的事情的措辞不是很详细Javascript 学习JS。。。似乎被困在这个练习中。。。不确定我是什么';我做错了,javascript,Javascript,这是一个在线课程的练习。。。不知道我做错了什么。我应该做的事情的措辞不是很详细 function exerciseFour(value){ let greaterThanFive = false; // In this exercise, you will be given a variable, it will be called: value // You will also be given a variable named: greaterThanFive // Usin
function exerciseFour(value){
let greaterThanFive = false;
// In this exercise, you will be given a variable, it will be called: value
// You will also be given a variable named: greaterThanFive
// Using an 'if' statement check to see if the value is greater than 5. If it is, re-assign greaterThanFive the boolean true.
if (value > 5) {
let greaterThanFive = true;
}
// Please write your answer in the line above.
return greaterThanFive;
}
function exerciseFive(name){
let isSondra = false;
// In this exercise, you will be given a variable, it will be called: name
// You will also be given a variable named: isSondra
// Using an 'if' statement check to see if the name is equal to the string 'Sondra'. If it is, re-assign isSondra the boolean true.
if (name === 'Sondra') {
let isSondra = true;
}
// Please write your answer in the line above.
return isSondra;
}
不能再次声明变量 删除第二个
let
将解决您的问题
function exerciseFour(value){
let greaterThanFive = false;
if (value > 5) {
greaterThanFive = true; // <--- Remove `let` here.
}
return greaterThanFive;
}
function exerciseFive(name){
let isSondra = false;
if (name === 'Sondra') {
isSondra = true;
}
return isSondra;
}
console.log(exerciseFour(3));
console.log(exerciseFour(7));
console.log(exerciseFive("Matt"));
console.log(exerciseFive("Sondra"));
函数练习四(值){
让大于五=假;
如果(值>5){
greaterThanFive=true;//关键字let
在词法范围内定义了一个新变量。此行的词法范围:
function exerciseFour(value){
let greaterThanFive = false; // <----
// ...
}
因此,一旦我们离开if语句块的作用域,greaterThanFive
引用函数作用域变量,这就是为什么它仍然具有值false
只需删除第二个let
将使该语句成为赋值语句,而不是变量声明:
let greaterThanFive=true;
您正在声明一个同名的变量,将外部变量隐藏起来。删除let
。您可以根据需要多次设置let
变量的值,但只能声明一次。greaterThanFive=true;
而不是let greaterThanFive=true;
。。。您只想更改已声明变量的值,而不是重新声明它。“您不能第二次声明变量”-您可以,let
是块范围的。它只会对外部变量进行阴影处理,而不会执行任何预期的操作。详细说明:let x=0;const f=p=>x=p;if(true){let x=5;f(x);x=0;console.log(x);}console.log(x);
对不起,我不知道我的错
function exerciseFour(value){
let greaterThanFive = false; // <-- scoped to function
if (value > 5) {
let greaterThanFive = true; // <-- define new variable, scoped to if statement
}
return greaterThanFive;
}
function exerciseFour(value){
let greaterThanFive = false;
if (value > 5) {
greaterThanFive = true; // Assigns a new value to the variable
}
return greaterThanFive;
}