Ok, so I am trying to call the convert function in the calculate_drug function but the final "sotp" "start" alert keeps coming up as undefined. I am assuming this is because it thinks convert is not defined. This is a python code I'm trying to convert to JS and I'm obviously not very familiar with JS.
What am I doing wrong here?
<!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>
5 Answers 5
You missed the return, so the result is always undefined.
And note variables without var declaration are treated as global variables, which is highly not recommended.
Comments
You need to return something from the convert function.
function convert(time)
{
minutes = time.substr(-2)
hours = Math.floor(time/100)
con_time = (hours * 60) + parseInt(minutes)
return con_time;
}
Comments
I'm not js expert but almost all my java scripts look like this:
<script>
$(document).ready(function () {
console.log("js kicked");
$("#req").addClass("active open");
$("#req-history").addClass("active");
});
1 Comment
You forgot to return a value
function convert(time)
{
minutes = time.substr(-2)
hours = Math.floor(time/100)
con_time = (hours * 60) + parseInt(minutes)
return con_time;
}
Comments
Try this :
function convert(time)
{
var minutes = time.substr(-2);
var hours = Math.floor(minutes/100);
var con_time = (hours * 60) + parseInt(minutes);
return con_time;
}
function calculate_drug()
{
var startTime = prompt("Enter Start Time");
var stopTime = prompt("Enter Stop Time");
startTime = convert(startTime);
alert(startTime);
stopTime = convert(stopTime);
alert(stopTime);
}
varprompt. It's weird, but there it is.