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

  • Home
  • SEARCH
  • 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 7016333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:46:15+00:00 2026-05-27T22:46:15+00:00

I am very new Javascript and am messing around trying to make a simple

  • 0

I am very new Javascript and am messing around trying to make a simple game. Basically, you can attack or heal and your HP goes up or down accordingly. Here is what I have so far:

<script type="text/javascript">
var myHP=50;
var eHP=50;
</script>

<script type="text/javascript">
function attack()
{
    var dmg=(Math.floor(Math.random()*11));
    var edmg=(Math.floor(Math.random()*11));
    var nHP=myHP - edmg;

    alert("You deal " + dmg + " damage!");
    alert("You are dealt " + edmg + " damage!");
    document.getElementById("hp").innerHTML=nHP;
}
</script>

So, I make a paragraph with an id of “hp” and I can see it change when I click the attack button. However, it always does 50-edmg. I.e. I get attacked for 3, my HP is 47. Next, I get attacked for 2, my HP is 48 and not 45 because it is always subtracting from the base. How can I make it so that the myHP variable is updated live and therefore always reflects the correct number?

  • 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-05-27T22:46:15+00:00Added an answer on May 27, 2026 at 10:46 pm

    You are never updating the myHP variable. The fix could be as simple as adding:

    myHP = nHP;
    

    as the last line of your last script.

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

Sidebar

Related Questions

I'm very new to javascript (started messing with it today). I'm trying to change
I am very new in Javascript. I want to make some basic stuffs that
I'm very new to the AJAX and Javascript world and I'm trying to implement
(very new to JavaScript jQuery) Can an if statement contain a collection of other
I'm very new with javascript. I'm trying to create a tag using document.write (with
I am messing around in Node.js trying to replicate that simple chat server that
Hello I a very new to Javascript and I need to make a code
I am very new to both JavaScript and ActionScript. I am trying to get
I'm really very new to javascript, and trying to validate a page to xhtml
I am very new to javascript I know that you can add an onclick=

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.