0

This is my first attempt at using App Script, but I want to create a form with 50 questions with the same two choices utilizing radio buttons.

I was hoping I could create an array with the questions and just iterate through each question in the array (with a for loop) to create the question items with the same choices, but not exactly clear on how to implement this perhaps via an index with the .setTitle(item) object.

Thank you and any guidance would be appreciated.

// radiobuttons  
var items = ["Q1", "Q2", "Q3"];         
var arrayLength = items.length; 
var roundNumber = 0;
var choices = ["Successful", "Unsuccessful"];  

for (i = 0; i < arrayLength; i++ { . // Incomplete

form.addMultipleChoiceItem()  
   .setTitle(item)  
   .setChoiceValues(choices)  
   .setRequired(true); 
4
  • Your for loop is missing the closing ) Commented May 17, 2018 at 20:41
  • But, wouldn't I need an index or something to refer to each array element within the .setTitle(item) like .setTitle(i) versus .setTitle(item) ? Commented May 17, 2018 at 22:55
  • Yes you would. I see now, you want to loop through items? Commented May 18, 2018 at 0:30
  • Side notes, there's no point in using arrayLength, just do the standard way, i < items.Length. And woudn't a better name for items be titles, seeing as that's what you're using it for? Commented May 18, 2018 at 0:41

1 Answer 1

1

If what you want to do is loop through items, there's 3 ways to do so:

For Loop

for (var i = 0; i < items.Length; i++) {
   form.addMultipleChoiceItem()
      .setTitle(items[i])           // Index the array by using items[i]
      .setChoiceValues(choices)
      .setRequired(true);
}

Foreach Loop

foreach (var item in items) {
    form.addMultipleChoiceItem()
       .setTitle(item)
       .setChoiceValues(choices)
       .setRequired(true);
}

Anonymous Function

items.forEach(function(item) {
    form.addMultipleChoiceItem()
       .setTitle(item)
       .setChoiceValues(choices)
       .setRequired(true);
}

They all do the same thing, but different syntax.

Sign up to request clarification or add additional context in comments.

Comments

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.