我不知道如何编写这个程序。我想让程序询问用户速度和时间,并使用循环显示总计
I cant figure out quite how to write this program. I want to make the program ask the user for speed and time and use a loop to display total
function showAndCalculateDistance(speed, time) {
for (var i=0; i<5; i++){
var mph = vehicleSpeed* timeTraveled;
return mph
}
var vehicleSpeed = parseInt(prompt('How fast is the vehicle traveling (in miles per hour)? '));
var timeTraveled = parseFloat(prompt('How long has this vehicle traveled (in hours)? '));
var totalDistanceTraveled = showAndCalculateDistance(vehicleSpeed,timeTraveled);
alert('In ' + timeTraveled + ' hours, a vehicle traveling at ' + vehicleSpeed + ' miles/hour will have traveled ' + totalDistanceTraveled + ' miles.');
我想编写程序,以便它使用循环调用函数来显示车辆在该时间段内每小时行驶的英里数,并返回行驶的总英里数
您缺少用于终止 for 循环的右括号。将来,您应该缩进代码,以便更容易捕获此类错误。尽管我不确定 for 循环在您的代码中的确切用途是什么,因为您只是将相同的值分配给 mph
5 次。
function showAndCalculateDistance(speed, time) {
for (var i=0; i<5; i++){
var mph = vehicleSpeed* timeTraveled;
}
return mph
} // <-- you did not put this.
var vehicleSpeed = parseInt(prompt('How fast is the vehicle traveling (in miles per hour)? '));
var timeTraveled = parseFloat(prompt('How long has this vehicle traveled (in hours)? '));
var totalDistanceTraveled = showAndCalculateDistance(vehicleSpeed,timeTraveled);
alert('In ' + timeTraveled + ' hours, a vehicle traveling at ' + vehicleSpeed + ' miles/hour will have traveled ' + totalDistanceTraveled + ' miles.');
您缺少结束括号。要么是为了关闭for循环,要么是为了函数。
一定要检查你的括号。当您看到如下错误时,您可以判断出问题所在: Uncaught SyntaxError: Unexpected end of input
此外,请确保函数中的参数名称与您在函数内部键入的名称相匹配。目前这无关紧要,因为您将 vehicleSpeed 和 timeTraveled 声明为全局变量,但如果您打算扩展它,请确保修复它,以免引起任何意外问题。
function showAndCalculateDistance(speed, time) {
for (var i=0; i<5; i++){
//you typed: var mph = vehicleSpeed* timeTraveled
var mph = speed*time;
}
return mph
} // <-- close bracket here
var vehicleSpeed = parseInt(prompt('How fast is the vehicle traveling (in miles per hour)? '));
var timeTraveled = parseFloat(prompt('How long has this vehicle traveled (in hours)? '));
var totalDistanceTraveled = showAndCalculateDistance(vehicleSpeed,timeTraveled);
alert('In ' + timeTraveled + ' hours, a vehicle traveling at ' + vehicleSpeed + ' miles/hour will have traveled ' + totalDistanceTraveled + ' miles.');
function showAndCalculateDistance(speed, time) {
for (var i=0; i<5; i++){
var mph = vehicleSpeed* timeTraveled;
return mph
}
var vehicleSpeed = parseInt(prompt('How fast is the vehicle traveling (in miles per hour)? '));
var timeTraveled = parseFloat(prompt('How long has this vehicle traveled (in hours)? '));
var totalDistanceTraveled = showAndCalculateDistance(vehicleSpeed,timeTraveled);
alert('In ' + timeTraveled + ' hours, a vehicle traveling at ' + vehicleSpeed + ' miles/hour will have traveled ' + totalDistanceTraveled + ' miles.');
我想编写程序,以便它使用循环调用函数来显示车辆在该时间段内每小时行驶的英里数,并返回行驶的总英里数
您缺少用于终止 for 循环的右括号。将来,您应该缩进代码,以便更容易捕获此类错误。尽管我不确定 for 循环在您的代码中的确切用途是什么,因为您只是将相同的值分配给 mph
5 次。
function showAndCalculateDistance(speed, time) {
for (var i=0; i<5; i++){
var mph = vehicleSpeed* timeTraveled;
}
return mph
} // <-- you did not put this.
var vehicleSpeed = parseInt(prompt('How fast is the vehicle traveling (in miles per hour)? '));
var timeTraveled = parseFloat(prompt('How long has this vehicle traveled (in hours)? '));
var totalDistanceTraveled = showAndCalculateDistance(vehicleSpeed,timeTraveled);
alert('In ' + timeTraveled + ' hours, a vehicle traveling at ' + vehicleSpeed + ' miles/hour will have traveled ' + totalDistanceTraveled + ' miles.');
您缺少结束括号。要么是为了关闭for循环,要么是为了函数。
一定要检查你的括号。当您看到如下错误时,您可以判断出问题所在: Uncaught SyntaxError: Unexpected end of input
此外,请确保函数中的参数名称与您在函数内部键入的名称相匹配。目前这无关紧要,因为您将 vehicleSpeed 和 timeTraveled 声明为全局变量,但如果您打算扩展它,请确保修复它,以免引起任何意外问题。
function showAndCalculateDistance(speed, time) {
for (var i=0; i<5; i++){
//you typed: var mph = vehicleSpeed* timeTraveled
var mph = speed*time;
}
return mph
} // <-- close bracket here
var vehicleSpeed = parseInt(prompt('How fast is the vehicle traveling (in miles per hour)? '));
var timeTraveled = parseFloat(prompt('How long has this vehicle traveled (in hours)? '));
var totalDistanceTraveled = showAndCalculateDistance(vehicleSpeed,timeTraveled);
alert('In ' + timeTraveled + ' hours, a vehicle traveling at ' + vehicleSpeed + ' miles/hour will have traveled ' + totalDistanceTraveled + ' miles.');