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

152
Views
My for loop runs only once while trying to reverse an array in javascript

var amount = Number(window.prompt("number of elements"));
var list = []
for (var i = 0; i < amount; i++); {
  list.push(window.prompt("enter elements of the list"));
}
list.reverse();
document.write(list);

I tried to reverse a list but whatever amount I enter my code only runs trough for loop once, asks me for element of the list and ends the for loop and prints out my one and only entry since the for loop doesn't ask me for elements multiple times. I'm a beginner it's a silly mistake probably but I just can't figure it out.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As @Barmar pointed out the problem is due to a logic error.

/* Read the item from the prompt until you and write it to the list container. */
function read(list, size){
  for(let i = 0 ; i < size ; ++i)
    list.push(window.prompt());
}

/* Print a list container */
function print(list){
   for(let i = 0 ; i < list.length ; ++i)
    console.log(list[i]);
}

var list = []
read(list, Number(window.prompt("number of elements")));
list.reverse();
print(list);

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!