好的,所以我尝试在calculate_drug 函数中调用convert 函数,但最终的“sotp”“start”警报始终显示为未定义。我假设这是因为它认为 Convert 没有定义。这是我试图转换为 JS 的 python 代码,显然我对 JS 不是很熟悉。
我在这里做错了什么?
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>Click the button</p>
<button onclick="calculate_drug()">Drug</button>
<script>
function convert(time)
{
minutes = time.substr(-2)
hours = Math.floor(time/100)
con_time = (hours * 60) + parseInt(minutes)
}
function calculate_drug(start, stop)
{
start = prompt("Enter Start Time")
stop = prompt("Enter Stop Time")
start = convert(start)
alert(start)
stop = convert(stop)
alert(stop)
}
</script>
</body>
</html>
最佳答案
您错过了返回
,因此结果始终是未定义
。
请注意,没有 var
声明的变量将被视为全局变量,强烈不建议这样做。
关于javascript - 基本 JavaScript 函数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23283694/