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

162
Views
Adding prompts to an array in javascript

I'm busy with a task that requires me to ask the user to keep entering random numbers until the number is "-1". After that I would have to get the average of all the numbers entered excluding the "-1". I've gotten this far with it:

var userNumbers;
while (userNumbers !== "-1") {
    userNumbers = prompt("Enter a number");
}
numbersArray = [userNumbers];
console.log(numbersArray);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Try this

// Store all numbers
const numbers = [];

let userNumber;
for(;;){
    userNumber = prompt("Enter a number");
    if(userNumber === '-1') { break; }
    numbers.push(userNumber);
}

// Calculate average
let sum = 0;
let avg = 0;

numbers.forEach((value) => sum += value);

avg = sum / numbers.length
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!