0

I'm going to have unknown number of prompt dialogs.
I will have their number after some user actions, but I want to create them, display them and at the end check if some of them is null(empty).
I'm looking for ideas how to do that.
Can I do it without loop?
Something like:

arrayName[] = prompt("Enter text");
arrayName[] = prompt("Enter text");
arrayName[] = prompt("Enter text");
arrayName[] = prompt("Enter text");

and then check if some of them is empty?
And can I declare array in shorthand way like in PHP and C#?

array[]
array[]
4
  • "Can I do it without loop?" Why would you want to? This is what loops are for. Commented Apr 23, 2013 at 9:41
  • How do you define how many prompts have to be defined? Commented Apr 23, 2013 at 9:49
  • is it a value passed from the server? or a count calculated at the client side or decided based on a value entered by the user in one of the prompts Commented Apr 23, 2013 at 9:50
  • Nope. The value is typed by the user. I wanted to escape the loop, because I have to send an AJAX request to the server for each prompt in the array and I just wanted to ignore the empty prompts. Commented Apr 23, 2013 at 10:07

1 Answer 1

1

Yes you can and you can use =[] to create the array

var arrayName=[];
arrayName.push(prompt("Enter text"));
arrayName.push(prompt("Enter text"));
arrayName.push(prompt("Enter text"));
arrayName.push(prompt("Enter text"));
arrayName.push(prompt("Enter text"));

for (var i=0;i<arrayNames.length;i++) {
  if (arrayName[i]==null) alert("You did not answer "+(i+1))
}

If you can use a loop it is of course simpler. Please tell use why you do not want to use a loop?

var arrayName=[];
for (var i=0;i<unknownNumberOfPrompts;i++) {
  var answer = prompt("Enter text","");
  if (answer) arrayName.push(answer);
}
if (arrayname.length>0) {
  // something was answered
}
Sign up to request clarification or add additional context in comments.

5 Comments

"I'm going to have unknown number of prompt dialogs." (my emphasis)
Yes, my code will check any number of prompts assuming each prompt is specified as arrayName.push(prompt("Enter text"));
But again, it's an unknown number. So clearly they can't just be written in the source; that would be known. (He also asked to do this "without loop" for some completely unknown reason...) Anyway, I leave you and the OP to it. If the question ever makes sense, he's in good hands getting it answered. :-)
I will create a loop and it will set the correct number of arrays.
It may be unknown to the script at the time of writing and receive an unknown number of prompts from a cfout or php echo or whatever

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.