Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!