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

251
Views
How to ask same question in a loop and receive different correct answer and show it?

I'm trying to make the web ask the same question and receive different, but I can't make it save the first answer and ask it again and save another answer.

var numWeb = 1;
var web = 0;

while (web < numWeb){
var arr = [`reference1`, `reference2`, `reference3`,`reference4`,`reference5`, `reference6`];
    arr[0] = prompt("what your name");
    arr[1] = prompt("when you birth ?");
    arr[2] = prompt("What hobby you love to play ?");
    arr[3] = prompt("Where you live ?");
    arr[4] = prompt("do you know bill gate");
    arr[5] = prompt("when you graduate? (YYYY/MM/DD)");
    if (arr[1] == []){
    arr[1] = "not given";
    }
    if (arr[4] == []){
        arr[4] = arr.splice(4); 

    }var answer = prompt("do you need to ask other people? Y/N");

if (answer == "Y" || answer == "y" ) {
      document.write(arr);
      prompt(arr);
    web ++
numWeb++
}
else if (answer == "n" || answer == "N" ){
    web ++
    document.write(arr)
   
}
else {
    alert("ERROR");

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

0

I've fixed some of the issues I mentioned in the comment above. Your code actually would have performed your desired purpose of displaying all responses if it had been error-free.

var numWeb = 1;
var web = 0;

while (web < numWeb){
    var arr = [`reference1`, `reference2`, `reference3`,`reference4`,`reference5`, `reference6`];
        arr[0] = prompt("what your name");
        arr[1] = prompt("when you birth ?");
        arr[2] = prompt("What hobby you love to play ?");
        arr[3] = prompt("Where you live ?");
        arr[4] = prompt("do you know bill gate");
        arr[5] = prompt("when you graduate? (YYYY/MM/DD)");
        if (arr[1] == []){
            arr[1] = "not given";
        }
        if (arr[4] == []){
            arr[4] = arr.splice(4); 
        }
        var answer = prompt("do you need to ask other people? Y/N");

    if (answer == 'Y' || answer == 'y' ) { // added quotes to compare with string
        document.write(arr + '<br>'); // added line break to display in a separate line
        prompt(arr); // what is the purpose of prompting this rather than displaying as an alert?
        web++
        numWeb++ // corrected capitalization
    }
    else if (answer == 'n' || answer == 'N' ) { // added quotes to compare with string
        web++
        document.write(arr + '<br>') // added line break to display in a separate line
    }
    else {
        alert("ERROR"); // added quotes to display a string
    }
};

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!