-1

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>
nobody
20.3k17 gold badges59 silver badges80 bronze badges
asked Apr 25, 2014 at 3:35
3
  • You need to be declaring variables with var Commented Apr 25, 2014 at 3:38
  • @Geo Doesn't need to, though; there are parameters that are promptly read in via a prompt. It's weird, but there it is. Commented Apr 25, 2014 at 3:40
  • @DaveNewton True. I didn't read the whole thing before replying. I posted an answer to the question that is actually working though :) Commented Apr 25, 2014 at 3:50

5 Answers 5

2

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.

answered Apr 25, 2014 at 3:38
Sign up to request clarification or add additional context in comments.

Comments

0

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;
}
answered Apr 25, 2014 at 3:38

Comments

0

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");
});
answered Apr 25, 2014 at 3:41

1 Comment

Sir, this is jQuery, and the thing he is using is plain javascript.
0

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;
}
answered Apr 25, 2014 at 3:42

Comments

0

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);
 }
answered Apr 25, 2014 at 3:49

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.