Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
how can i make this script show the value of the var "per" based on the new value of the var "a" that raise on every click

I want to be able to display a 'clicker' variable, storing the number of clicks on a certain element, and then display it when i click on another element.

per = a * 6.6666 so I tried again and it wont work the result stay 0 but if I change the value of a to 1 it shows 6.6666 so the calculation is working fine but it is not taking the value of a based on how many times I click it just take the value I gave in the script ...

    var a =  0 ;
    const six = 6.6666;
            
    var per = a * six;

    $(".mychoice").click(function () {
        (a++);
    });

    $(".show").click(function () {
        $("#ss").text( per );
    });
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Calculations are done when you tell them to be done.

If you change the value of a in a click event, then that value doesn't travel back through time so that a is the new value back when you read it and used it to multiply six.

If you want to redo that calculation when something is clicked then you need to write that expression in that click event handler function.

about 4 years ago · Santiago Trujillo Relatório

0

Keep in mind that a and per are two distinct locations in memory, therefore, you have to update the per variable along with the a variable.

var a = 0;
const six = 6.6666;
var per = a * six;
$(".mychoice").click(function () {
    (a++);
    // memory location labelled "a" has changed
    // update memory location labelled "per" accordingly
    per = a * six;
});

$(".show").click(function () {
    $("#ss").text(per);
});
about 4 years ago · Santiago Trujillo Relatório

0

var per = a * six;

The calculation is executed and stored in the per variable as soon as the line is executed, not when you reference the per variable.

If you want to calculate the value at a later time, use an (arrow) function instead.

// define `per` as arrow function with 0 arguments
var per = () => a * six;

Then you evoke the function when you need the result:

$(".show").click(function () {
    $("#ss").text( per() );
});

In this scenario doing the calculation directly, without defining a function, might be cleaner.

$(".show").click(function () {
    $("#ss").text(a * six);
});
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda