Javascript 卡住了。新的JS。几个小时以来一直在想办法解决这个问题
我解不出练习三和练习四。我很高兴能得到一些帮助。提前谢谢你Javascript 卡住了。新的JS。几个小时以来一直在想办法解决这个问题,javascript,variable-assignment,string-length,global-object,Javascript,Variable Assignment,String Length,Global Object,我解不出练习三和练习四。我很高兴能得到一些帮助。提前谢谢你 function exerciseThree(str){ // In this exercise, you will be given a variable, it will be called: str // On the next line create a variable called 'length' and using the length property assign the new variable to th
function exerciseThree(str){
// In this exercise, you will be given a variable, it will be called: str
// On the next line create a variable called 'length' and using the length property assign the new variable to the length of str
// Please write your answer in the line above.
return length;
}
function exerciseFour(num1){
// In this exercise, you will be given a variable, it will be called: num1
// On the next line create a variable called 'rounded'. Call the Math global object's round method, passing it num1, and assign it to the rounded variable.
var num1 = rounded;
math.round (num1);
// Please write your answer in the line above.
return rounded;
}
这些练习的措辞非常混乱。你从哪里弄来的 不管怎样,以下是答案,希望能有所帮助:
function exerciseThree(str){
// In this exercise, you will be given a variable, it will be called: str
// On the next line create a variable called 'length' and using the length property assign the new variable to the length of str
var length = str.length
// Please write your answer in the line above.
return length;
}
function exerciseFour(num1){
// In this exercise, you will be given a variable, it will be called: num1
// On the next line create a variable called 'rounded'. Call the Math global object's round method, passing it num1, and assign it to the rounded variable.
var rounded = Math.round(num1)
// Please write your answer in the line above.
return rounded;
}
这些练习试图教您如何声明变量以及如何为变量赋值 变量就像为您保存值的小容器。例如,我可以做一个小容器来装你的名字。由于在JavaScript中声明变量的方法之一是使用
var
关键字,因此我可以编写如下内容:
var name = "Sevr";
我用var
关键字制作了一个容器,并将其命名为name
。这个名称
容器现在保存您的名称,即Sevr
。您现在可以反复键入Name
,而不是反复键入Sevr
。但是,这没有多大区别Sevr
和name
都包含相同数量的字符。让变量包含您不想反复键入的信息更有意义
因此,练习三希望您声明一个名为length的变量,并使其包含所提供的任何字符串的长度
function exerciseThree(str) {
var length = str.length
return length;
}
上面的函数接受一个字符串,您可以创建一个名为length
的变量,该变量包含该字符串的长度
现在,如果我们传递任何字符串,它将告诉我们它们的长度。如果我们将您的姓名Sevr
和name
传递给它,我们将看到它们都返回4:
exerciseThree("name") // 4
exerciseThree("Sevr") // 4
在第四个练习中,概念是相同的。这个练习想告诉你,你可以使用一个简单的变量名来保存一些复杂的值。这一次,它希望您声明名为rounded的变量,该变量保持数字的舍入值
function exerciseFour(num1) {
var rounded = Math.round(num1)
return rounded;
}
现在,如果你把一个带小数的数字传递给这个函数,它会为你四舍五入
exerciseFour(4.5) // 5
似乎已经有很好的说明了。你被困在哪里?你试过什么?网上有很多javascript教程,如果这些练习太难的话,你应该通过它们来学习。