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

141
Visualizações
Button function not working like another one?

So I'm trying to write this simple program which one will count the visitors who went into gallery. The problem is the function live their own life. I can see this one( I' m talking about: "Previous Entries:" ) start counting when the gallery is full but only to one. I tryed many ways. I think sometimes wrong is with function call

let count = 0
let entryEl = document.getElementById("entry-el")
let leftEl = document.getElementById("left-el")
let countEl = document.getElementById("count-el")
let previousEl = document.getElementById("previous-el")

function peopleInShop() {
  count += 1
  countEl.textContent = count
  if (count <= 5) {
    window.alert("Welcome")
  }
  if (count >= 6 && count != 0) {
    window.alert("You can not Entry to the shop now! Wait till someone left the shop!")
  }
  if (count == 6) {
    count -= 1
    countEl.textContent = 5
  } else {
    pass
  }

}

function left() {
  count -= 1
  if (count == -1) {
    count += 1
  }
  countEl.textContent = count

}

function previousVisitors() {
  a = 0
  a = a + 1
  previousEl.textContent = a
}
<h1> People in the gallery:</h1>
<h2 id="count-el"> 0</h2>
<button id="entry-el" onclick="peopleInShop() ; previousVisitors()">Entry</button>
<button id="left-el" onclick="left()">Left</button>
<p> Previous Entries: </p>
<h3 id="previous-el">0</h3>
<br>
<p><u>Ther avarage quantity of people in the shop can not be higher than five persons!</u></p>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

a is set to 0 every time you call previousVisitors(). You need to declare the variable outside of the function like the others.

let count = 0;
let entryEl = document.getElementById("entry-el");
let leftEl = document.getElementById("left-el");
let countEl = document.getElementById("count-el");
let previousEl = document.getElementById("previous-el");
let a = 0;

function peopleInShop() {
  //count += 1
  count++;
  countEl.textContent = count;
  if (count <= 5) {
    window.alert("Welcome");
  }
  if (count >= 6 && count !== 0) {
    window.alert("You can not Entry to the shop now! Wait till someone left the shop!");
  }
  if (count === 6) {
    //count -= 1;
    count--;
    countEl.textContent = 5;
  } else {
    //pass
  }

}

function left() {
  count--;
  if (count === -1) {
    count++;
  }
  countEl.textContent = count;

}

function previousVisitors() {
  a++;
  previousEl.textContent = a;
}
<h1> People in the gallery:</h1>
<h2 id="count-el"> 0</h2>
<button id="entry-el" onclick="peopleInShop() ; previousVisitors()">Entry</button>
<button id="left-el" onclick="left()">Left</button>
<p> Previous Entries: </p>
<h3 id="previous-el">0</h3>
<br>
<p><u>Ther avarage quantity of people in the shop can not be higher than five persons!</u></p>

Also, pass is declared in Python but not JS. You can just leave those brackets empty. Also, in JS (and many other languages), += 1 and -= 1 can be shortened to ++ and -- respectively. And if you know that two variables will always be of the same type, then you can use === instead of == (JS only). === is faster since == converts data types in JS. Also, please end every line with a semicolon. It is not required in JS, but it is a standard, unlike Python.

about 4 years ago · Juan Pablo Isaza Relatório

0

Hi,

First you have pass in your code that is undefined

So js code stop on the line 22

If you haven't any to statement on fault just remove else statement

In previousVisitors function you have declare a variable in the function

So a always = 0

And you use a = a + 1

So previousVistors will always be 1

To Solve declare a variable in global scoop not in function

like this

let a = 0;
function previousVisitors() {
  a = a + 1;
  previousEl.textContent = a;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

let a = 0    
function previousVisitors() {
  a +=1
  previousEl.textContent = a
}

In your last function: Define "a" variable, And update the value when calling this function (When pressing the Button),

Call the function like That:

<button id="left-el" onclick="left();previousVisitors()">Left</button>
about 4 years ago · Juan Pablo Isaza 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