How to make a Function, replace the value of a variable in JS?

Asked

Viewed 43 times

-2

Hello! I am developing a little game in Java Script, but I am with a difficulty in the moment that the player buys a coins bonus, it will replace a value in another function, for when it is activated, the player instead of winning 1 point, will earn 2 points. But inflictly, I can’t make the variable ${money}, worth 2 points instead of 1, and that goes for the other two bonuses you have in the game! Any help or assistance is of great value! Follow the code:

<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Clique para ganhar!</title>
    <style>
        body{
            background-color: rgb(12, 12, 136);
            margin: 25px;
            
        }
        h1{
            text-align: center;
            font-family: 'Lexend Mega', sans-serif;
        }
        form{
            background-color: white;
            

        }
        div{
            text-align: center;
        }
        #clqg{
            margin: 10px;
            padding: 15px 15px;
            font-family: 'Happy Monkey', cursive;
            font-size: large;
        }
        .buyling{
            padding: 10px 0px 0px;
        }
        .title{
            font-size: xx-large;
            font-family: 'Lexend Mega', sans-serif;
        }
        .btnbuy{
            margin: 15px;
        }
    </style>
</head>
<body>
    <h1>Clique, Ganhe, Vença!</h1>
    
    
    <div id="titleex">
    
        <form>
            <div id="money" class="title"></div>
            <input type="button" id="clqg"  value="Ganhar!" onclick="button()">
            <div class="buyling" id="btnone">
                Bonús: 75$
                <input type="button" class="btnbuy"  value="Comprar" onclick="buyone()">
            </diV>
            
            <div id="btntwo">
                Híper bonús: 125$
                <input type="button" class="btnbuy" value="Comprar" onclick="buytwo()">
            </div>
            
            <div id="btn3">
                Ultimate final: 1000$
                <input type="button" class="btnbuy" value="Comprar" onclick="buythree()">
            </div>
        </form>

    </div>
    
    <script>var txt = document.getElementById('money') // div onde vai o valor da moeda //
var money = Number(0) // valor da moeda inicial = 0 //
function button() {
   money += 1
   txt.innerHTML = `Seu saldo é igual a <strong>${money}$</strong>`
   
   if (){
      money += 2
   } // aqui vai o botão de ganahr $ //
   
}
function buyone(){
   var btn1 = document.getElementById("btnone")
   if (money >= 75){
      btn1.innerHTML = `Comprado!`
      var m1 = 
   }else {
      window.alert('Seu saldo é insuficiente!')
   } // aqui vai a opção de comprar o dobro de $ //
}
function buytwo(){
   var btn2 = document.getElementById('btntwo')
   if(money >= 125){
      money += 4
      btn2.innerHTML = `Comprado!`
   } else{
      window.alert("Seu saldo é insuficiente!")
   } // opção de dobrar os pontos //
}
function buythree(){
   var btn3 = document.getElementById('titleex')
   if (money >= 1000){
      btn3.innerHTML = `Parabens, você ganhou!`
   }
} // aqui se zera o game //

</script>

</body>
</html>```

1 answer

0


You can assign declare a variable somador and use it to modify the value of money. then just modify the value of somador initially equal to 1, for the amount you want.

Example:

var txt = document.getElementById('money') // div onde vai o valor da moeda //
var money = Number(0); // valor da moeda inicial = 0 //
var somador = 1;

function button() {
  money += somador
  txt.innerHTML = `Seu saldo é igual a <strong>${money}$</strong>`
}

function buyone() {
  var btn1 = document.getElementById("btnone")
  if (money >= 75) {
    money -= 75;
    btn1.innerHTML = `Comprado!`
    somador = 2;
  } else {
    window.alert('Seu saldo é insuficiente!')
  }
}

function buytwo() {
  var btn2 = document.getElementById('btntwo')
  if (money >= 125) {
    money -= 125;
    somador = 4;
    btn2.innerHTML = `Comprado!`
  } else {
    window.alert("Seu saldo é insuficiente!")
  } // opção de dobrar os pontos //
}

function buythree() {
  var btn3 = document.getElementById('titleex')
  if (money >= 1000) {
    money = 0;
    btn3.innerHTML = `Parabens, você ganhou!`
  }
} // aqui se zera o game //
body {
  background-color: rgb(12, 12, 136);
  margin: 25px;
}

h1 {
  text-align: center;
  font-family: 'Lexend Mega', sans-serif;
}

form {
  background-color: white;
}

div {
  text-align: center;
}

#clqg {
  margin: 10px;
  padding: 15px 15px;
  font-family: 'Happy Monkey', cursive;
  font-size: large;
}

.buyling {
  padding: 10px 0px 0px;
}

.title {
  font-size: xx-large;
  font-family: 'Lexend Mega', sans-serif;
}

.btnbuy {
  margin: 15px;
}
<html lang="pt-br">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Clique para ganhar!</title>
</head>

<body>
  <h1>Clique, Ganhe, Vença!</h1>
  <div id="titleex">
    <form>
      <div id="money" class="title"></div>
      <input type="button" id="clqg" value="Ganhar!" onclick="button()">
      <div class="buyling" id="btnone">
        Bonús: 75$
        <input type="button" class="btnbuy" value="Comprar" onclick="buyone()">
      </diV>
      <div id="btntwo">
        Híper bonús: 125$
        <input type="button" class="btnbuy" value="Comprar" onclick="buytwo()">
      </div>
      <div id="btn3">
        Ultimate final: 1000$
        <input type="button" class="btnbuy" value="Comprar" onclick="buythree()">
      </div>
    </form>
  </div>
</body>

</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.