Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8054043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:03:12+00:00 2026-06-05T08:03:12+00:00

I’m trying to make a very simple calculator in HTML and javascript But it

  • 0

I’m trying to make a very simple calculator in HTML and javascript
But it still does not work

My question are:

1. How do I get value from a form and set it as a number, not a string?
2. How do I perform quadratic calculation in javascript?

3. How do I update html content with javascript? So whenever I change the value of the input form and press submit, the result will change based on the input that I type, I’ve tried the document.getElementById(“idhere”).innerHTML=valuehere; but still does not work.

<script tyle="text/javascript">
function calculateThis(form) {
var userweight=form.weight.value;
var caffeineamount=form.caffein.value;
var caffeinetimes=form.caffeintimes.value;
var totalcaffeine=caffeineamount*caffeinetimes;

// Calculate max caffeine per person
var maxcaffeine=userweight*10;

// Calculate remaining after 24 hours
// Half life = 6 hours
var totalcaffeineafter=totalcaffeine(1/16);

// Calculating how many hours until the caffeine completely digested
var totaldigest=totalcaffeine;
var digesttime=0;

while (totaldigest>0.05) {
totaldigest=totaldigest(1/2);
digesttime++;
}

digesttime=digesttime*6;

// Calculating when the user will probably die of overdose
var countcaffeine=0;
var overdosetime=1;

while (countcaffeine<maxcaffeine){
countcaffeine=countcaffeine+totalcaffeine;
overdosetime++;
}

// Show total amount of caffeine
document.getElementById("showtotalkafein").innerHTML=totalcaffeine;

// Show amount of caffeine after 1 day
document.getElementById("showtotalkafeinsetelah").innerHTML=totalcaffeineafter;

// Show digest time
document.getElementById("showwaktudigest").innerHTML=digesttime;

// Show overdose
document.getElementById("showberapakali").innerHTML=overdosetime;

return false;
}
</script>

<form class="form">
Weight<br />  
<input type="text" name="weight" class="required" value="" /><p /> 
Amount of caffein in coffee<br />
<input type="text" name="caffein" class="required" value="" /><p /> 
How many times drinking coffee in a day<br /> 
<input type="text" name="caffeintimes" class="required" value="" /><p />

<button type="button" onclick="calculateThis(this.form); return false;">Calculate</button></form>

<h1>Result</h1>
<p id="showtotalkafein">Show Caffein Total Here</p> 
<p id="showtotalkafeinsetelah">Show Caffeine Amount After 24 hours</p> 
<p id="showwaktudigest">Show Digest TIme Here</p> 
<p id="showberapakali">Show Overdose Time Here</p>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:03:16+00:00Added an answer on June 5, 2026 at 8:03 am
    function calculateThis(form) {
        var userweight = parseInt(form.weight.value, 10);
        var caffeineamount = parseInt(form.caffein.value, 10);
        var caffeinetimes = parseInt(form.caffeintimes.value, 10);
        var totalcaffeine = caffeineamount * caffeinetimes;
    
        console.log(totalcaffeine)
        // Calculate max caffeine per person
        var maxcaffeine = userweight * 10;
    
        // Calculate remaining after 24 hours
        // Half life = 6 hours
        var totalcaffeineafter = totalcaffeine * (1 / 16);
    
        // Calculating how many hours until the caffeine completely digested
        var totaldigest = totalcaffeine;
        var digesttime = 0;
    
        while (totaldigest > 0.05) {
            totaldigest = totaldigest * (1 / 2);
            digesttime++;
        }
    
        digesttime = digesttime * 6;
    
        // Calculating when the user will probably die of overdose
        var countcaffeine = 0;
        var overdosetime = 1;
    
        while (countcaffeine < maxcaffeine) {
            countcaffeine = countcaffeine + totalcaffeine;
            overdosetime++;
        }
    
        // Show total amount of caffeine
        document.getElementById("showtotalkafein").innerHTML = totalcaffeine;
    
        // Show amount of caffeine after 1 day
        document.getElementById("showtotalkafeinsetelah").innerHTML = totalcaffeineafter;
    
        // Show digest time
        document.getElementById("showwaktudigest").innerHTML = digesttime;
    
        // Show overdose
        document.getElementById("showberapakali").innerHTML = overdosetime;
    
        return false;
    }
    

    DEMO

    Note

    Here I used .parseInt() which used to convert a number to integer, if you need any value in Float then use .parseFloat()`.


    Problems in your code

    • form.weight.value, form.caffein.value … give value as string, so you need to convert them to number (integer/ float).

    • you used totalcaffeine(1 / 16) and totaldigest = totaldigest(1 / 2);, not a valid Math operation in javascript, it should be totalcaffeine * (1 / 16) and totaldigest = totaldigest * (1 / 2);


    According to your comment

    What .parseInt(form.caffeintimes.value, 10), do?

    Format of parseInt is:

    .parseInt(valueToConvert, [radix]).

    So, .parseInt(form.caffeintimes.value, 10) will convert form.caffeintimes.value string to a 10-base integer.


    Related refs:

    .parseInt()

    .parseFloat()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.