0

i am slowly developing a game in a html and java script. when i launch the go to work button it doesn't add what you earn it just changes the number to the number of hours you chose. i have been trying to figure this out on my own for about a month now

 <!DOCTYPE html>
<html>
<link rel="stylesheet" type="text/css" href="icecream shop2.css">
<head>
    <title></title>
</head>
<body>
 <marquee> Welcome to the Happy Smells Iced-Creamery</marquee>
 <button onclick="myFunction()" >Clich Here To Order An Iced-Cream</button>
 <button onclick="myFunction2()" >Click here to go to work</button>
 <p id="money">Money:</p>
 <table> 
 <tbody>
    <tr> <td> <h1> Available Iced-Cream Flavours <h1> </td></tr>
    <tr> <td> Chocolate </td> </tr>
    <tr> <td> Vanilla </td> </tr>
    <tr> <td> Caramel </td> </tr>
    <tr> <td> HoneyComb </td> </tr>
    <tr> <td> Strawberry </td> </tr>
    <tr> <td> Rainbow </td> </tr>
    <tr> <td> Rocky Road </td> </tr>
    <tr> <td> Rum And Raisin </td> </tr>
    <tr> <td> Lemon Sorbet </td> </tr> 
    <tr> <td> Raspberry Sorbet </td> </tr>
    <tr> <td> Mango Sorbet </td> </tr>
    <tr> <td> BoysenBerry</td> </tr>

 </tbody>
 </table>

<script type="text/javascript" src="icecream shop2.js"></script>
<script type="text/javascript" src="work.js"></script>
</body>
</html>  

JS

function myFunction2() {
    var user = prompt("How many hours do you want to work? pick a number between 1 and 10")
    var money = 0;

    if (user == "1") {
        money = money + 1 ; 
         document.getElementById("money").innerHTML=
            "Money: " + money +"";
    console.log(money)
    } else if (user == "2") {
       money = money + 2 ;
          document.getElementById("money").innerHTML=
             "Money: "+ money +"";
               console.log(money)
     } else if (user == "3") {
        money = money + 3 ;  
          document.getElementById("money").innerHTML=
             "Money: "+ money +"";
                console.log(money)
     } else if (user == "4") {
        money = money + 4 ;
          document.getElementById("money").innerHTML=
             "Money: "+ money +"";
                console.log(money) 
     } else if (user == "5") {
        money = money + 5 ;
          document.getElementById("money").innerHTML=
             "Money: "+ money +"";
                console.log(money)
     } else if (user == "6") {
        money = money + 6 ;
          document.getElementById("money").innerHTML=
             "Money: "+ money +"";
                console.log(money);
     } else if (user == "7") {
        money = money + 7 ;
          document.getElementById("money").innerHTML=
             "Money: "+ money +"";
                console.log(money);
     } else if (user == "8") {
        money = money + 8 ;
          document.getElementById("money").innerHTML=
             "Money: "+ money +"";
                console.log(money);
     } else if (user == "9") {
        money = money + 9 ;
          document.getElementById("money").innerHTML=
             "Money: "+ money +"";
                console.log(money);
     } else if (user == 10) {
        money = money + 10;
          document.getElementById("money").innerHTML=
             "Money: "+ money +"";
                console.log(money); 
     } else {
        alert("you chose either to few hours or to many hours")

    }
   }
1
  • Is the expected result that when you click that button a second time it adds the value? Then don’t reset money to 0 in myFunction2. Also, you didn’t close the <h1> correctly. Commented Apr 19, 2017 at 0:17

3 Answers 3

1

Each time myFunction2() is called money var money is a new one. Try to put var in a closure or in a function enclosing myFunction2() declaration.

Sign up to request clarification or add additional context in comments.

Comments

0

That is, because money is a local variable in your function. Each time the function is called it creates a new variable called money and sets it to 0.

If you want money to keep its value you should put it outside of this function like this:

var money = 0;
function myFunction2() { ... }

Comments

0

You are declaring money and assigning a value of zero to it every time you call the function. So it always starts at zero and then adds the input value to it. Declare money outside of the function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.