I have the following script:
function getMoods(nb) {
    var index;
    var a = ["Banana", "Coconut", "Peach", "Apple", ...];
    for (index=0; index<nb; ++index) {
        alert('a');
        if(index==1 || index==5 || index==9 || index==13) { moods += '<div class="col-xs-4">'; }
            moods += '
                <div class="checkbox">
                    <label for="'+a[index]+'">
                        <input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index]+'
                    </label>
                </div>';
        if(index==4 || index==8 || index==12) { moods += '</div> '; }
    }
    $("#moods-area").html(moods);
}
I do not understand why I have the following error:
SyntaxError: Unexpected EOF
Could you please help me ?
There are two problems:
A wrong use of the spread operator ...:
["Banana", "Coconut", "Peach", "Apple", ...];
This throws SyntaxError: expected expression, got ']', because after the spread operator there isn't any iterable object.
JavaScript doesn't support multiline strings.
You can use some alternatives:
Concatenate multiple strings
moods += 
  '<div class="checkbox">'
    +'<label for="'+a[index]+'">'
      +'<input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index]
    +'</label>'
  +'</div>';
Use \ at the end of each line to continue the string at the next one
moods += '\
  <div class="checkbox">\
    <label for="'+a[index]+'">\
      <input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index]+'\
    </label>\
  </div>';
Join an array of strings:
moods += [
  '<div class="checkbox">',
    '<label for="'+a[index]+'">',
      '<input type="checkbox" id="'+a[index]+'" class="moods"> '+a[index],
    '</label>',
  '</div>'].join('');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With