Reputation: 123
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>
Upvotes: -1
Views: 55
Reputation: 3200
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);
}
Upvotes: 0
Reputation: 215
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;
}
Upvotes: 0
Reputation: 285
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");
});
Upvotes: 0
Reputation: 7370
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;
}
Upvotes: 0
Reputation: 160943
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.
Upvotes: 2