<html>
<head>
<script language="javaScript">
var minutes=prompt("Enter number of minutes");
var answer=prompt("Enter h for hours or s for seconds");
function convertToHours(minutes) {
var hours=minutes/60;
window.alert("There are " + hours + " hours in " + minutes + " minutes");
}
function convertToSeconds(minutes) {
var seconds=minutes*60;
window.alert("There are " + seconds + " seconds in " +minutes + " minutes");
}
if (answer === 'h') {
convertToHours(minutes);
}else if(answer === 's'){
convertToSeconds(minutes);
}
</script>
</head>
<body>
</body>
</html>
Please refer javascript section for update. I am calling functions based on answer and passing minutes value while calling the function.
Solution 2 :
var minutes=prompt("Enter number of minutes");
var answer = prompt("Enter h or s");
function convertToHours(minutes) {
hours=minutes/60;
window.alert("There are " + parseInt(hours) + " hours in " + minutes + " minutes");
}
function convertToSeconds(minutes) {
seconds=minutes*60;
window.alert("There are " + seconds + " seconds in " +minutes + " minutes");
}
// just to check enter value is not a string
if(isNaN(minutes/2)){
window.alert("Enter number only in minutes");
} else {
switch(answer){
case "h":
convertToHours(minutes);
break;
case "s":
convertToSeconds(minutes);
break;
default:
window.alert("Enter valid choice either h or s only");
break;
}
}
You just have to call both methods with the value of prompt. everything else in the code looks fine.
Just one suggestion always add js at end of the body or want to add it into the head then defer it.
Solution 3 :
I think you don’t call to your function:
if (minutes!= null) {
// Do something or call your function in here
}
if (answer!= null) {
// Do something or call your function in here
}
Solution 4 :
function convertToHours(minutes) {
var minutes = prompt("Enter number of minutes");
if (minutes != null && minutes != '') {
// To get the number of hours from given minutes, divide the number of total minutes by 60 (minutes/60):
var hours = minutes / 60;
window.alert("There are " + hours + " hours in " + minutes + " minutes");
}
}
function convertToSeconds(minutes) {
var minutes = prompt("Enter number of minutes");
// To get the number of seconds in given minutes, divide the number of multiply minutes by 60 (minutes * 60):
if (minutes != null && minutes != '') {
var seconds = minutes * 60;
window.alert("There are " + seconds + " seconds in " +minutes + " minutes");
}
}
Call your functions
convertToHours();
convertToMinutes();
Solution 5 :
You just needed to call the right method depending on whether the use selects hours or seconds
<html>
<head>
<script language="javaScript">
var minutes = prompt("Enter number of minutes");
var answer = prompt("Enter h for hours or s for seconds");
if(answer == "h")
convertToHours(minutes);
else if(answer == "s")
convertToSeconds(minutes);
function convertToHours(minutes) {
hours=minutes/60;
window.alert("There are " + hours + " hours in " + minutes + " minutes");
}
function convertToSeconds(minutes) {
seconds=minutes*60;
window.alert("There are " + seconds + " seconds in " +minutes + " minutes");
}
</script>
</head>
<body>
</body>
</html>
Solution 6 :
Add this snippet below your code
switch (answer) {
case 'h':
convertToHours();
break;
case 's':
convertToSeconds();
break;
default:
break;
}
Also, remove the parameters minutes from the function definition of the functions convertToHours and convertToSeconds as it’s already a global variable.
The complete solution
getResult = () => {
let minutes = prompt("Enter number of minutes");
if (!minutes || isNaN(minutes) || +minutes <= 0) {
alert("Please enter a value greater than zero for minutes");
return;
}
// can omit as / and * will automatically coerce into a number
minutes = Number(minutes);
let answer = prompt("Enter h for hours or s for seconds");
if (!answer || !["h", "s"].includes(answer.trim())) {
alert("Please enter either h or s");
return;
}
answer = answer.trim();
const convertToHours = () => {
hours = minutes / 60;
alert("There are " + hours + " hours in " + minutes + " minutes");
}
const convertToSeconds = () => {
seconds = minutes * 60;
alert("There are " + seconds + " seconds in " + minutes + " minutes");
}
switch (answer) {
case 'h':
convertToHours();
break;
case 's':
convertToSeconds();
break;
default:
break;
}
}
document.querySelector("#result").addEventListener("click", getResult);
<button id="result">Get Results</button>
P.S: Both the prompts need validations/null-check ideally
Problem :
It’s supposed to open in a pop-up text box but I want the user to be able to choose h or s and then there’s another pop-up that gives the answer.
But nothing is working. This is what I have so far:
<html>
<head>
<script language="javaScript">
var minutes=prompt("Enter number of minutes");
var answer=prompt("Enter h for hours or s for seconds");
function convertToHours(minutes) {
hours=minutes/60;
window.alert("There are " + hours + " hours in " + minutes + " minutes");
}
function convertToSeconds(minutes) {
seconds=minutes*60;
window.alert("There are " + seconds + " seconds in " +minutes + " minutes");
}
</script>
</head>
<body>
</body>
</html>
Comments
Comment posted by Chintan Trivedi
code looks fine, but you did not call a function inside the script tag. if you call and pass minutes fetched from the prompt then it will work. you do not need answer prompt.
Comment posted by mirarara
How do I call a function and pass minutes fetched from the prompt? Sorry, I’m still really confused.
Comment posted by AussieDev81
Where does the user select hours or seconds?… And this is going to run both methods every time
Comment posted by AussieDev81
You’re not passing the minutes as a parameter to either of the methods
Comment posted by naveen
minutes is a global variable. no need to pass.
Comment posted by AussieDev81
Then wouldn’t the method signature need to change to require no parameter? If I pass no args I get
Comment posted by naveen
I don’t know how you used it. updated answer.
Comment posted by jsfiddle.net/dag4s9kh/1
I used the code exactly as it was given, just called the corresponding method dependant on if the user entered ‘h’ for hours or ‘s’ for seconds. See