Help with a function


Joined
Jan 30, 2020
Messages
18
Reaction score
0
I stuck trying to figure out how to convert hours and min to seconds inside a fucntion ill post my directions and if someone can help give me a better understanding of the formula id really appreciate it.



A function that takes hours, minutes, seconds as parameters, and returns the total number of seconds (convert each to seconds, then add them up)


Code:
<!DOCTYPE html>
<html>
<body>
<script>
// javascript code begin
function hms2s( h, m, s) {
   
    var total = h + m + s ;
     return total ;

}

document.write(hms2s(2,3,4) );

/*A function that takes hours, minutes, seconds as parameters, and returns the total number of seconds (convert each to seconds, then add them up)*/
// javascript code end
</script>
</body>
</html>


so far the above code is what ive been able to piece together the numbers at the bottom being used to call the function are just to test if the code works. thank you in advance.
 
Ad

Advertisements

Joined
Nov 27, 2019
Messages
163
Reaction score
24
You convert minutes to seconds by multiplying the minutes by 60.
You can change the hours two way:
1.) Multiplying the hours by 60 to obtain the number of minutes and then converting that OR
2.) Simply multiplying hours by 3600 to obtain the number of seconds straight away.

Code:
function hms2s( h, m, s) {
       var total = (3600 * h) + (60 * m) + s ;
     return total ;
}

A big tip FYI
Don't use document.write() it over writes anything already on the browser.
Guess it's OK(?) it your stage of learning, BUT it's a bad habit to get into.
Make a DIV, with an ID, in the HTML body and then write to that using
document.getElementById().innerHTML or
document.getElementById().innerText

Code:
<!DOCTYPE html>
<html>
<head>
  <title>Your Title</title>
</head>
<body>
<div id="wrapper"></div>

<script>
function hms2s( h, m, s) {
  var total = (3600 * h) + (60 * m) + s ;
  return total ;
}
document.getElementById("wrapper").innerHTML = hms2s( 5, 38, 10);
</script>
</body>
</html>
 
Joined
Jan 30, 2020
Messages
18
Reaction score
0
thank you again for your help dude, are you the only person maintaining on here? either way i appreciate your help alot i wish i had more knowledge of this to help you... last question for this post am i able to use multiple divs on here? so if i made another function i could use a seperate div for that function ?
 
Joined
Nov 27, 2019
Messages
163
Reaction score
24
Yes of course you can. You could also use the same DIV, all depends on how you want to display the two outputs.
If you have an idea what the final browser screen will look like for the outputs we can put the JS together.
 
Joined
Nov 23, 2019
Messages
67
Reaction score
1
mmm,I think ..if this user is requesting that example for a real webpage application,then using.innerHTML might be not suitable for her/him at this stage.
 
Joined
Jan 30, 2020
Messages
18
Reaction score
0
all so far what we have been doing is creating code in the w3schools sandbox occasionally ill use a text editor like sublime or atom then preview the code in firefow since i cant for the life of me figure out how to use visual studio to easily preview code . since we are learning our teacher doesnt really care about little things you shouldnt do in the code just as long as it works and we learn the subject being showed at hand. but im always trying to improve and i have been trying to take all the advice youve been giving to me . but i have another question , we are starting to learn about loops and im trying to use the <div> you showed me to get the output of this loop, i can only get it to work using document.write am i doing something wrong ?
Code:
<!DOCTYPE html>
<html>
<body>
<div id="count"></div>
<script>
// javascript code begin

var counterVar = 0

do {
    document.getElementById("count").innerHTML= counterVar + " " ;
    counterVar += 1 ;
    }
    while ( counterVar <= 15 )

// javascript code end
</script>
</body>
</html>
 
Ad

Advertisements

Joined
Nov 27, 2019
Messages
163
Reaction score
24
Always end a statement with a semicolon. Sloppy habit to not include them.
Moved to innerHTML to outside the loop so it prints the final value:
Code:
<!DOCTYPE html>
<html>
<body>
<div id="count"></div>
<script>
// javascript code begin
var counterVar = 0;
do {
    counterVar += 1;
}
while ( counterVar <= 15 );
    document.getElementById("count").innerHTML= counterVar;
// javascript code end
</script>
</body>
</html>
 
Joined
Jan 30, 2020
Messages
18
Reaction score
0
Im gonna try playing around with it to figure it out hopefully but I need it to display the output as the whole count from 0 - 15. is there no way to do that?
 
Ad

Advertisements

Joined
Nov 27, 2019
Messages
163
Reaction score
24
Oh :D My bad.
Code:
<!DOCTYPE html>
<html>
<body>
<div id="count"></div>
<script>
// javascript code begin
var text = ""
var counterVar = 0;
do {
    text += counterVar + "<br>";
    counterVar++;
}while (counterVar <= 15);
document.getElementById("count").innerHTML= text;
// javascript code end
</script>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top