1

I'm getting an error Uncaught error unexpected token ( in script tag on string with for loop

<script type="text/javascript">
 var offer_foto_index = 'slide_offer_preview_1';
 var slide_offer_preview_ = [];
 var photo_array = <%= @photos.to_json.html_safe %>;
 var offer_foto = {
 for(var i = 0; i < <%= @photos.length %>; i++) {
 slide_offer_preview_[i] = photo_array[i];
 }
 }
</script>

I need an offer_photo variable which should contain arrays slide_offer_preview_1, slide_offer_preview_2, slide_offer_preview_3 etc.

Where did I make a mistake? Thanks!

asked Mar 26, 2014 at 10:52
1
  • you defined an object offer_foto with no properties, instead, you put an instruction, that's not permitted Commented Mar 26, 2014 at 10:59

2 Answers 2

1

Answer based on your comment: "...I need a offer_photo variable. It should contain array like slide_offer_preview_1, slide_offer_preview_2 etc."

<script type="text/javascript">
 var offer_foto_index = 'slide_offer_preview_1';
 var slide_offer_preview_ = [];
 var photo_array = <%= @photos.to_json.html_safe %>;
 var offer_foto = {};
 for(var i = 0; i < <%= @photos.length %>; i++) {
 offer_photo['slide_offer_preview_' + i] = photo_array[i];
 }
</script>

Or if you need it to be in one array within "offer_foto" object:

var offer_foto = {};
offer_foto.slide_offer_preview_ = [];
for(var i = 0; i < <%= @photos.length %>; i++) {
 offer_photo.slide_offer_preview_[0] = photo_array[i];
}
answered Mar 26, 2014 at 11:07
Sign up to request clarification or add additional context in comments.

Comments

1

Without seeing what your actual rendered code looks like, this will be difficult. But my guess is you should be using photo_array instead of @photos:

<script type="text/javascript">
 var offer_foto_index = 'slide_offer_preview_1';
 var slide_offer_preview_ = [];
 var photo_array = <%= @photos.to_json.html_safe %>;
 //var offer_foto = { - I've commented this because you don't need it...
 for(var i = 0; i < photo_array.length; i++) {
 slide_offer_preview_[i] = photo_array[i];
 }
 //}
</script>
answered Mar 26, 2014 at 10:55

3 Comments

Thanks for helping, but I need a offer_photo variable. It should contain array like slide_offer_preview_1, slide_offer_preview_2 etc.
I'm not quite sure I can use loop inside var
I think you need to rewrite your question. This isn't explained in your post.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.